LeetCode 300.Longest Increasing Subsequence

preview_player
Показать описание


Thanks in advance!

LeetCode 300. Longest Increasing Subsequence
1. Dynamic Programming
2. Binary Search
Рекомендации по теме
Комментарии
Автор

Thank you. I couldn't solve the question because I couldn't understand what is meant by subsequence. I thought the example gave the one solution but you pointed out that in fact there are two possible solutions: [2, 5, 7, 101] and [2, 3, 7, 101]. This helped me understand and solve it. Thank you again.

thesuperiorman
Автор

my python conversion with bisect for binary search

class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
import bisect
dp = [0] * len(nums)
length = 0
for num in nums:
idx = bisect.bisect_left(dp, num, 0, length)
dp[idx] = num
if idx == length:
length += 1
return length

aunnfriends
Автор

Thank you for this explanation!
btw Arrays.binarySearch(...) returns the index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1), so I guess in the video it should be "pos = -(index) - 1"

joeybing
Автор

Thanks for the explanation. Question, would the runtime be O(n * log n)? since we do binary search n times and each search takes log(n) and everything else constant?

dnavas
Автор

Very clear explanation. Thank you so much. I cannot express how much you motivate me to continue practicing Leetcode.

domng