-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincreasing_triplet_subsequence.cpp
More file actions
61 lines (57 loc) · 1.38 KB
/
increasing_triplet_subsequence.cpp
File metadata and controls
61 lines (57 loc) · 1.38 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
50
51
52
53
54
55
56
57
58
59
60
61
/*
* =====================================================================================
*
* Filename: increasing_triplet_subsequence.cpp
*
* Description: 334. Increasing Triplet Subsequence.
* Given an unsorted array return whether an increasing subsequence of
* length 3 exists or not in the array.
*
* Version: 1.0
* Created: 07/13/2019 10:01:36 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <vector>
using std::vector;
class Solution
{
public:
bool increasingTriplet(vector<int>& nums)
{
int first = INT_MAX;
int second = INT_MAX;
for (auto n: nums)
{
if (n <= first)
{
first = n;
}
else if (n <= second)
{
second = n;
}
else
{
// Found
return true;
}
}
return false;
}
};
int main(int argc, char* argv[])
{
vector<int> nums = {10, 1, 9, 2, 6, 3};
auto found = Solution().increasingTriplet(nums);
printf("Found? %s\n", (found ? "Yes" : "No"));
return 0;
}