-
Notifications
You must be signed in to change notification settings - Fork 0
/
44_LongestIncreasingSubSequence.cpp
246 lines (227 loc) · 6.41 KB
/
44_LongestIncreasingSubSequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// https://leetcode.com/problems/longest-increasing-subsequence/
// Given an integer array nums, return the length of the longest
// strictly increasing subsequence.
// A subsequence is a sequence that can be derived from an array by
// deleting some or no elements without changing the order of the
// remaining elements. For example, [3,6,2,7] is a subsequence of
// the array [0,3,1,6,2,2,7].
#include <bits/stdc++.h>
using namespace std;
// Discussion Solution
// https://leetcode.com/problems/longest-increasing-subsequence/discuss/1326308/C++Python-DP-Binary-Search-BIT-Solutions-Picture-explain-O(NlogN)
/*
class Solution8 { // 256 ms, faster than 42.84%
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
for (int i = 0; i < n; ++i)
for (int j = 0; j < i; ++j)
if (nums[i] > nums[j] && dp[i] < dp[j] + 1)
dp[i] = dp[j] + 1;
return *max_element(dp.begin(), dp.end());
}
};
class Solution7 { // 8 ms, faster than 91.61%
public:
int lengthOfLIS(vector<int>& nums) {
vector<int> sub;
for (int x : nums) {
if (sub.empty() || sub[sub.size() - 1] < x) {
sub.push_back(x);
} else {
auto it = lower_bound(sub.begin(), sub.end(), x); // Find the index of the smallest number >= x
*it = x; // Replace that number with x
}
}
return sub.size();
}
};
class Solution6 {
public:
vector<int> pathOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> sub, subIndex; // Store index instead of value for tracing path purpose
vector<int> path(n, -1); // path[i] point to the index of previous number in LIS
for (int i = 0; i < n; ++i) {
if (sub.empty() || sub[sub.size() - 1] < nums[i]) {
path[i] = sub.empty() ? -1 : subIndex[sub.size() - 1];
sub.push_back(nums[i]);
subIndex.push_back(i);
} else {
int idx = lower_bound(sub.begin(), sub.end(), nums[i]) - sub.begin();
path[i] = idx == 0 ? -1 : subIndex[idx - 1];
sub[idx] = nums[i];
subIndex[idx] = i;
}
}
vector<int> ans;
int t = subIndex[subIndex.size() - 1];
while (t != -1) {
ans.push_back(nums[t]);
t = path[t];
}
reverse(ans.begin(), ans.end());
return ans;
}
};
class MaxBIT { // One-based indexing
vector<int> bit;
public:
MaxBIT(int size) {
bit.resize(size + 1);
}
int get(int idx) {
int ans = 0;
for (; idx > 0; idx -= idx & -idx)
ans = max(ans, bit[idx]);
return ans;
}
void update(int idx, int val) {
for (; idx < bit.size(); idx += idx & -idx)
bit[idx] = max(bit[idx], val);
}
};
class Solution5 { // 16 ms, faster than 72.16%
public:
int lengthOfLIS(vector<int>& nums) {
int BASE = 10001;
MaxBIT bit(20001);
for (int x : nums) {
int subLongest = bit.get(BASE + x - 1);
bit.update(BASE + x, subLongest + 1);
}
return bit.get(20001);
}
};
*/
// My Solution:
class Solution4
{
// Tabulation: SpaceOptimisation
public:
int lengthOfLIS(vector<int> &nums)
{
int n = nums.size();
vector<int> curr(n+1, 0), temp(n+1, 0);
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int pick = -1e9;
if (j == 0 || nums[j-1] > nums[i-1]) {pick = 1 + curr[i];}
int noPick = curr[j];
temp[j] = max(pick, noPick);
}
curr = temp;
}
return curr[0];
}
};
class Solution3
{
// Tabulation
public:
int lengthOfLIS(vector<int> &nums)
{
int n = nums.size();
vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int pick = -1e9;
if (j == 0 || nums[j-1] > nums[i-1]) {pick = 1 + dp[i-1][i];}
int noPick = dp[i-1][j];
dp[i][j] = max(pick, noPick);
}
}
return dp[n][0];
}
};
class Solution2
{
// Wrong Solution : Runtime error
public:
int lengthOfLIS(vector<int> &nums)
{
int n = nums.size();
vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
for (int i = 1; i < n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int pick = -1e9;
if (j == 0 || nums[j] > nums[i-1]) {pick = 1 + dp[i-1][i];}
int noPick = dp[i-1][j];
dp[i][j+1] = max(pick, noPick);
}
}
return dp[n][0];
}
};
class Solution2
{
// Recursion: Memoization
public:
int lengthOfLIS(vector<int> &nums)
{
int n = nums.size();
vector<vector<int>> dp(n, vector<int>(n+1, -1));
return solve(nums, dp, n-1);
}
int solve(vector<int> &nums, vector<vector<int>> &dp, int i, int j = -1)
{
if (i == -1) return 0;
if (dp[i][j+1] != -1) return dp[i][j+1];
int pick = -1e9;
if (j == -1 || nums[j] > nums[i]) {pick = 1 + solve(nums, dp, i-1, i);}
int noPick = solve(nums, dp, i-1, j);
return dp[i][j+1] = max(pick, noPick);
}
};
class Solution1
{
// BruteForce: Recursion
public:
int lengthOfLIS(vector<int> &nums)
{
int n = nums.size();
return solve(nums, n-1);
}
int solve(vector<int> &nums, int i, int j = -1)
{
if (i == -1) return 0;
int pick = -1e9;
if (j == -1 || nums[j] > nums[i]) {pick = 1 + solve(nums, i-1, i);}
int noPick = solve(nums, i-1, j);
return max(pick, noPick);
}
};
class Solution
{
// BruteForce: Recursion
public:
int lengthOfLIS(vector<int> &nums)
{
return solve(nums);
}
int solve(vector<int> &nums, int prev = INT_MIN, int i = 0)
{
if (i == nums.size())
return 0;
int pick = -1e9;
if (nums[i] > prev)
{
pick = 1 + solve(nums, nums[i], i + 1);
}
int noPick = solve(nums, prev, i + 1);
return max(pick, noPick);
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}