-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path334.IncreasingTripletSubsequence.cpp
More file actions
49 lines (46 loc) · 1.14 KB
/
334.IncreasingTripletSubsequence.cpp
File metadata and controls
49 lines (46 loc) · 1.14 KB
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
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size()<3) {
return false;
}
vector<int> temp(3, 0);
int length = 0;
temp[0] = nums[0];
for (int i=1; i<nums.size(); i++) {
if(nums[i]>temp[length]) {
temp[++length] = nums[i];
if (length+1 == 3) {
return true;
}
continue;
}
int maxSmallerIndex = length;
for (;maxSmallerIndex >= 0; maxSmallerIndex--){
if (temp[maxSmallerIndex] < nums[i]){
break;
}
}
temp[maxSmallerIndex+1] = nums[i];
}
return false;
}
};
/*
* 最优解:
* https://leetcode.com/problems/increasing-triplet-subsequence/discuss/594995/C%2B%2Bor95-Runtime-(4ms)-or-100-Memoryor-O(n)-time-and-O(1)-space
*/
class Solution2 {
public:
bool increasingTriplet(vector<int>& nums) {
int n = nums.size();
if(n<3) return false;
int least=INT_MAX, second_least=INT_MAX;
for(int i=0; i<n; i++) {
if(nums[i]>second_least) return true;
if(nums[i]>least) second_least=nums[i];
if(nums[i]<least) least=nums[i];
}
return false;
}
};