Skip to content

Commit bcb9d14

Browse files
authored
feat: use the slices.Min/Max functions in golang code snippets (doocs#1951)
1 parent 9015f7c commit bcb9d14

File tree

180 files changed

+279
-857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+279
-857
lines changed

lcci/08.13.Pile Box/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public:
108108
### **Go**
109109
110110
```go
111-
func pileBox(box [][]int) (ans int) {
111+
func pileBox(box [][]int) int {
112112
sort.Slice(box, func(i, j int) bool {
113113
a, b := box[i], box[j]
114114
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
@@ -122,9 +122,8 @@ func pileBox(box [][]int) (ans int) {
122122
}
123123
}
124124
f[i] += box[i][2]
125-
ans = max(ans, f[i])
126125
}
127-
return
126+
return slices.Max(f)
128127
}
129128
```
130129

lcci/08.13.Pile Box/README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public:
9898
### **Go**
9999
100100
```go
101-
func pileBox(box [][]int) (ans int) {
101+
func pileBox(box [][]int) int {
102102
sort.Slice(box, func(i, j int) bool {
103103
a, b := box[i], box[j]
104104
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
@@ -112,9 +112,8 @@ func pileBox(box [][]int) (ans int) {
112112
}
113113
}
114114
f[i] += box[i][2]
115-
ans = max(ans, f[i])
116115
}
117-
return
116+
return slices.Max(f)
118117
}
119118
```
120119

lcci/08.13.Pile Box/Solution.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func pileBox(box [][]int) (ans int) {
1+
func pileBox(box [][]int) int {
22
sort.Slice(box, func(i, j int) bool {
33
a, b := box[i], box[j]
44
return a[0] < b[0] || (a[0] == b[0] && b[1] < a[1])
@@ -12,7 +12,6 @@ func pileBox(box [][]int) (ans int) {
1212
}
1313
}
1414
f[i] += box[i][2]
15-
ans = max(ans, f[i])
1615
}
17-
return
16+
return slices.Max(f)
1817
}

lcof2/剑指 Offer II 073. 狒狒吃香蕉/README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ public:
132132
133133
```go
134134
func minEatingSpeed(piles []int, h int) int {
135-
mx := 0
136-
for _, pile := range piles {
137-
mx = max(mx, pile)
138-
}
139-
left, right := 1, mx
135+
left, right := 1, slices.Max(piles)
140136
for left < right {
141137
mid := (left + right) >> 1
142138
s := 0

lcof2/剑指 Offer II 073. 狒狒吃香蕉/Solution.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
func minEatingSpeed(piles []int, h int) int {
2-
mx := 0
3-
for _, pile := range piles {
4-
mx = max(mx, pile)
5-
}
6-
left, right := 1, mx
2+
left, right := 1, slices.Max(piles)
73
for left < right {
84
mid := (left + right) >> 1
95
s := 0

lcp/LCP 33. 蓄水/README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ public:
125125
126126
```go
127127
func storeWater(bucket []int, vat []int) int {
128-
mx := 0
129-
for _, x := range vat {
130-
mx = max(mx, x)
131-
}
128+
mx := slices.Max(vat)
132129
if mx == 0 {
133130
return 0
134131
}

lcp/LCP 33. 蓄水/Solution.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
func storeWater(bucket []int, vat []int) int {
2-
mx := 0
3-
for _, x := range vat {
4-
mx = max(mx, x)
5-
}
2+
mx := slices.Max(vat)
63
if mx == 0 {
74
return 0
85
}

lcp/LCP 50. 宝石补给/README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ func giveGem(gem []int, operations [][]int) int {
131131
gem[y] += v
132132
gem[x] -= v
133133
}
134-
mx, mi := 0, 1<<30
135-
for _, x := range gem {
136-
mx = max(mx, x)
137-
mi = min(mi, x)
138-
}
139-
return mx - mi
134+
return slices.Max(gem) - slices.Min(gem)
140135
}
141136
```
142137

lcp/LCP 50. 宝石补给/Solution.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,5 @@ func giveGem(gem []int, operations [][]int) int {
55
gem[y] += v
66
gem[x] -= v
77
}
8-
mx, mi := 0, 1<<30
9-
for _, x := range gem {
10-
mx = max(mx, x)
11-
mi = min(mi, x)
12-
}
13-
return mx - mi
8+
return slices.Max(gem) - slices.Min(gem)
149
}

lcp/LCP 68. 美观的花束/README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ public:
128128
129129
```go
130130
func beautifulBouquet(flowers []int, cnt int) (ans int) {
131-
mx := 0
132-
for _, x := range flowers {
133-
mx = max(mx, x)
134-
}
131+
mx := slices.Max(flowers)
135132
d := make([]int, mx+1)
136133
j := 0
137134
const mod = 1e9 + 7

lcp/LCP 68. 美观的花束/Solution.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
func beautifulBouquet(flowers []int, cnt int) (ans int) {
2-
mx := 0
3-
for _, x := range flowers {
4-
mx = max(mx, x)
5-
}
2+
mx := slices.Max(flowers)
63
d := make([]int, mx+1)
74
j := 0
85
const mod = 1e9 + 7

solution/0000-0099/0032.Longest Valid Parentheses/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public:
152152
### **Go**
153153
154154
```go
155-
func longestValidParentheses(s string) (ans int) {
155+
func longestValidParentheses(s string) int {
156156
n := len(s)
157157
f := make([]int, n+1)
158158
for i := 2; i <= n; i++ {
@@ -162,10 +162,9 @@ func longestValidParentheses(s string) (ans int) {
162162
} else if j := i - f[i-1] - 1; j > 0 && s[j-1] == '(' {
163163
f[i] = f[i-1] + 2 + f[j-1]
164164
}
165-
ans = max(ans, f[i])
166165
}
167166
}
168-
return
167+
return slices.Max(f)
169168
}
170169
```
171170

solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public:
142142
### **Go**
143143
144144
```go
145-
func longestValidParentheses(s string) (ans int) {
145+
func longestValidParentheses(s string) int {
146146
n := len(s)
147147
f := make([]int, n+1)
148148
for i := 2; i <= n; i++ {
@@ -152,10 +152,9 @@ func longestValidParentheses(s string) (ans int) {
152152
} else if j := i - f[i-1] - 1; j > 0 && s[j-1] == '(' {
153153
f[i] = f[i-1] + 2 + f[j-1]
154154
}
155-
ans = max(ans, f[i])
156155
}
157156
}
158-
return
157+
return slices.Max(f)
159158
}
160159
```
161160

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func longestValidParentheses(s string) (ans int) {
1+
func longestValidParentheses(s string) int {
22
n := len(s)
33
f := make([]int, n+1)
44
for i := 2; i <= n; i++ {
@@ -8,8 +8,7 @@ func longestValidParentheses(s string) (ans int) {
88
} else if j := i - f[i-1] - 1; j > 0 && s[j-1] == '(' {
99
f[i] = f[i-1] + 2 + f[j-1]
1010
}
11-
ans = max(ans, f[i])
1211
}
1312
}
14-
return
13+
return slices.Max(f)
1514
}

solution/0200-0299/0253.Meeting Rooms II/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ func minMeetingRooms(intervals [][]int) int {
148148
delta[e[0]]++
149149
delta[e[1]]--
150150
}
151-
res := delta[0]
152151
for i := 1; i < n; i++ {
153152
delta[i] += delta[i-1]
154-
res = max(res, delta[i])
155153
}
156-
return res
154+
return slices.Max(delta)
157155
}
158156
```
159157

solution/0200-0299/0253.Meeting Rooms II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,10 @@ func minMeetingRooms(intervals [][]int) int {
129129
delta[e[0]]++
130130
delta[e[1]]--
131131
}
132-
res := delta[0]
133132
for i := 1; i < n; i++ {
134133
delta[i] += delta[i-1]
135-
res = max(res, delta[i])
136134
}
137-
return res
135+
return slices.Max(delta)
138136
}
139137
```
140138

solution/0200-0299/0253.Meeting Rooms II/Solution.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ func minMeetingRooms(intervals [][]int) int {
55
delta[e[0]]++
66
delta[e[1]]--
77
}
8-
res := delta[0]
98
for i := 1; i < n; i++ {
109
delta[i] += delta[i-1]
11-
res = max(res, delta[i])
1210
}
13-
return res
11+
return slices.Max(delta)
1412
}

solution/0200-0299/0265.Paint House II/README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public:
144144
### **Go**
145145
146146
```go
147-
func minCostII(costs [][]int) (ans int) {
147+
func minCostII(costs [][]int) int {
148148
n, k := len(costs), len(costs[0])
149149
f := cp(costs[0])
150150
for i := 1; i < n; i++ {
@@ -160,13 +160,7 @@ func minCostII(costs [][]int) (ans int) {
160160
}
161161
f = g
162162
}
163-
ans = f[0]
164-
for _, v := range f {
165-
if ans > v {
166-
ans = v
167-
}
168-
}
169-
return
163+
return slices.Min(f)
170164
}
171165
172166
func cp(arr []int) []int {

solution/0200-0299/0265.Paint House II/README_EN.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public:
120120
### **Go**
121121
122122
```go
123-
func minCostII(costs [][]int) (ans int) {
123+
func minCostII(costs [][]int) int {
124124
n, k := len(costs), len(costs[0])
125125
f := cp(costs[0])
126126
for i := 1; i < n; i++ {
@@ -136,13 +136,7 @@ func minCostII(costs [][]int) (ans int) {
136136
}
137137
f = g
138138
}
139-
ans = f[0]
140-
for _, v := range f {
141-
if ans > v {
142-
ans = v
143-
}
144-
}
145-
return
139+
return slices.Min(f)
146140
}
147141
148142
func cp(arr []int) []int {

solution/0200-0299/0265.Paint House II/Solution.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func minCostII(costs [][]int) (ans int) {
1+
func minCostII(costs [][]int) int {
22
n, k := len(costs), len(costs[0])
33
f := cp(costs[0])
44
for i := 1; i < n; i++ {
@@ -14,13 +14,7 @@ func minCostII(costs [][]int) (ans int) {
1414
}
1515
f = g
1616
}
17-
ans = f[0]
18-
for _, v := range f {
19-
if ans > v {
20-
ans = v
21-
}
22-
}
23-
return
17+
return slices.Min(f)
2418
}
2519

2620
func cp(arr []int) []int {

0 commit comments

Comments
 (0)