forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest_Increasing_Subsequence.cpp
121 lines (86 loc) · 3.04 KB
/
Longest_Increasing_Subsequence.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
#include <iostream>
#include <vector>
using namespace std;
// practice --> https://leetcode.com/problems/longest-increasing-subsequence/
// This approach is using prev_index
// Please revise in this order
// Recursion -> Memoization -> Tabulation -> Space Optimization
//
//
// Approaches ->
// Recursive Approach ------------------------------------------
int rec(vector<int> &arr, int &n, int index, int prev_index)
{
if (index == n)
return 0;
int pick = 0, non_pick;
if (prev_index == -1 or arr[index] > arr[prev_index])
pick = 1 + rec(arr, n, index + 1, index);
non_pick = 0 + rec(arr, n, index + 1, prev_index);
return max(pick, non_pick);
}
int lengthOfLIS_rec(vector<int> &nums)
{
int n = nums.size();
return rec(nums, n, 0, -1);
}
// Memoized Approach ------------------------------------------
int mem(vector<int> &arr, int &n, int index, int prev_index, vector<vector<int>> &dp)
{
if (index == n)
return 0;
if (dp[index][prev_index + 1] != -1)
return dp[index][prev_index + 1];
int pick = 0, non_pick;
if (prev_index == -1 or arr[index] > arr[prev_index])
pick = 1 + mem(arr, n, index + 1, index, dp);
non_pick = 0 + mem(arr, n, index + 1, prev_index, dp);
return dp[index][prev_index + 1] = max(pick, non_pick);
}
int lengthOfLIS_mem(vector<int> &nums)
{
int n = nums.size();
vector<vector<int>> dp(n, vector<int>(n + 1, -1));
// using dp[n][n+1], because -1 state of prev_index can't be accessed in array, that's why we shifted the prev_index, like we'll say -1 to 0, 0 to 1, 1 to 2 ......... n-1 to n, n to n+1
// that's why
return mem(nums, n, 0, -1, dp);
}
// Tabulated Approach ------------------------------------------
int lengthOfLIS_tab(vector<int> &arr)
{
int n = arr.size();
vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
// but actually, we'll use kinda like reverse triangular dp matrix
for (int index = n - 1; index >= 0; index--)
{
for (int prev_index = index - 1; prev_index >= -1; prev_index--)
{
int pick = 0, non_pick;
if (prev_index == -1 or arr[index] > arr[prev_index])
pick = 1 + dp[index + 1][index + 1];
non_pick = 0 + dp[index + 1][prev_index + 1];
dp[index][prev_index + 1] = max(pick, non_pick);
}
}
return dp[0][-1 + 1]; // dp[0]
}
// Space Optimized Approach ------------------------------------------
int lengthOfLIS_SpaceOptimized(vector<int> &arr)
{
int n = arr.size();
vector<int> dp(n + 1, 0);
for (int index = n - 1; index >= 0; index--)
{
vector<int> temp(index + 1, 0);
for (int prev_index = index - 1; prev_index >= -1; prev_index--)
{
int pick = 0, non_pick;
if (prev_index == -1 or arr[index] > arr[prev_index])
pick = 1 + dp[index + 1];
non_pick = 0 + dp[prev_index + 1];
temp[prev_index + 1] = max(pick, non_pick);
}
dp = temp;
}
return dp[-1 + 1]; // dp[0]
}