Skip to content

Commit 7ea4249

Browse files
authored
Merge pull request #3759 from ImmidiSivani/Leetcode-334
solution added to 334
2 parents 6585662 + 6fcb5ab commit 7ea4249

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
id: increasing-triplet-subsequence
3+
title: Increasing Triplet Subsequence
4+
sidebar_label: 334-Increasing Triplet Subsequence
5+
tags:
6+
- Arrays
7+
- LeetCode
8+
- Java
9+
- Python
10+
- C++
11+
description: "This is a solution to the Increasing Triplet Subsequence problem on LeetCode."
12+
sidebar_position: 4
13+
---
14+
15+
## Problem Description
16+
17+
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 exist, return `false`.
18+
19+
### Examples
20+
21+
**Example 1:**
22+
23+
```
24+
Input: nums = [1, 2, 3, 4, 5]
25+
Output: true
26+
Explanation: Any triplet where i < j < k is valid.
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: nums = [5, 4, 3, 2, 1]
33+
Output: false
34+
Explanation: No triplet exists.
35+
```
36+
37+
**Example 3:**
38+
39+
```
40+
Input: nums = [2, 1, 5, 0, 4, 6]
41+
Output: true
42+
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
43+
```
44+
45+
### Constraints
46+
47+
- `1 <= nums.length <= 5 * 10^5`
48+
- `-2^31 <= nums[i] <= 2^31 - 1`
49+
50+
### Follow-up
51+
52+
Could you implement a solution that runs in `O(n)` time complexity and `O(1)` space complexity?
53+
54+
---
55+
56+
## Solution for Increasing Triplet Subsequence Problem
57+
58+
### Approach
59+
60+
To solve this problem, we can maintain two variables first and second to represent the smallest and second smallest numbers encountered so far. The algorithm iterates through the array and updates these two variables. If we find a number that is larger than second, we have found an increasing triplet.
61+
62+
#### Code in Different Languages
63+
64+
<Tabs>
65+
<TabItem value="C++" label="C++" default>
66+
<SolutionAuthor name="@ImmidiSivani"/>
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
bool increasingTriplet(vector<int>& nums) {
72+
int first = INT_MAX, second = INT_MAX;
73+
for (int num : nums) {
74+
if (num <= first) {
75+
first = num;
76+
} else if (num <= second) {
77+
second = num;
78+
} else {
79+
return true;
80+
}
81+
}
82+
return false;
83+
}
84+
};
85+
```
86+
87+
</TabItem>
88+
<TabItem value="Java" label="Java">
89+
<SolutionAuthor name="@ImmidiSivani"/>
90+
91+
```java
92+
class Solution {
93+
public boolean increasingTriplet(int[] nums) {
94+
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
95+
for (int num : nums) {
96+
if (num <= first) {
97+
first = num;
98+
} else if (num <= second) {
99+
second = num;
100+
} else {
101+
return true;
102+
}
103+
}
104+
return false;
105+
}
106+
}
107+
```
108+
109+
</TabItem>
110+
<TabItem value="Python" label="Python">
111+
<SolutionAuthor name="@ImmidiSivani"/>
112+
113+
```python
114+
class Solution:
115+
def increasingTriplet(self, nums: List[int]) -> bool:
116+
first = second = float('inf')
117+
for num in nums:
118+
if num <= first:
119+
first = num
120+
elif num <= second:
121+
second = num
122+
else:
123+
return True
124+
return False
125+
```
126+
127+
</TabItem>
128+
</Tabs>
129+
130+
#### Complexity Analysis
131+
132+
- **Time Complexity**: $O(n)$, where $n$ is the length of the array. We iterate through the array once.
133+
- **Space Complexity**: $O(1)$. We use a constant amount of extra space for the variables `first` and `second`.
134+
135+
---
136+
137+
<h2>Authors:</h2>
138+
139+
<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}>
140+
{['ImmidiSivani'].map(username => (
141+
<Author key={username} username={username} />
142+
))}
143+
</div>

0 commit comments

Comments
 (0)