Skip to content

Commit d908490

Browse files
authored
Merge pull request #73 from tiationg-kho/update
update
2 parents c3b362e + ebf20ab commit d908490

5 files changed

+72
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ Feel free to raise issues or post in discussions.
483483
| 461 | | [368. Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [M]dynamic-programming | linear sequence | [Python]([M]dynamic-programming/[M]dynamic-programming/368-largest-divisible-subset.py) |
484484
| 462 | | [1105. Filling Bookcase Shelves](https://leetcode.com/problems/filling-bookcase-shelves/) | [M]dynamic-programming | linear sequence | [Python]([M]dynamic-programming/[M]dynamic-programming/1105-filling-bookcase-shelves.py) |
485485
| 463 | O | [300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [M]dynamic-programming | LIS | [Python]([M]dynamic-programming/[M]dynamic-programming/300-longest-increasing-subsequence.py) |
486-
| 464 | | [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [M]dynamic-programming | LIS | [Python]([M]dynamic-programming/[M]dynamic-programming/334-increasing-triplet-subsequence.py) |
486+
| 464 | | [1964. Find the Longest Valid Obstacle Course at Each Position](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/) | [M]dynamic-programming | LIS | [Python]([M]dynamic-programming/[M]dynamic-programming/1964-find-the-longest-valid-obstacle-course-at-each-position.py) |
487487
| 465 | | [354. Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/) | [M]dynamic-programming | LIS | [Python]([M]dynamic-programming/[M]dynamic-programming/354-russian-doll-envelopes.py) |
488488
| 466 | | [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [M]dynamic-programming | double sequence | [Python]([M]dynamic-programming/[M]dynamic-programming/1143-longest-common-subsequence.py) |
489489
| 467 | | [72. Edit Distance](https://leetcode.com/problems/edit-distance/) | [M]dynamic-programming | double sequence | [Python]([M]dynamic-programming/[M]dynamic-programming/72-edit-distance.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
3+
4+
def find_first_larger(vals, num):
5+
left, right, boundary = 0, len(vals) - 1, - 1
6+
while left <= right:
7+
m = (left + right) // 2
8+
if num < vals[m]:
9+
boundary = m
10+
right = m - 1
11+
else:
12+
left = m + 1
13+
return boundary
14+
15+
o = obstacles
16+
res = [1 for _ in range(len(o))]
17+
dp = []
18+
for i, num in enumerate(o):
19+
if not dp or dp[- 1] <= num:
20+
dp.append(num)
21+
res[i] = len(dp)
22+
else:
23+
idx = find_first_larger(dp, num)
24+
dp[idx] = num
25+
res[i] = idx + 1
26+
return res
27+
28+
# time O(nlogn), due to binary search costs logn and traverse every num
29+
# space O(n), due to dp list
30+
# using dynamic programming and LIS and patience sort and greedy and binary search
31+
'''
32+
1. dp[i] means the smallest last num when subseq's length is i+1
33+
2. this num should greedily find out the smallest one
34+
3. when new num is greater or equal than last one means subsequence can grow
35+
4. else have to find this num can help which length's subsequence improve
36+
'''

[M]dynamic-programming/[M]dynamic-programming/300-longest-increasing-subsequence.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution:
22
def lengthOfLIS(self, nums: List[int]) -> int:
33
dp = [1 for _ in range(len(nums))]
4-
5-
for i in range(len(nums)):
6-
for j in range(i):
7-
if nums[j] < nums[i]:
8-
dp[i] = max(dp[i], dp[j] + 1)
9-
4+
for end in range(len(nums)):
5+
for start in range(end):
6+
if nums[start] < nums[end]:
7+
dp[end] = max(dp[end], dp[start] + 1)
108
return max(dp)
119

1210
# time O(n**2), due to each num has run a loop to check the nums before it
@@ -16,15 +14,26 @@ def lengthOfLIS(self, nums: List[int]) -> int:
1614
1. dp[i] means the length of LIS which ends in i
1715
'''
1816

19-
import bisect
2017
class Solution:
2118
def lengthOfLIS(self, nums: List[int]) -> int:
19+
20+
def find_first_larger_or_equal(vals, num):
21+
left, right, boundary = 0, len(vals) - 1, - 1
22+
while left <= right:
23+
m = (left + right) // 2
24+
if num <= vals[m]:
25+
boundary = m
26+
right = m - 1
27+
else:
28+
left = m + 1
29+
return boundary
30+
2231
dp = []
23-
for num in nums:
32+
for i, num in enumerate(nums):
2433
if not dp or dp[- 1] < num:
2534
dp.append(num)
2635
else:
27-
idx = bisect.bisect_left(dp, num)
36+
idx = find_first_larger_or_equal(dp, num)
2837
dp[idx] = num
2938
return len(dp)
3039

[M]dynamic-programming/[M]dynamic-programming/334-increasing-triplet-subsequence.py

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
import bisect
21
class Solution:
32
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
3+
4+
def find_first_larger_or_equal(vals, num):
5+
left, right, boundary = 0, len(vals) - 1, - 1
6+
while left <= right:
7+
m = (left + right) // 2
8+
if num <= vals[m]:
9+
boundary = m
10+
right = m - 1
11+
else:
12+
left = m + 1
13+
return boundary
14+
415
envelopes.sort(key = lambda x: (x[0], - x[1]))
516
dp = []
617
for w, h in envelopes:
718
if not dp or dp[- 1] < h:
819
dp.append(h)
920
else:
10-
idx = bisect.bisect_left(dp, h)
21+
idx = find_first_larger_or_equal(dp, h)
1122
dp[idx] = h
1223

1324
return len(dp)
1425

1526
# time O(nlogn)
1627
# space O(n)
17-
# using dynamic programming and LIS and patience sort and greedy and binary search and sort
28+
# using dynamic programming and LIS and patience sort and greedy and binary search and sort
29+
'''
30+
1. notice the condition of sorting
31+
'''

0 commit comments

Comments
 (0)