LeetCode 334 increments ternary sequence
The longest ascending sequence in question, provided the sequence length is n, the length of the longest ascending sequence m
Complexity time before optimization: \ (O (n-m *) \)
Optimized time complexity: \ (O (n-\ log {m}) \)
Space complexity: \ (O (m) \)
When m=3
, the m
constant
In this case, the time complexity: \ (O (n-) \) , the spatial complexity: \ (O (. 1) \)
C Lv
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size() < 3) return false;
int dp[3];
memset(dp, 0x7F, sizeof dp);
for (int num : nums) {
for (int i = 0; i < 3; ++i) {
if (dp[i] < num) continue;
if (i == 2) return true;
dp[i] = num;
break;
}
}
return false;
}
};