Skip to content

Commit ea405e3

Browse files
committed
[LeetCode Sync] Runtime - 13 ms (79.11%), Memory - 37.4 MB (89.98%)
1 parent 0c87636 commit ea405e3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> nums = [1,2,3,4,5]
8+
<strong>Output:</strong> true
9+
<strong>Explanation:</strong> Any triplet where i &lt; j &lt; k is valid.
10+
</pre>
11+
12+
<p><strong class="example">Example 2:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [5,4,3,2,1]
16+
<strong>Output:</strong> false
17+
<strong>Explanation:</strong> No triplet exists.
18+
</pre>
19+
20+
<p><strong class="example">Example 3:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [2,1,5,0,4,6]
24+
<strong>Output:</strong> true
25+
<strong>Explanation:</strong> One of the valid triplet is (3, 4, 5), because nums[3] == 0 &lt; nums[4] == 4 &lt; nums[5] == 6.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>5</sup></code></li>
33+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
34+
</ul>
35+
36+
<p>&nbsp;</p>
37+
<strong>Follow up:</strong> Could you implement a solution that runs in <code>O(n)</code> time complexity and <code>O(1)</code> space complexity?
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def increasingTriplet(self, nums: List[int]) -> bool:
3+
first, second = float('inf'), float('inf')
4+
for num in nums:
5+
if num <= first:
6+
first = num
7+
elif num <= second:
8+
second = num
9+
else:
10+
return True
11+
return False

0 commit comments

Comments
 (0)