-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
95 lines (85 loc) · 2.85 KB
/
Solution.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
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstddef>
#include <vector>
using namespace std;
using i64 = long long;
constexpr int maxElems = 100001;
const int maxRoot = std::sqrt(maxElems);
class Solution {
private:
int getStreak(std::bitset<maxElems>& set,
std::array<int, maxElems>& lss,
int curr) {
if (!set.test(curr)) {
return 0;
}
if (curr > maxRoot) {
return 1;
}
if (lss[curr] > 0) {
return lss[curr];
}
// No need to check for overflow; base case checks it already.
lss[curr] = 1 + getStreak(set, lss, curr * curr);
return lss[curr];
}
public:
int longestSquareStreak(vector<int>& nums) {
// A subsequence is called a Streak if
// - the length of the subsequence is at least 2,
// - after sorting the subsequence, each element is the square of the
// previous element.
//
// I guess this is a more constrained Longest Increasing Subsequence
// problem? Except the order does not matter. Therefore, find all unique
// squares, like two-sum?
// I.e., for {4, 3, 6, 16, 8, 2},
//
// Use the squared number as the key:
// 16: 4
// 9: 3
// 36: 6
// 16 -> saw 4 earlier, 256: 16
// 64 -> 8
// 4 -> 2 (how can we determine if we found 4? explore both directions?)
// What if we have multiple possible sequences? i.e., power of 2s, power of
// 3s?
//
// What if we just sort the array, using an ordered Set?
// Visualizes into something like a linkedlist:
// e.g., for {4, 3, 6, 16, 8, 2}
// => {2, 3, 4, 6, 8, 16}
// ^
// ^
// ^
// ^ // length 4
// ^ // 3 * 3 = 9 not in array, terminate
// ^ // Ah, repeated work... Cant simply erase?
// What about a DP array/memoization?
// i.e., store the length of the subsequence
// Ah....... Just update the value of the
// squared number, since we will traverse
// it later due to the sorted order...
// In fact, given the constraints, we can solve in O(n) time?
// std::sort(nums.begin(), nums.end());
// Element Set.
std::bitset<maxElems> set{};
for (int num : nums) {
set.set(num);
}
// longest square streak. Maintains the length of the square subsequence
// ending at nums[i].
std::array<int, maxElems> lss{};
int maxStreak = 1;
for (int i = 2; i < maxElems; ++i) {
if (!set.test(i)) {
continue;
}
maxStreak = std::max(maxStreak, getStreak(set, lss, i));
}
return maxStreak > 1 ? maxStreak : -1;
}
};