-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
76 lines (60 loc) · 2.85 KB
/
Source.cpp
File metadata and controls
76 lines (60 loc) · 2.85 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
using namespace std;
bool increasingTriplet(const vector<int>& nums);
int main() {
// Given an integer array nums, return true if there exists a triple
// of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k].
// If no such indices exists, return false.
// Case 1: Non-decreasing values
cout << "Test 1: [1, 2, 3, 4, 5]\nExpected: true\nActual: " <<
(increasingTriplet({ 1, 2, 3, 4, 5 }) == 1 ? "true\n\n" : "false\n\n");
// Case 2: Only decreasing values
cout << "Test 2: [5, 4, 3, 2, 1]\nExpected: false\nActual: " <<
(increasingTriplet({ 5, 4, 3, 2, 1 }) == 1 ? "true\n\n" : "false\n\n");
// Case 3: Negative values
cout << "Test 3: [2, 4, -2, -3]\nExpected: false\nActual: " <<
(increasingTriplet({ 2, 4, -2, -3 }) == 1 ? "true\n\n" : "false\n\n");
// Case 4: Mixed values with a valid increasing triplet in the middle
cout << "Test 4: [10, 1, 2, 7, 9, 5]\nExpected: true\nActual: " <<
(increasingTriplet({ 10, 1, 2, 7, 9, 5 }) == 1 ? "true\n\n" : "false\n\n");
// Case 5: Mixed values without any increasing triplet
cout << "Test 5: [8, 6, 5, 4, 3, 2]\nExpected: false\nActual: " <<
(increasingTriplet({ 8, 6, 5, 4, 3, 2 }) == 1 ? "true\n\n" : "false\n\n");
// Case 6: Contains duplicates and a valid increasing triplet
cout << "Test 6: [1, 5, 5, 2, 4, 6]\nExpected: true\nActual: " <<
(increasingTriplet({ 1, 5, 5, 2, 4, 6 }) == 1 ? "true\n\n" : "false\n\n");
// Case 7: Long sequence with one valid increasing triplet near the end
cout << "Test 7: [10, 9, 8, 7, 1, 2, 3]\nExpected: true\nActual: " <<
(increasingTriplet({ 10, 9, 8, 7, 1, 2, 3 }) == 1 ? "true\n\n" : "false\n\n");
// Case 8: Large range of values, including negatives, with multiple valid triplets
cout << "Test 8: [-10, -8, -5, -3, 0, 2, 4, 6]\nExpected: true\nActual: " <<
(increasingTriplet({ -10, -8, -5, -3, 0, 2, 4, 6 }) == 1 ? "true\n\n" : "false\n\n");
// Case 9: Increasing sequence with a dip that still contains a valid triplet
cout << "Test 9: [1, 2, 0, 3]\nExpected: true\nActual: " <<
(increasingTriplet({ 1, 2, 0, 3 }) == 1 ? "true\n\n" : "false\n\n");
// Case 10: All elements are the same, no increasing triplet
cout << "Test 10: [4, 4, 4, 4]\nExpected: false\nActual: " <<
(increasingTriplet({ 4, 4, 4, 4 }) == 1 ? "true\n\n" : "false\n\n");
return 0;
}
bool increasingTriplet(const vector<int>& nums) {
int numsLength = nums.size();
if (numsLength < 3) { return false; }
int lowestValue = nums[0];
int secondLowestValue = INT_MAX;
for (int i = 1; i < numsLength; ++i) {
if (nums[i] > secondLowestValue) {
return true;
}
else if (nums[i] > lowestValue) {
// Ensure that the secondLowestValue always comes after the lowest value
secondLowestValue = nums[i];
}
else {
// Ensure that lowestValue is always the lowest number seen so far
lowestValue = nums[i];
}
}
return false;
}