|
10 | 10 | // difficulty: 2
|
11 | 11 | // clang-format on
|
12 | 12 |
|
13 |
| -#include <climits> |
| 13 | +#include <unordered_map> |
| 14 | +#include <unordered_set> |
14 | 15 | #include <vector>
|
15 | 16 |
|
16 | 17 | using namespace std;
|
17 | 18 |
|
18 |
| -template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { |
19 |
| - os << "["; |
20 |
| - for (size_t i = 0, N = v.size(); i < N; ++i) { |
21 |
| - os << v[i]; |
22 |
| - if (i < N - 1) { |
23 |
| - os << ", "; |
24 |
| - } |
25 |
| - } |
26 |
| - os << "]"; |
27 |
| - return os; |
28 |
| -} |
29 |
| - |
30 | 19 | class Solution {
|
31 | 20 | public:
|
32 | 21 | int longestSubsequence(vector<int> &arr, int difference) {
|
33 | 22 | /*
|
34 |
| - Let's say that arr is length 1, e.g. A = [1], and difference is -42. |
35 |
| -
|
36 |
| - Would the longest subsequence be 1 as a base case? What if A had length |
37 |
| - 0? Would the longest subsequence be 0 as a base case? |
| 23 | + If we proceed from the 0th element in arr to the end |
| 24 | + we can check if arr[i] - difference already exists in O(1) time |
| 25 | + by using an unordered_map<int,int>, where the key is the |
| 26 | + value from arr[j], 0 <= j < i, and the value is the length |
| 27 | + of the subsequence finishing with arr[j]. |
38 | 28 |
|
39 |
| - Say A = [1,2,3,4] and difference == 1 |
| 29 | + If the key exists, then simply add a one to it for a new entry, arr[i]. |
| 30 | + if the key does not exist, then simply set it to one (the same as adding one |
| 31 | + to a value not already in the map with the [] operator). |
40 | 32 |
|
41 |
| - i j A[i] A[j] d LS(i) LS(j) |
42 |
| - --------------------------------- |
43 |
| - 0 - 1 - - 1 - |
44 |
| - 1 0 2 1 1 2 1 |
45 |
| - 2 0 3 1 2 1 1 |
46 |
| - 2 1 3 2 1 3 2 |
47 |
| - 3 0 4 1 3 1 1 |
48 |
| - 3 1 4 2 2 1 2 |
49 |
| - 3 2 4 3 1 4 3 |
| 33 | + Keep track of the maximum subsequence length with a separate variable. |
50 | 34 |
|
51 |
| - LS(i) = 1 + max(LS(j)), 0 < j < i and d = A[i] - A[j] == difference |
52 |
| - = 1 if no such j exists |
53 |
| -
|
54 |
| - There is a problem though, in that we still need to remember the max |
55 |
| - of LS(.), because the max will not necessarily be at the last position. |
56 |
| - To solve that, maintain a max_len variable initialized to INT_MIN. |
| 35 | + O(N) |
57 | 36 | */
|
58 |
| - int N = arr.size(); |
59 |
| - vector<int> dp(N, 1); |
60 |
| - int max_len = INT_MIN; |
61 | 37 |
|
62 |
| - for (int i = 1; i < N; ++i) { |
63 |
| - for (int j = 0; j < i; ++j) { |
64 |
| - if (arr[i] - arr[j] == difference) { |
65 |
| - dp[i] = max(dp[i], 1 + dp[j]); |
66 |
| - } |
67 |
| - } |
68 |
| - max_len = max(max_len, dp[i]); |
69 |
| - } |
| 38 | + int max_len = 0; |
| 39 | + unordered_map<int, int> dp; |
70 | 40 |
|
71 |
| - // cout << "arr: " << arr << ", difference: " << difference << endl; |
72 |
| - // cout << "dp: " << dp << endl; |
| 41 | + for (auto &a : arr) { |
| 42 | + dp[a] = dp[a - difference] + 1; |
| 43 | + max_len = max(max_len, dp[a]); |
| 44 | + } |
73 | 45 |
|
74 | 46 | return max_len;
|
75 | 47 | }
|
|
0 commit comments