Skip to content

Commit aeb8aa8

Browse files
Simplify sort code
1 parent f6b4dc2 commit aeb8aa8

File tree

7 files changed

+11
-99
lines changed
  • src
    • 0891-Sum-of-Subsequence-Widths
    • 1170-Compare-Strings-by-Frequency-of-the-Smallest-Character
    • 1187-Make-Array-Strictly-Increasing
    • 1196-How-Many-Apples-Can-You-Put-into-the-Basket
    • 1200-Minimum-Absolute-Difference
    • 1202-Smallest-String-With-Swaps
    • 1215-Stepping-Numbers

7 files changed

+11
-99
lines changed
+1-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func sumSubseqWidths(A []int) int {
2-
sort.Sort(IntSlice(A))
2+
sort.Ints(A)
33
res, n, mod := 0, len(A), 1000000007
44
for i := 0; i < n; i++ {
55
res *= 2
@@ -8,18 +8,4 @@ func sumSubseqWidths(A []int) int {
88
res %= mod
99
}
1010
return (res + mod) % mod
11-
}
12-
13-
type IntSlice []int
14-
15-
func (s IntSlice) Len() int {
16-
return len(s)
17-
}
18-
19-
func (s IntSlice) Swap(i, j int) {
20-
s[i], s[j] = s[j], s[i]
21-
}
22-
23-
func (s IntSlice) Less(i, j int) bool {
24-
return s[i] < s[j]
2511
}

src/1170-Compare-Strings-by-Frequency-of-the-Smallest-Character/1170.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func numSmallerByFrequency(queries []string, words []string) []int {
1616
for i, word := range words {
1717
w[i] = f(word)
1818
}
19-
sort.Sort(IntSlice(w))
19+
sort.Ints(w)
2020

2121
for i, v := range queries {
2222
cnt, l, r := f(v), 0, len(w) - 1
@@ -35,10 +35,4 @@ func numSmallerByFrequency(queries []string, words []string) []int {
3535
res[i] = len(w) - l
3636
}
3737
return res
38-
}
39-
40-
type IntSlice []int
41-
42-
func (s IntSlice) Len() int { return len(s) }
43-
func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
44-
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
38+
}

src/1187-Make-Array-Strictly-Increasing/1187.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
func makeArrayIncreasing(arr1 []int, arr2 []int) int {
22
l1, l2 := len(arr1), len(arr2)
3-
sort.Sort(IntSlice(arr2))
3+
sort.Ints(arr2)
44
dp := map[int]int{0:arr1[0], 1:arr2[0]}
55
for i := 1; i < l1; i++ {
66
ndp := make(map[int]int)
@@ -45,18 +45,4 @@ func min(a, b int) int {
4545
return a
4646
}
4747
return b
48-
}
49-
50-
type IntSlice []int
51-
52-
func (s IntSlice) Len() int {
53-
return len(s)
54-
}
55-
56-
func (s IntSlice) Swap(i, j int) {
57-
s[i], s[j] = s[j], s[i]
58-
}
59-
60-
func (s IntSlice) Less(i, j int) bool {
61-
return s[i] < s[j]
6248
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func maxNumberOfApples(arr []int) int {
2-
sort.Sort(IntSlice(arr))
2+
sort.Ints(arr)
33
res, t := 0, 0
44
for _, v := range arr {
55
t += v
@@ -9,18 +9,4 @@ func maxNumberOfApples(arr []int) int {
99
res++
1010
}
1111
return res
12-
}
13-
14-
type IntSlice []int
15-
16-
func (s IntSlice) Len() int {
17-
return len(s)
18-
}
19-
20-
func (s IntSlice) Swap(i, j int) {
21-
s[i], s[j] = s[j], s[i]
22-
}
23-
24-
func (s IntSlice) Less(i, j int) bool {
25-
return s[i] < s[j]
2612
}
+1-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func minimumAbsDifference(arr []int) [][]int {
2-
sort.Sort(IntSlice(arr))
2+
sort.Ints(arr)
33
res := make([][]int, 0)
44
min_dif := 10000000
55
for i := 1; i < len(arr); i++ {
@@ -13,18 +13,4 @@ func minimumAbsDifference(arr []int) [][]int {
1313
res = append(res, []int{arr[i-1], arr[i]})
1414
}
1515
return res
16-
}
17-
18-
type IntSlice []int
19-
20-
func (s IntSlice) Len() int {
21-
return len(s)
22-
}
23-
24-
func (s IntSlice) Swap(i, j int) {
25-
s[i], s[j] = s[j], s[i]
26-
}
27-
28-
func (s IntSlice) Less(i, j int) bool {
29-
return s[i] < s[j]
3016
}

src/1202-Smallest-String-With-Swaps/1202.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func smallestStringWithSwaps(s string, pairs [][]int) string {
1919
mem[x] = append(mem[x], s[i])
2020
}
2121
for k, _ := range mem {
22-
sort.Sort(IntSlice(mem[k]))
22+
sort.Slice(mem[k], func(i, j int) bool {
23+
return mem[k][i] < mem[k][j]
24+
})
2325
}
2426

2527
res := make([]byte, 0)
@@ -36,18 +38,4 @@ func find(x int) int {
3638
p[x] = find(p[x])
3739
}
3840
return p[x]
39-
}
40-
41-
type IntSlice []byte
42-
43-
func (s IntSlice) Len() int {
44-
return len(s)
45-
}
46-
47-
func (s IntSlice) Swap(i, j int) {
48-
s[i], s[j] = s[j], s[i]
49-
}
50-
51-
func (s IntSlice) Less(i, j int) bool {
52-
return s[i] < s[j]
5341
}

src/1215-Stepping-Numbers/1215.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func countSteppingNumbers(low int, high int) []int {
77
for i := 0; i <= 9; i++ {
88
bfs(i)
99
}
10-
sort.Sort(IntSlice(res))
10+
sort.Ints(res)
1111
return res
1212
}
1313

@@ -34,18 +34,4 @@ func bfs(s int) {
3434
q = append(q, s2)
3535
}
3636
}
37-
}
38-
39-
type IntSlice []int
40-
41-
func (s IntSlice) Len() int {
42-
return len(s)
43-
}
44-
45-
func (s IntSlice) Swap(i, j int) {
46-
s[i], s[j] = s[j], s[i]
47-
}
48-
49-
func (s IntSlice) Less(i, j int) bool {
50-
return s[i] < s[j]
51-
}
37+
}

0 commit comments

Comments
 (0)