MEDIUM NC#109 Blind #18 Dynamic Programming 1D

300. Longest Increasing Subsequence

šŸ“– Problem

Given an integer array nums, return the length of the longest strictly increasing subsequence.

🧠 Visual Learning Aid

1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases

JS/TS Refreshers

  • •Array methods (`push`, `pop`, `shift`, `slice`)
  • •Object/Map/Set usage patterns
  • •Function parameter and return typing
  • •Midpoint overflow-safe math
  • •Loop invariants
  • •Monotonic condition design

Logical Thinking Concepts

  • •Define invariants before coding
  • •Check edge cases first (`[]`, single element, duplicates)
  • •Estimate time/space before implementation
  • •Apply Binary Search reasoning pattern
  • •Apply Dynamic Programming reasoning pattern

šŸ’” Approach

  • → dp[i] = length of LIS ending at index i
  • → For each i, check all j < i where nums[j] < nums[i]
  • → dp[i] = max(dp[j] + 1) for all valid j
  • → Time: O(n²), Space: O(n)

šŸ› ļø Hints & Pitfalls

Hints

  • •dp[i] = length of LIS ending at index i
  • •For each i, check all j < i where nums[j] < nums[i]
  • •dp[i] = max(dp[j] + 1) for all valid j

Common Pitfalls

  • •Time: O(n²), Space: O(n)

🧪 Test Cases

Hidden tests on submit: 1

Test Case 1
Not run
Input:
lengthOfLIS([10,9,2,5,3,7,101,18]);
Expected:
4
Test Case 2
Not run
Input:
lengthOfLIS([0,1,0,3,2,3]);
Expected:
4
Test Case 3
Not run
Input:
lengthOfLIS([7,7,7,7,7,7]);
Expected:
1

šŸ“ Code Editor

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit