Skip to content

Commit 3c28def

Browse files
committed
feat: add solutions to lc problem: No.0334.Increasing Triplet Subsequence
1 parent 5389785 commit 3c28def

File tree

8 files changed

+235
-3
lines changed

8 files changed

+235
-3
lines changed

.github/workflows/contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Contributors
33
on:
44
push:
55
branches: [main]
6+
workflow_dispatch:
67

78
jobs:
89
contributors:

.github/workflows/sync.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Sync
33
on:
44
push:
55
branches: [main]
6+
workflow_dispatch:
67

78
jobs:
89
sync:

solution/0300-0399/0334.Increasing Triplet Subsequence/README.md

+91-2
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,116 @@
4848

4949
<p><strong>进阶:</strong>你能实现时间复杂度为 <code>O(n)</code> ,空间复杂度为 <code>O(1)</code> 的解决方案吗?</p>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

55+
用 min, mid 记录遍历过程中遇到的最小值以及中间值,若出现 num > mid,说明找到了满足题目的三元组,返回 true;否则遍历结束返回 false。
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

6061
<!-- 这里可写当前语言的特殊实现逻辑 -->
6162

6263
```python
63-
64+
class Solution:
65+
def increasingTriplet(self, nums: List[int]) -> bool:
66+
mi, mid = float('inf'), float('inf')
67+
for num in nums:
68+
if num > mid:
69+
return True
70+
if num <= mi:
71+
mi = num
72+
else:
73+
mid = num
74+
return False
6475
```
6576

6677
### **Java**
6778

6879
<!-- 这里可写当前语言的特殊实现逻辑 -->
6980

7081
```java
82+
class Solution {
83+
public boolean increasingTriplet(int[] nums) {
84+
int n = nums.length;
85+
int[] lmi = new int[n];
86+
int[] rmx = new int[n];
87+
lmi[0] = Integer.MAX_VALUE;
88+
rmx[n - 1] = Integer.MIN_VALUE;
89+
for (int i = 1; i < n; ++i) {
90+
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
91+
}
92+
for (int i = n - 2; i >= 0; --i) {
93+
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
94+
}
95+
for (int i = 0; i < n; ++i) {
96+
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
97+
return true;
98+
}
99+
}
100+
return false;
101+
}
102+
}
103+
```
104+
105+
空间优化:
106+
107+
```java
108+
class Solution {
109+
public boolean increasingTriplet(int[] nums) {
110+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
111+
for (int num : nums) {
112+
if (num > mid) {
113+
return true;
114+
}
115+
if (num <= min) {
116+
min = num;
117+
} else {
118+
mid = num;
119+
}
120+
}
121+
return false;
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
bool increasingTriplet(vector<int>& nums) {
132+
int mi = INT_MAX, mid = INT_MAX;
133+
for (int num : nums)
134+
{
135+
if (num > mid) return true;
136+
if (num <= mi) mi = num;
137+
else mid = num;
138+
}
139+
return false;
140+
}
141+
};
142+
```
71143
144+
### **Go**
145+
146+
```go
147+
func increasingTriplet(nums []int) bool {
148+
min, mid := math.MaxInt32, math.MaxInt32
149+
for _, num := range nums {
150+
if num > mid {
151+
return true
152+
}
153+
if num <= min {
154+
min = num
155+
} else {
156+
mid = num
157+
}
158+
}
159+
return false
160+
}
72161
```
73162

74163
### **...**

solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md

+87-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,99 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def increasingTriplet(self, nums: List[int]) -> bool:
54+
mi, mid = float('inf'), float('inf')
55+
for num in nums:
56+
if num > mid:
57+
return True
58+
if num <= mi:
59+
mi = num
60+
else:
61+
mid = num
62+
return False
5363
```
5464

5565
### **Java**
5666

5767
```java
68+
class Solution {
69+
public boolean increasingTriplet(int[] nums) {
70+
int n = nums.length;
71+
int[] lmi = new int[n];
72+
int[] rmx = new int[n];
73+
lmi[0] = Integer.MAX_VALUE;
74+
rmx[n - 1] = Integer.MIN_VALUE;
75+
for (int i = 1; i < n; ++i) {
76+
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
77+
}
78+
for (int i = n - 2; i >= 0; --i) {
79+
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
80+
}
81+
for (int i = 0; i < n; ++i) {
82+
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
83+
return true;
84+
}
85+
}
86+
return false;
87+
}
88+
}
89+
```
90+
91+
```java
92+
class Solution {
93+
public boolean increasingTriplet(int[] nums) {
94+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
95+
for (int num : nums) {
96+
if (num > mid) {
97+
return true;
98+
}
99+
if (num <= min) {
100+
min = num;
101+
} else {
102+
mid = num;
103+
}
104+
}
105+
return false;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
bool increasingTriplet(vector<int>& nums) {
116+
int mi = INT_MAX, mid = INT_MAX;
117+
for (int num : nums)
118+
{
119+
if (num > mid) return true;
120+
if (num <= mi) mi = num;
121+
else mid = num;
122+
}
123+
return false;
124+
}
125+
};
126+
```
58127
128+
### **Go**
129+
130+
```go
131+
func increasingTriplet(nums []int) bool {
132+
min, mid := math.MaxInt32, math.MaxInt32
133+
for _, num := range nums {
134+
if num > mid {
135+
return true
136+
}
137+
if num <= min {
138+
min = num
139+
} else {
140+
mid = num
141+
}
142+
}
143+
return false
144+
}
59145
```
60146

61147
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
bool increasingTriplet(vector<int>& nums) {
4+
int mi = INT_MAX, mid = INT_MAX;
5+
for (int num : nums)
6+
{
7+
if (num > mid) return true;
8+
if (num <= mi) mi = num;
9+
else mid = num;
10+
}
11+
return false;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func increasingTriplet(nums []int) bool {
2+
min, mid := math.MaxInt32, math.MaxInt32
3+
for _, num := range nums {
4+
if num > mid {
5+
return true
6+
}
7+
if num <= min {
8+
min = num
9+
} else {
10+
mid = num
11+
}
12+
}
13+
return false
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean increasingTriplet(int[] nums) {
3+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
4+
for (int num : nums) {
5+
if (num > mid) {
6+
return true;
7+
}
8+
if (num <= min) {
9+
min = num;
10+
} else {
11+
mid = num;
12+
}
13+
}
14+
return false;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def increasingTriplet(self, nums: List[int]) -> bool:
3+
mi, mid = float('inf'), float('inf')
4+
for num in nums:
5+
if num > mid:
6+
return True
7+
if num <= mi:
8+
mi = num
9+
else:
10+
mid = num
11+
return False
12+

0 commit comments

Comments
 (0)