Skip to content

Commit 6d1bae3

Browse files
committed
+ problem 1964
1 parent 0f05387 commit 6d1bae3

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 1964. Find the Longest Valid Obstacle Course at Each Position
2+
You want to build some obstacle courses. You are given a **0-indexed** integer array `obstacles` of length `n`, where `obstacles[i]` describes the height of the <code>i<sup>th</sup></code> obstacle.
3+
4+
For every index `i` between `0` and `n - 1` (**inclusive**), find the length of the **longest obstacle course** in `obstacles` such that:
5+
6+
* You choose any number of obstacles between `0` and `i` **inclusive**.
7+
* You must include the <code>i<sup>th</sup></code> obstacle in the course.
8+
* You must put the chosen obstacles in the **same order** as they appear in `obstacles`.
9+
* Every obstacle (except the first) is **taller** than or the **same height** as the obstacle immediately before it.
10+
11+
Return *an array* `ans` *of length* `n`, *where* `ans[i]` *is the length of the **longest obstacle course** for index* `i` *as described above*.
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> obstacles = [1,2,3,2]
16+
<strong>Output:</strong> [1,2,3,3]
17+
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
18+
- i = 0: [1], [1] has length 1.
19+
- i = 1: [1,2], [1,2] has length 2.
20+
- i = 2: [1,2,3], [1,2,3] has length 3.
21+
- i = 3: [1,2,3,2], [1,2,2] has length 3.
22+
</pre>
23+
24+
#### Example 2:
25+
<pre>
26+
<strong>Input:</strong> obstacles = [2,2,1]
27+
<strong>Output:</strong> [1,2,1]
28+
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
29+
- i = 0: [2], [2] has length 1.
30+
- i = 1: [2,2], [2,2] has length 2.
31+
- i = 2: [2,2,1], [1] has length 1.
32+
</pre>
33+
34+
#### Example 3:
35+
<pre>
36+
<strong>Input:</strong> obstacles = [3,1,5,6,4,2]
37+
<strong>Output:</strong> [1,1,2,3,2,2]
38+
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
39+
- i = 0: [3], [3] has length 1.
40+
- i = 1: [3,1], [1] has length 1.
41+
- i = 2: [3,1,5], [3,5] has length 2. [1,5] is also valid.
42+
- i = 3: [3,1,5,6], [3,5,6] has length 3. [1,5,6] is also valid.
43+
- i = 4: [3,1,5,6,4], [3,4] has length 2. [1,4] is also valid.
44+
- i = 5: [3,1,5,6,4,2], [1,2] has length 2.
45+
</pre>
46+
47+
#### Constraints:
48+
* `n == obstacles.length`
49+
* <code>1 <= n <= 10<sup>5</sup></code>
50+
* <code>1 <= obstacles[i] <= 10<sup>7</sup></code>
51+
52+
## Solutions (Python)
53+
54+
### 1. Solution
55+
```Python
56+
from sortedcontainers import SortedList
57+
58+
59+
class Solution:
60+
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
61+
n = len(obstacles)
62+
sl = SortedList([(0, 0)])
63+
ans = [1] * n
64+
65+
for i in range(n):
66+
j = sl.bisect_left((obstacles[i], n)) - 1
67+
ans[i] = sl[j][1] + 1
68+
69+
if j + 1 < len(sl) and sl[j + 1][1] == ans[i]:
70+
sl.pop(j + 1)
71+
if sl[j][0] == obstacles[i]:
72+
sl.pop(j)
73+
74+
sl.add((obstacles[i], ans[i]))
75+
76+
return ans
77+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 1964. 找出到每个位置为止最长的有效障碍赛跑路线
2+
你打算构建一些障碍赛跑路线。给你一个 **下标从 0 开始** 的整数数组 `obstacles` ,数组长度为 `n` ,其中 `obstacles[i]` 表示第 `i` 个障碍的高度。
3+
4+
对于每个介于 `0``n - 1` 之间(包含 `0``n - 1`)的下标 `i` ,在满足下述条件的前提下,请你找出 `obstacles` 能构成的最长障碍路线的长度:
5+
6+
* 你可以选择下标介于 `0``i` 之间(包含 `0``i`)的任意个障碍。
7+
* 在这条路线中,必须包含第 `i` 个障碍。
8+
* 你必须按障碍在 `obstacles` 中的 **出现顺序** 布置这些障碍。
9+
* 除第一个障碍外,路线中每个障碍的高度都必须和前一个障碍 **相同** 或者 **更高**
10+
11+
返回长度为 `n` 的答案数组 `ans` ,其中 `ans[i]` 是上面所述的下标 `i` 对应的最长障碍赛跑路线的长度。
12+
13+
#### 示例 1:
14+
<pre>
15+
<strong>输入:</strong> obstacles = [1,2,3,2]
16+
<strong>输出:</strong> [1,2,3,3]
17+
<strong>解释:</strong> 每个位置的最长有效障碍路线是:
18+
- i = 0: [1], [1] 长度为 1
19+
- i = 1: [1,2], [1,2] 长度为 2
20+
- i = 2: [1,2,3], [1,2,3] 长度为 3
21+
- i = 3: [1,2,3,2], [1,2,2] 长度为 3
22+
</pre>
23+
24+
#### 示例 2:
25+
<pre>
26+
<strong>输入:</strong> obstacles = [2,2,1]
27+
<strong>输出:</strong> [1,2,1]
28+
<strong>解释:</strong> 每个位置的最长有效障碍路线是:
29+
- i = 0: [2], [2] 长度为 1
30+
- i = 1: [2,2], [2,2] 长度为 2
31+
- i = 2: [2,2,1], [1] 长度为 1
32+
</pre>
33+
34+
#### 示例 3:
35+
<pre>
36+
<strong>输入:</strong> obstacles = [3,1,5,6,4,2]
37+
<strong>输出:</strong> [1,1,2,3,2,2]
38+
<strong>解释:</strong> 每个位置的最长有效障碍路线是:
39+
- i = 0: [3], [3] 长度为 1
40+
- i = 1: [3,1], [1] 长度为 1
41+
- i = 2: [3,1,5], [3,5] 长度为 2, [1,5] 也是有效的障碍赛跑路线
42+
- i = 3: [3,1,5,6], [3,5,6] 长度为 3, [1,5,6] 也是有效的障碍赛跑路线
43+
- i = 4: [3,1,5,6,4], [3,4] 长度为 2, [1,4] 也是有效的障碍赛跑路线
44+
- i = 5: [3,1,5,6,4,2], [1,2] 长度为 2
45+
</pre>
46+
47+
#### 提示:
48+
* `n == obstacles.length`
49+
* <code>1 <= n <= 10<sup>5</sup></code>
50+
* <code>1 <= obstacles[i] <= 10<sup>7</sup></code>
51+
52+
## 题解 (Python)
53+
54+
### 1. 题解
55+
```Python
56+
from sortedcontainers import SortedList
57+
58+
59+
class Solution:
60+
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
61+
n = len(obstacles)
62+
sl = SortedList([(0, 0)])
63+
ans = [1] * n
64+
65+
for i in range(n):
66+
j = sl.bisect_left((obstacles[i], n)) - 1
67+
ans[i] = sl[j][1] + 1
68+
69+
if j + 1 < len(sl) and sl[j + 1][1] == ans[i]:
70+
sl.pop(j + 1)
71+
if sl[j][0] == obstacles[i]:
72+
sl.pop(j)
73+
74+
sl.add((obstacles[i], ans[i]))
75+
76+
return ans
77+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class Solution:
5+
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
6+
n = len(obstacles)
7+
sl = SortedList([(0, 0)])
8+
ans = [1] * n
9+
10+
for i in range(n):
11+
j = sl.bisect_left((obstacles[i], n)) - 1
12+
ans[i] = sl[j][1] + 1
13+
14+
if j + 1 < len(sl) and sl[j + 1][1] == ans[i]:
15+
sl.pop(j + 1)
16+
if sl[j][0] == obstacles[i]:
17+
sl.pop(j)
18+
19+
sl.add((obstacles[i], ans[i]))
20+
21+
return ans

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@
11831183
[1961][1961l]|[Check If String Is a Prefix of Array][1961] |![py]
11841184
[1962][1962l]|[Remove Stones to Minimize the Total][1962] |![rs]
11851185
[1963][1963l]|[Minimum Number of Swaps to Make the String Balanced][1963] |![rs]
1186+
[1964][1964l]|[Find the Longest Valid Obstacle Course at Each Position][1964] |![py]
11861187
[1967][1967l]|[Number of Strings That Appear as Substrings in Word][1967] |![py]
11871188
[1968][1968l]|[Array With Elements Not Equal to Average of Neighbors][1968] |![rs]
11881189
[1969][1969l]|[Minimum Non-Zero Product of the Array Elements][1969] |![rs]
@@ -2718,6 +2719,7 @@
27182719
[1961]:Problemset/1961-Check%20If%20String%20Is%20a%20Prefix%20of%20Array/README.md#1961-check-if-string-is-a-prefix-of-array
27192720
[1962]:Problemset/1962-Remove%20Stones%20to%20Minimize%20the%20Total/README.md#1962-remove-stones-to-minimize-the-total
27202721
[1963]:Problemset/1963-Minimum%20Number%20of%20Swaps%20to%20Make%20the%20String%20Balanced/README.md#1963-minimum-number-of-swaps-to-make-the-string-balanced
2722+
[1964]:Problemset/1964-Find%20the%20Longest%20Valid%20Obstacle%20Course%20at%20Each%20Position/README.md#1964-find-the-longest-valid-obstacle-course-at-each-position
27212723
[1967]:Problemset/1967-Number%20of%20Strings%20That%20Appear%20as%20Substrings%20in%20Word/README.md#1967-number-of-strings-that-appear-as-substrings-in-word
27222724
[1968]:Problemset/1968-Array%20With%20Elements%20Not%20Equal%20to%20Average%20of%20Neighbors/README.md#1968-array-with-elements-not-equal-to-average-of-neighbors
27232725
[1969]:Problemset/1969-Minimum%20Non-Zero%20Product%20of%20the%20Array%20Elements/README.md#1969-minimum-non-zero-product-of-the-array-elements
@@ -4252,6 +4254,7 @@
42524254
[1961l]:https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/
42534255
[1962l]:https://leetcode.com/problems/remove-stones-to-minimize-the-total/
42544256
[1963l]:https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/
4257+
[1964l]:https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/
42554258
[1967l]:https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/
42564259
[1968l]:https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/
42574260
[1969l]:https://leetcode.com/problems/minimum-non-zero-product-of-the-array-elements/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@
11831183
[1961][1961l]|[检查字符串是否为数组前缀][1961] |![py]
11841184
[1962][1962l]|[移除石子使总数最小][1962] |![rs]
11851185
[1963][1963l]|[使字符串平衡的最小交换次数][1963] |![rs]
1186+
[1964][1964l]|[找出到每个位置为止最长的有效障碍赛跑路线][1964] |![py]
11861187
[1967][1967l]|[作为子字符串出现在单词中的字符串数目][1967] |![py]
11871188
[1968][1968l]|[构造元素不等于两相邻元素平均值的数组][1968] |![rs]
11881189
[1969][1969l]|[数组元素的最小非零乘积][1969] |![rs]
@@ -2718,6 +2719,7 @@
27182719
[1961]:Problemset/1961-Check%20If%20String%20Is%20a%20Prefix%20of%20Array/README_CN.md#1961-检查字符串是否为数组前缀
27192720
[1962]:Problemset/1962-Remove%20Stones%20to%20Minimize%20the%20Total/README_CN.md#1962-移除石子使总数最小
27202721
[1963]:Problemset/1963-Minimum%20Number%20of%20Swaps%20to%20Make%20the%20String%20Balanced/README_CN.md#1963-使字符串平衡的最小交换次数
2722+
[1964]:Problemset/1964-Find%20the%20Longest%20Valid%20Obstacle%20Course%20at%20Each%20Position/README_CN.md#1964-找出到每个位置为止最长的有效障碍赛跑路线
27212723
[1967]:Problemset/1967-Number%20of%20Strings%20That%20Appear%20as%20Substrings%20in%20Word/README_CN.md#1967-作为子字符串出现在单词中的字符串数目
27222724
[1968]:Problemset/1968-Array%20With%20Elements%20Not%20Equal%20to%20Average%20of%20Neighbors/README_CN.md#1968-构造元素不等于两相邻元素平均值的数组
27232725
[1969]:Problemset/1969-Minimum%20Non-Zero%20Product%20of%20the%20Array%20Elements/README_CN.md#1969-数组元素的最小非零乘积
@@ -4252,6 +4254,7 @@
42524254
[1961l]:https://leetcode.cn/problems/check-if-string-is-a-prefix-of-array/
42534255
[1962l]:https://leetcode.cn/problems/remove-stones-to-minimize-the-total/
42544256
[1963l]:https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/
4257+
[1964l]:https://leetcode.cn/problems/find-the-longest-valid-obstacle-course-at-each-position/
42554258
[1967l]:https://leetcode.cn/problems/number-of-strings-that-appear-as-substrings-in-word/
42564259
[1968l]:https://leetcode.cn/problems/array-with-elements-not-equal-to-average-of-neighbors/
42574260
[1969l]:https://leetcode.cn/problems/minimum-non-zero-product-of-the-array-elements/

0 commit comments

Comments
 (0)