Skip to content

Commit

Permalink
Merge pull request #73 from tiationg-kho/update
Browse files Browse the repository at this point in the history
update
  • Loading branch information
tiationg-kho authored May 12, 2024
2 parents c3b362e + ebf20ab commit d908490
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ Feel free to raise issues or post in discussions.
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
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
'''
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1 for _ in range(len(nums))]

for i in range(len(nums)):
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)

for end in range(len(nums)):
for start in range(end):
if nums[start] < nums[end]:
dp[end] = max(dp[end], dp[start] + 1)
return max(dp)

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

import bisect
class Solution:
def lengthOfLIS(self, nums: 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

dp = []
for num in nums:
for i, num in enumerate(nums):
if not dp or dp[- 1] < num:
dp.append(num)
else:
idx = bisect.bisect_left(dp, num)
idx = find_first_larger_or_equal(dp, num)
dp[idx] = num
return len(dp)

Expand Down

This file was deleted.

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
'''

0 comments on commit d908490

Please sign in to comment.