forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.kt
28 lines (26 loc) · 880 Bytes
/
Solution.kt
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
package g0301_0400.s0334_increasing_triplet_subsequence
// #Medium #Array #Greedy #Data_Structure_II_Day_5_Array
// #2022_11_15_Time_672_ms_(60.61%)_Space_128.6_MB_(5.23%)
class Solution {
fun increasingTriplet(nums: IntArray): Boolean {
if (nums.size == 1) {
return false
}
var big: Int = nums.lastIndex
var medium: Int? = null
for (i in nums.lastIndex - 1 downTo 0) {
if (nums[i] > nums[big]) {
big = i
continue
} else if ((medium != null && nums[i] > nums[medium] && nums[i] < nums[big]) ||
(medium == null && nums[i] < nums[big])
) {
medium = i
continue
} else if (medium != null && nums[i] < nums[medium]) {
return true
}
}
return false
}
}