Skip to content

Commit 0a849c0

Browse files
committed
[LeetCode Sync] Runtime - 102 ms (100.00%), Memory - 35.2 MB (58.71%)
1 parent c921cca commit 0a849c0

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p>
2+
3+
<p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p>
4+
5+
<ul>
6+
<li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li>
7+
<li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li>
8+
<li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li>
9+
<li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li>
10+
</ul>
11+
12+
<p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> obstacles = [1,2,3,2]
19+
<strong>Output:</strong> [1,2,3,3]
20+
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
21+
- i = 0: [<u>1</u>], [1] has length 1.
22+
- i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2.
23+
- i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3.
24+
- i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> obstacles = [2,2,1]
31+
<strong>Output:</strong> [1,2,1]
32+
<strong>Explanation: </strong>The longest valid obstacle course at each position is:
33+
- i = 0: [<u>2</u>], [2] has length 1.
34+
- i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2.
35+
- i = 2: [2,2,<u>1</u>], [1] has length 1.
36+
</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> obstacles = [3,1,5,6,4,2]
42+
<strong>Output:</strong> [1,1,2,3,2,2]
43+
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
44+
- i = 0: [<u>3</u>], [3] has length 1.
45+
- i = 1: [3,<u>1</u>], [1] has length 1.
46+
- i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid.
47+
- i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid.
48+
- i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid.
49+
- i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2.
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>n == obstacles.length</code></li>
57+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
58+
<li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li>
59+
</ul>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# class BinaryIndexedTree:
2+
# def __init__ (self, n: int):
3+
# self.n = n
4+
# self.tree = [0] * (n + 1)
5+
6+
# def update(self, index: int, val: int):
7+
# while index <= self.n:
8+
# self.tree[index] = max(self.tree[index], val)
9+
# index += index & -index
10+
11+
# def query(self, index: int):
12+
# result = 0
13+
# while index:
14+
# result = max(self.tree[index], result)
15+
# index -= index & -index
16+
# return result
17+
18+
# class Solution:
19+
# def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
20+
# nums = sorted(set(obstacles))
21+
# n = len(nums)
22+
23+
# tree = BinaryIndexedTree(n)
24+
# result = []
25+
# for val in obstacles:
26+
# idx = bisect_left(nums, val) + 1
27+
# result.append(tree.query(idx) + 1)
28+
# tree.update(idx, result[-1])
29+
# return result
30+
31+
class Solution:
32+
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
33+
count = 0
34+
dp, result = [], []
35+
36+
for ob in obstacles:
37+
if not dp or ob >= dp[-1]:
38+
dp.append(ob)
39+
count += 1
40+
result.append(count)
41+
else:
42+
idx = bisect_right(dp, ob)
43+
dp[idx] = ob
44+
result.append(idx + 1)
45+
return result

0 commit comments

Comments
 (0)