|
| 1 | +--- |
| 2 | +id: longest-increasing-subsequence |
| 3 | +title: Longest Increasing Subsequence (LeetCode) |
| 4 | +sidebar_label: 0300-LongestIncreasingSubsequence |
| 5 | +--- |
| 6 | + |
| 7 | +## Problem Description |
| 8 | + |
| 9 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 10 | +| :---------------- | :------------ | :--------------- | |
| 11 | +| [Merge Two Sorted Lists](https://leetcode.com/problems/longest-increasing-subsequence/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/longest-increasing-subsequence/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) | |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +Given an integer array nums, return the length of the longest strictly increasing subsequence. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +#### Example 1: |
| 20 | + |
| 21 | +- **Input:** nums = [10,9,2,5,3,7,101,18] |
| 22 | +- **Output:** 4 |
| 23 | +- **Explanation:** The longest increasing subsequence is [2,3,7,101], therefore the length is 4. |
| 24 | + |
| 25 | +#### Example 2: |
| 26 | + |
| 27 | +- **Input:** nums = [0,1,0,3,2,3] |
| 28 | +- **Output:** 4 |
| 29 | + |
| 30 | +#### Example 3: |
| 31 | + |
| 32 | +- **Input:** nums = [7,7,7,7,7,7,7] |
| 33 | +- **Output:** 1 |
| 34 | + |
| 35 | +### Constraints: |
| 36 | + |
| 37 | +- $1 <= nums.length <= 2500$ |
| 38 | +- $-10^4 <= nums[i] <= 10^4$ |
| 39 | + |
| 40 | +## Approach |
| 41 | + |
| 42 | +We can solve this problem using dynamic programming or binary search. Here, we will discuss the binary search approach which achieves $O(n log(n))$ time complexity. |
| 43 | + |
| 44 | +1. Initialize an empty list tails to keep track of the smallest ending element for all increasing subsequences. |
| 45 | +2. Iterate through each element num in the input nums. |
| 46 | +3. If num is greater than the last element in tails, append num to tails. |
| 47 | +4. Otherwise, perform a binary search on tails to find the smallest element greater than or equal to num and update that element with num. |
| 48 | +5. After iterating through all elements in nums, return the length of tails. |
| 49 | + |
| 50 | +## Solution Code |
| 51 | + |
| 52 | +#### Python |
| 53 | + |
| 54 | +```py |
| 55 | +class Solution: |
| 56 | + def lengthOfLIS(self, nums): |
| 57 | + tails = [] |
| 58 | + for num in nums: |
| 59 | + left, right = 0, len(tails) - 1 |
| 60 | + while left <= right: |
| 61 | + mid = (left + right) // 2 |
| 62 | + if tails[mid] < num: |
| 63 | + left = mid + 1 |
| 64 | + else: |
| 65 | + right = mid - 1 |
| 66 | + if left < len(tails): |
| 67 | + tails[left] = num |
| 68 | + else: |
| 69 | + tails.append(num) |
| 70 | + return len(tails) |
| 71 | +``` |
| 72 | + |
| 73 | +#### C++ |
| 74 | + |
| 75 | +```cpp |
| 76 | +class Solution { |
| 77 | +public: |
| 78 | + int lengthOfLIS(vector<int>& nums) { |
| 79 | + vector<int> tails; |
| 80 | + for (int num : nums) { |
| 81 | + auto it = lower_bound(tails.begin(), tails.end(), num); |
| 82 | + if (it == tails.end()) |
| 83 | + tails.push_back(num); |
| 84 | + else |
| 85 | + *it = num; |
| 86 | + } |
| 87 | + return tails.size(); |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +
|
| 92 | +#### Java |
| 93 | +
|
| 94 | +```java |
| 95 | +class Solution { |
| 96 | + public int lengthOfLIS(int[] nums) { |
| 97 | + List<Integer> tails = new ArrayList<>(); |
| 98 | + for (int num : nums) { |
| 99 | + int left = 0, right = tails.size() - 1; |
| 100 | + while (left <= right) { |
| 101 | + int mid = left + (right - left) / 2; |
| 102 | + if (tails.get(mid) < num) |
| 103 | + left = mid + 1; |
| 104 | + else |
| 105 | + right = mid - 1; |
| 106 | + } |
| 107 | + if (left < tails.size()) |
| 108 | + tails.set(left, num); |
| 109 | + else |
| 110 | + tails.add(num); |
| 111 | + } |
| 112 | + return tails.size(); |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Conclusion |
| 118 | + |
| 119 | +The "Longest Increasing Subsequence" problem can be efficiently solved using the binary search approach, achieving $O(n log(n))$ time complexity. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem. |
0 commit comments