|
1 |
| - |
2 |
| -/****************************************************************************** |
3 |
| - * @details |
4 |
| -Given an integer array nums, return the length of the longest strictly |
5 |
| -increasing subsequence. |
6 |
| -
|
7 |
| -The longest increasing subsequence is described as a subsequence of an array |
8 |
| -where: All elements of the subsequence are in increasing order. This subsequence |
9 |
| -itself is of the longest length possible. |
10 |
| -
|
11 |
| -For solving this problem we have Three Approaches :- |
12 |
| -
|
13 |
| -Approach 1 :- Using Brute Force |
14 |
| -The first approach that came to your mind is the Brute Force approach where we |
15 |
| -generate all subsequences and then manually filter the subsequences whose |
16 |
| -elements come in increasing order and then return the longest such subsequence. |
17 |
| -Time Complexity :- O(2^n) |
18 |
| -It's time complexity is exponential. Therefore we will try some other |
19 |
| -approaches. |
20 |
| -
|
21 |
| -Approach 2 :- Using Dynamic Programming |
22 |
| -To generate all subsequences we will use recursion and in the recursive logic we |
23 |
| -will figure out a way to solve this problem. Recursive Logic to solve this |
24 |
| -problem:- |
25 |
| -1. We only consider the element in the subsequence if the element is grater then |
26 |
| -the last element present in the subsequence |
27 |
| -2. When we consider the element we will increase the length of subsequence by 1 |
28 |
| -Time Complexity: O(N*N) |
29 |
| -Space Complexity: O(N*N) + O(N) |
30 |
| -
|
31 |
| -This approach is better then the previous Brute Force approach so, we can |
32 |
| -consider this approach. |
33 |
| -
|
34 |
| -But when the Constraints for the problem is very larger then this approach fails |
35 |
| -
|
36 |
| -Approach 3 :- Using Binary Search |
37 |
| -Other approaches use additional space to create a new subsequence Array. |
38 |
| -Instead, this solution uses the existing nums Array to build the subsequence |
39 |
| -array. We can do this because the length of the subsequence array will never be |
40 |
| -longer than the current index. |
41 |
| -
|
42 |
| -Time complexity: O(n∗log(n)) |
43 |
| -Space complexity: O(1) |
44 |
| -
|
45 |
| -This approach consider Most optimal Approach for solving this problem |
46 |
| -
|
47 |
| - *******************************************************************************/ |
48 |
| - |
49 |
| -#include <cassert> /// for std::assert |
50 |
| -#include <iostream> /// for IO operations |
51 |
| -#include <vector> /// for std::vector |
| 1 | +#include <cassert> /// for std::assert |
| 2 | +#include <iostream> /// for IO operations |
| 3 | +#include <vector> /// for std::vector |
| 4 | +#include <algorithm> /// for std::lower_bound |
52 | 5 |
|
53 | 6 | /**
|
54 |
| - * @brief Function to find the length of Longest Increasing Subsequence (LIS) |
| 7 | + * @brief Function to find the length of the Longest Increasing Subsequence (LIS) |
55 | 8 | * using Binary Search
|
56 |
| - * @param nums Input integer array |
57 |
| - * @return Length of the longest increasing subsequence |
| 9 | + * @param nums The input vector of integers |
| 10 | + * @return The length of the longest increasing subsequence |
58 | 11 | */
|
59 |
| -int Longest_Increasing_Subsequence_using_binary_search(std::vector<int>& nums){ |
60 |
| - |
61 |
| - if(nums.size() == 0) return 0; |
| 12 | +int longest_increasing_subsequence_using_binary_search(std::vector<int>& nums) { |
| 13 | + if (nums.empty()) return 0; |
62 | 14 |
|
63 | 15 | std::vector<int> ans;
|
64 | 16 | ans.push_back(nums[0]);
|
65 |
| - for(int i=1;i<nums.size();i++){ |
66 |
| - if(nums[i] > ans.back()){ |
| 17 | + for (int i = 1; i < nums.size(); i++) { |
| 18 | + if (nums[i] > ans.back()) { |
67 | 19 | ans.push_back(nums[i]);
|
68 |
| - } |
69 |
| - else{ |
70 |
| - int idx = lower_bound(ans.begin(),ans.end(),nums[i]) -ans.begin(); |
| 20 | + } else { |
| 21 | + int idx = std::lower_bound(ans.begin(), ans.end(), nums[i]) - ans.begin(); |
71 | 22 | ans[idx] = nums[i];
|
72 | 23 | }
|
73 | 24 | }
|
74 | 25 | return ans.size();
|
75 | 26 | }
|
76 | 27 |
|
77 | 28 | /**
|
78 |
| - * @brief test implementations |
| 29 | + * @brief Test cases for Longest Increasing Subsequence function |
79 | 30 | * @returns void
|
80 | 31 | */
|
81 | 32 | static void tests() {
|
82 | 33 | std::vector<int> arr = {10, 9, 2, 5, 3, 7, 101, 18};
|
83 |
| - assert(Longest_Increasing_Subsequence_using_binary_search(arr) == 4); |
| 34 | + assert(longest_increasing_subsequence_using_binary_search(arr) == 4); |
84 | 35 |
|
85 | 36 | std::vector<int> arr2 = {0, 1, 0, 3, 2, 3};
|
86 |
| - assert(Longest_Increasing_Subsequence_using_binary_search(arr2) == 4); |
| 37 | + assert(longest_increasing_subsequence_using_binary_search(arr2) == 4); |
87 | 38 |
|
88 | 39 | std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
|
89 |
| - assert(Longest_Increasing_Subsequence_using_binary_search(arr3) == 1); |
| 40 | + assert(longest_increasing_subsequence_using_binary_search(arr3) == 1); |
90 | 41 |
|
91 | 42 | std::cout << "All tests have successfully passed!\n";
|
92 | 43 | }
|
93 | 44 |
|
94 | 45 | /**
|
95 |
| - * @brief Main function |
| 46 | + * @brief Main function to run tests |
96 | 47 | * @returns 0 on exit
|
97 | 48 | */
|
98 | 49 | int main() {
|
|
0 commit comments