-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from tiationg-kho/update
update
- Loading branch information
Showing
5 changed files
with
72 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ng/[M]dynamic-programming/1964-find-the-longest-valid-obstacle-course-at-each-position.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution: | ||
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]: | ||
|
||
def find_first_larger(vals, num): | ||
left, right, boundary = 0, len(vals) - 1, - 1 | ||
while left <= right: | ||
m = (left + right) // 2 | ||
if num < vals[m]: | ||
boundary = m | ||
right = m - 1 | ||
else: | ||
left = m + 1 | ||
return boundary | ||
|
||
o = obstacles | ||
res = [1 for _ in range(len(o))] | ||
dp = [] | ||
for i, num in enumerate(o): | ||
if not dp or dp[- 1] <= num: | ||
dp.append(num) | ||
res[i] = len(dp) | ||
else: | ||
idx = find_first_larger(dp, num) | ||
dp[idx] = num | ||
res[i] = idx + 1 | ||
return res | ||
|
||
# time O(nlogn), due to binary search costs logn and traverse every num | ||
# space O(n), due to dp list | ||
# using dynamic programming and LIS and patience sort and greedy and binary search | ||
''' | ||
1. dp[i] means the smallest last num when subseq's length is i+1 | ||
2. this num should greedily find out the smallest one | ||
3. when new num is greater or equal than last one means subsequence can grow | ||
4. else have to find this num can help which length's subsequence improve | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
[M]dynamic-programming/[M]dynamic-programming/334-increasing-triplet-subsequence.py
This file was deleted.
Oops, something went wrong.
20 changes: 17 additions & 3 deletions
20
[M]dynamic-programming/[M]dynamic-programming/354-russian-doll-envelopes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import bisect | ||
class Solution: | ||
def maxEnvelopes(self, envelopes: List[List[int]]) -> int: | ||
|
||
def find_first_larger_or_equal(vals, num): | ||
left, right, boundary = 0, len(vals) - 1, - 1 | ||
while left <= right: | ||
m = (left + right) // 2 | ||
if num <= vals[m]: | ||
boundary = m | ||
right = m - 1 | ||
else: | ||
left = m + 1 | ||
return boundary | ||
|
||
envelopes.sort(key = lambda x: (x[0], - x[1])) | ||
dp = [] | ||
for w, h in envelopes: | ||
if not dp or dp[- 1] < h: | ||
dp.append(h) | ||
else: | ||
idx = bisect.bisect_left(dp, h) | ||
idx = find_first_larger_or_equal(dp, h) | ||
dp[idx] = h | ||
|
||
return len(dp) | ||
|
||
# time O(nlogn) | ||
# space O(n) | ||
# using dynamic programming and LIS and patience sort and greedy and binary search and sort | ||
# using dynamic programming and LIS and patience sort and greedy and binary search and sort | ||
''' | ||
1. notice the condition of sorting | ||
''' |