Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56ad44a

Browse files
committed
feat: use the slices.Min/Max functions in golang code snippets
1 parent 60a9dba commit 56ad44a

File tree

153 files changed

+252
-748
lines changed

Some content is hidden

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

153 files changed

+252
-748
lines changed

‎lcci/08.13.Pile Box/README.md‎

Lines changed: 2 additions & 3 deletions
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‎

Lines changed: 2 additions & 3 deletions
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‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func pileBox(box [][]int) (ansint) {
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+
returnslices.Max(f)
1817
}

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

Lines changed: 1 addition & 5 deletions
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‎

Lines changed: 1 addition & 5 deletions
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

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

Lines changed: 2 additions & 3 deletions
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‎

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func longestValidParentheses(s string) (ansint) {
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+
returnslices.Max(f)
1514
}

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

Lines changed: 1 addition & 3 deletions
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‎

Lines changed: 1 addition & 3 deletions
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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /