Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use the slices.Max function in golang code snippets #1951

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: use the slices.Max function in golang code snippets
  • Loading branch information
yanglbme committed Nov 10, 2023
commit 60a9dba0f729be3dee66a7090dd9ee91d63e12a5
5 changes: 1 addition & 4 deletions lcp/LCP 33. 蓄水/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public:

```go
func storeWater(bucket []int, vat []int) int {
mx := 0
for _, x := range vat {
mx = max(mx, x)
}
mx := slices.Max(vat)
if mx == 0 {
return 0
}
Expand Down
5 changes: 1 addition & 4 deletions lcp/LCP 33. 蓄水/Solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func storeWater(bucket []int, vat []int) int {
mx := 0
for _, x := range vat {
mx = max(mx, x)
}
mx := slices.Max(vat)
if mx == 0 {
return 0
}
Expand Down
7 changes: 1 addition & 6 deletions lcp/LCP 50. 宝石补给/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ func giveGem(gem []int, operations [][]int) int {
gem[y] += v
gem[x] -= v
}
mx, mi := 0, 1<<30
for _, x := range gem {
mx = max(mx, x)
mi = min(mi, x)
}
return mx - mi
return slices.Max(gem) - slices.Min(gem)
}
```

Expand Down
7 changes: 1 addition & 6 deletions lcp/LCP 50. 宝石补给/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@ func giveGem(gem []int, operations [][]int) int {
gem[y] += v
gem[x] -= v
}
mx, mi := 0, 1<<30
for _, x := range gem {
mx = max(mx, x)
mi = min(mi, x)
}
return mx - mi
return slices.Max(gem) - slices.Min(gem)
}
5 changes: 1 addition & 4 deletions lcp/LCP 68. 美观的花束/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,7 @@ public:

```go
func beautifulBouquet(flowers []int, cnt int) (ans int) {
mx := 0
for _, x := range flowers {
mx = max(mx, x)
}
mx := slices.Max(flowers)
d := make([]int, mx+1)
j := 0
const mod = 1e9 + 7
Expand Down
5 changes: 1 addition & 4 deletions lcp/LCP 68. 美观的花束/Solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func beautifulBouquet(flowers []int, cnt int) (ans int) {
mx := 0
for _, x := range flowers {
mx = max(mx, x)
}
mx := slices.Max(flowers)
d := make([]int, mx+1)
j := 0
const mod = 1e9 + 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@ public:

```go
func countTriplets(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
cnt := make([]int, mx+1)
for _, x := range nums {
for _, y := range nums {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ public:

```go
func countTriplets(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
cnt := make([]int, mx+1)
for _, x := range nums {
for _, y := range nums {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func countTriplets(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
cnt := make([]int, mx+1)
for _, x := range nums {
for _, y := range nums {
Expand Down
5 changes: 1 addition & 4 deletions solution/1000-1099/1054.Distant Barcodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public:

```go
func rearrangeBarcodes(barcodes []int) []int {
mx := 0
for _, x := range barcodes {
mx = max(mx, x)
}
mx := slices.Max(barcodes)
cnt := make([]int, mx+1)
for _, x := range barcodes {
cnt[x]++
Expand Down
5 changes: 1 addition & 4 deletions solution/1000-1099/1054.Distant Barcodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ public:

```go
func rearrangeBarcodes(barcodes []int) []int {
mx := 0
for _, x := range barcodes {
mx = max(mx, x)
}
mx := slices.Max(barcodes)
cnt := make([]int, mx+1)
for _, x := range barcodes {
cnt[x]++
Expand Down
5 changes: 1 addition & 4 deletions solution/1000-1099/1054.Distant Barcodes/Solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func rearrangeBarcodes(barcodes []int) []int {
mx := 0
for _, x := range barcodes {
mx = max(mx, x)
}
mx := slices.Max(barcodes)
cnt := make([]int, mx+1)
for _, x := range barcodes {
cnt[x]++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ func findBestValue(arr []int, target int) (ans int) {
sort.Ints(arr)
n := len(arr)
s := make([]int, n+1)
mx := 0
mx := slices.Max(arr)
for i, x := range arr {
s[i+1] = s[i] + x
mx = max(mx, x)
}
diff := 1 << 30
for value := 0; value <= mx; value++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ func findBestValue(arr []int, target int) (ans int) {
sort.Ints(arr)
n := len(arr)
s := make([]int, n+1)
mx := 0
mx := slices.Max(arr)
for i, x := range arr {
s[i+1] = s[i] + x
mx = max(mx, x)
}
diff := 1 << 30
for value := 0; value <= mx; value++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ func findBestValue(arr []int, target int) (ans int) {
sort.Ints(arr)
n := len(arr)
s := make([]int, n+1)
mx := 0
mx := slices.Max(arr)
for i, x := range arr {
s[i+1] = s[i] + x
mx = max(mx, x)
}
diff := 1 << 30
for value := 0; value <= mx; value++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ public:

```go
func minSetSize(arr []int) (ans int) {
mx := 0
for _, x := range arr {
mx = max(mx, x)
}
mx := slices.Max(arr)
cnt := make([]int, mx+1)
for _, x := range arr {
cnt[x]++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ public:

```go
func minSetSize(arr []int) (ans int) {
mx := 0
for _, x := range arr {
mx = max(mx, x)
}
mx := slices.Max(arr)
cnt := make([]int, mx+1)
for _, x := range arr {
cnt[x]++
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func minSetSize(arr []int) (ans int) {
mx := 0
for _, x := range arr {
mx = max(mx, x)
}
mx := slices.Max(arr)
cnt := make([]int, mx+1)
for _, x := range arr {
cnt[x]++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@ func minimumTeachings(n int, languages [][]int, friendships [][]int) int {
cnt[l]++
}
}
mx := 0
for _, v := range cnt {
mx = max(mx, v)
}
return len(s) - mx
return len(s) - slices.Max(cnt)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ func minimumTeachings(n int, languages [][]int, friendships [][]int) int {
cnt[l]++
}
}
mx := 0
for _, v := range cnt {
mx = max(mx, v)
}
return len(s) - mx
return len(s) - slices.Max(cnt)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@ func minimumTeachings(n int, languages [][]int, friendships [][]int) int {
cnt[l]++
}
}
mx := 0
for _, v := range cnt {
mx = max(mx, v)
}
return len(s) - mx
return len(s) - slices.Max(cnt)
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ public:

```go
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
vis := make([]bool, mx+1)
for _, x := range nums {
vis[x] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ public:

```go
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
vis := make([]bool, mx+1)
for _, x := range nums {
vis[x] = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
mx := 0
for _, x := range nums {
mx = max(mx, x)
}
mx := slices.Max(nums)
vis := make([]bool, mx+1)
for _, x := range nums {
vis[x] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ public:

```go
func longestSubarray(nums []int) int {
mx := 0
for _, v := range nums {
mx = max(mx, v)
}
mx := slices.Max(nums)
ans, cnt := 0, 0
for _, v := range nums {
if v == mx {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ public:

```go
func longestSubarray(nums []int) int {
mx := 0
for _, v := range nums {
mx = max(mx, v)
}
mx := slices.Max(nums)
ans, cnt := 0, 0
for _, v := range nums {
if v == mx {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
func longestSubarray(nums []int) int {
mx := 0
for _, v := range nums {
mx = max(mx, v)
}
mx := slices.Max(nums)
ans, cnt := 0, 0
for _, v := range nums {
if v == mx {
Expand Down