Skip to content

Commit 4a24ca0

Browse files
authored
Merge pull request #464 from TTWShell/bugfix/s334
Bugfix/s334
2 parents 94c4b56 + e92bff4 commit 4a24ca0

File tree

4 files changed

+25
-38
lines changed

4 files changed

+25
-38
lines changed

.circleci/config.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module github.com/TTWShell/algorithms
22

3-
go 1.16
3+
go 1.19
44

55
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

leetcode/array/increasingTriplet.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ Example 2:
2020

2121
package larray
2222

23-
func increasingTriplet(nums []int) bool {
24-
left, right := -1, 0
25-
for i := 1; i < len(nums); i++ {
26-
if nums[i-1] >= nums[i] {
27-
continue
28-
}
23+
import "math"
2924

30-
if left < 0 {
31-
left, right = i-1, i
32-
continue
33-
}
25+
func increasingTriplet(nums []int) bool {
26+
if len(nums) < 3 {
27+
return false
28+
}
3429

35-
if nums[i-1] < nums[right] && nums[left] < nums[i-1] { // update right only
36-
right = i - 1
30+
min := nums[0]
31+
minMid := math.MaxInt32
32+
for i := 1; i < len(nums); i++ {
33+
if nums[i] > minMid {
34+
return true
3735
}
38-
if nums[i] < nums[right] { // need update all
39-
left, right = i-1, i
36+
if min > nums[i-1] {
37+
min = nums[i-1]
4038
}
41-
if left >= 0 && nums[left] < nums[right] && nums[right] < nums[i] {
42-
return true
39+
if nums[i] > min {
40+
if nums[i] < minMid {
41+
minMid = nums[i]
42+
}
4343
}
4444
}
4545
return false

leetcode/array/increasingTriplet_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ func Test_increasingTriplet(t *testing.T) {
1717
assert.False(increasingTriplet([]int{2, 1, 5, 0, 3}))
1818
assert.True(increasingTriplet([]int{2, 1, 5, 0, 4, 6}))
1919
assert.True(increasingTriplet([]int{1, 2, -10, -8, -7}))
20+
assert.True(increasingTriplet([]int{0, 4, 1, -1, 2}))
21+
assert.True(increasingTriplet([]int{20, 100, 10, 12, 5, 13}))
2022
}

0 commit comments

Comments
 (0)