Skip to content

Commit 96c6ecb

Browse files
committed
334. Increasing Triplet Subsequence
1 parent 2d0fdc8 commit 96c6ecb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 334. Increasing Triplet Subsequence
2+
# Given an integer array nums, return true if there exists a triple of indices (i, j, k)
3+
# such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
4+
5+
# Example 1:
6+
7+
# Input: nums = [1,2,3,4,5]
8+
# Output: true
9+
# Explanation: Any triplet where i < j < k is valid.
10+
# Example 2:
11+
12+
# Input: nums = [5,4,3,2,1]
13+
# Output: false
14+
# Explanation: No triplet exists.
15+
# Example 3:
16+
17+
# Input: nums = [2,1,5,0,4,6]
18+
# Output: true
19+
# Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
20+
21+
22+
def increasingTriplet(nums):
23+
first = second = float('inf')
24+
for n in nums:
25+
if n <= first:
26+
first = n
27+
elif n <= second:
28+
second = n
29+
else:
30+
return True
31+
return False
32+
33+
nums =[1,2,3,4,5]
34+
print(increasingTriplet(nums))
35+
36+
def increasingTriplet(nums):
37+
n=len(nums)
38+
n1=float('inf')
39+
n2=float('inf')
40+
for i in range(n):
41+
if nums[i]>n2:
42+
return True
43+
if nums[i]<n1 :
44+
n1=nums[i]
45+
elif nums[i]>n1 and nums[i]<n2:
46+
n2=nums[i]
47+
return False
48+
49+
nums =[2,1,5,0,4,6]
50+
print(increasingTriplet(nums))

0 commit comments

Comments
 (0)