Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 300 · LIS

Longest Increasing Subsequence

LeetCode 300 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Patience sorting: tails stay sorted, so bisect_left finds where each number belongs.

ಮಧ್ಯಮArrayBinary SearchDynamic ProgrammingLongest Increasing Subsequence

ನಾನು Longest Increasing Subsequence (LeetCode 300) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — LIS ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

dp[i] = longest increasing run that ends at nums[i]: look at every earlier smaller number and extend its best run. A faster view keeps 'the smallest possible tail for each length' — each new number either extends the longest list or replaces the first tail that is ≥ it (found with binary search).

Approach

  1. 1

    O(n²): dp[i] = 1 + max(dp[j]) over j < i with nums[j] < nums[i].

  2. 2

    O(n log n): subs holds the smallest tail for each length.

  3. 3

    If the number is bigger than the last tail, append it (a longer sequence exists).

  4. 4

    Otherwise replace the first tail ≥ it — same length, smaller tail, more room to grow.

  5. 5

    len(subs) is the LIS length (subs itself isn't necessarily a real subsequence).

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ

1
O(n²) DPO(n²) time · O(n) space

dp[i] from every smaller earlier number.

2
Binary search (patience)AcceptedO(n log n) time · O(n) space

Maintain minimal tails with bisect_left.

O(n²) DPPythonO(n²) time · O(n) space
class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        #1,3,5,1,7
        [1,1,1,1,1]
        [1,2,3,1,2]
        [1,2,3,1,max(2,3+1)]
        dp=[1]*len(nums)
        n = len(nums)
        for i in range(n):
            for j in range(i):
                if nums[i]>nums[j]:
                    dp[i]=max(dp[i],dp[j]+1)
        return max(dp)
Binary search (patience)PythonO(n log n) time · O(n) space
class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        #1,3,5,1,7
        #[1,3,5]
        subs = [nums[0]]
        n = len(nums)
        #if greater than last number - insert
        #search insert position, replace with current number
        for i in range(n):
            if nums[i]>subs[-1]:
                subs.append(nums[i])
            else:
                k = bisect_left(subs,nums[i])
                subs[k]=nums[i]
        return len(subs)
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Longest Increasing Subsequence — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Longest Increasing Subsequence ಹಿಂದಿನ intuition ಏನು?

dp[i] = longest increasing run that ends at nums[i]: look at every earlier smaller number and extend its best run. A faster view keeps 'the smallest possible tail for each length' — each new number either extends the longest list or replaces the first tail that is ≥ it (found with binary search).

Longest Increasing Subsequence (LeetCode 300) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. O(n²): dp[i] = 1 + max(dp[j]) over j < i with nums[j] < nums[i]. 2. O(n log n): subs holds the smallest tail for each length. 3. If the number is bigger than the last tail, append it (a longer sequence exists). 4. Otherwise replace the first tail ≥ it — same length, smaller tail, more room to grow. 5. len(subs) is the LIS length (subs itself isn't necessarily a real subsequence).

Longest Increasing Subsequence ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(n log n). Space: O(n). (Accepted approach: Binary search (patience).)

Longest Increasing Subsequence ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

Use bisect_left for strictly increasing; bisect_right allows equal values. subs is not the actual subsequence — only its length is meaningful.

Longest Increasing Subsequence ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?

O(n²) DP (O(n²) time · O(n) space): dp[i] from every smaller earlier number. Binary search (patience) (O(n log n) time · O(n) space): Maintain minimal tails with bisect_left.