Skip to content

Commit bebb8a2

Browse files
committed
[Array] Add a solution to Increasing Triplet Subsequence
1 parent a9552ed commit bebb8a2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/increasing-triplet-subsequence/
3+
* Primary idea: Two pointers. One is to store the smallest value,
4+
* the other is to store the second smallest value.
5+
* Return true once find a value greater than both.
6+
* Time Complexity: O(n), Space Complexity: O(1)
7+
*/
8+
9+
class IncreasingTripletSubsequence {
10+
func increasingTriplet(_ nums: [Int]) -> Bool {
11+
var smallest = Int.max, smaller = Int.max
12+
13+
for num in nums {
14+
if smallest >= num {
15+
smallest = num
16+
} else if smaller >= num {
17+
smaller = num
18+
} else {
19+
return true
20+
}
21+
}
22+
23+
return false
24+
}
25+
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 285 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 286 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -64,6 +64,7 @@
6464
[3Sum](https://leetcode.com/problems/3sum/)| [Swift](./Array/ThreeSum.swift)| Medium| O(n^2)| O(nC3)|
6565
[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Swift](./Array/ThreeSumClosest.swift)| Medium| O(n^2)| O(nC3)|
6666
[4Sum](https://leetcode.com/problems/4sum/)| [Swift](./Array/FourSum.swift)| Medium| O(n^3)| O(nC4)|
67+
[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)| [Swift](./Array/IncreasingTripletSubsequence.swift)| Medium| O(n)| O(1)|
6768
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| O(n)| O(n)|
6869
[Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/)| [Swift](./Array/NumMatrix.swift)| Medium| O(mn)| O(mn)|
6970
[Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [Swift](./Array/MissingRanges.swift)| Medium| O(n)| O(1)|
@@ -546,7 +547,7 @@
546547
| [Swift](./Tree/HouseRobberIII.swift) | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | Medium
547548
| | 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | Hard
548549
| | 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | Hard
549-
| | 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium
550+
| [Swift](./Tree/IncreasingTripletSubsequence.swift) | 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium
550551
| | 333 | [Largest BST Subtree](https://leetcode.com/problems/largest-bst-subtree/) ♥ | Medium
551552
| | 332 | [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | Medium
552553
| | 331 | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | Medium

0 commit comments

Comments
 (0)