-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1054-DistantBarcodes.go
More file actions
98 lines (88 loc) · 2.36 KB
/
1054-DistantBarcodes.go
File metadata and controls
98 lines (88 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
// 1054. Distant Barcodes
// In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].
// Rearrange the barcodes so that no two adjacent barcodes are equal.
// You may return any answer, and it is guaranteed an answer exists.
// Example 1:
// Input: barcodes = [1,1,1,2,2,2]
// Output: [2,1,2,1,2,1]
// Example 2:
// Input: barcodes = [1,1,1,1,2,2,3,3]
// Output: [1,3,1,3,1,2,1,2]
// Constraints:
// 1 <= barcodes.length <= 10000
// 1 <= barcodes[i] <= 10000
import "fmt"
import "sort"
func rearrangeBarcodes(barcodes []int) []int {
mp := map[int]int{}
for _, v := range barcodes { // 统计出现频次
mp[v]++
}
arr := make([][]int, 0, len(mp))
for k, v := range mp {
arr = append(arr, []int{ k, v })
}
sort.Slice(arr, func(i, j int) bool { // 出现次数从大到小排
return arr[i][1] > arr[j][1]
})
index := 0
for i := 0; i < len(barcodes); i += 2 {
if arr[index][1] == 0 {
index++
}
arr[index][1]--
barcodes[i] = arr[index][0]
}
for i := 1; i < len(barcodes); i += 2 {
if arr[index][1] == 0 {
index++
}
arr[index][1]--
barcodes[i] = arr[index][0]
}
return barcodes
}
func rearrangeBarcodes1(barcodes []int) []int {
n := len(barcodes)
mp := map[int]int{}
for _, v := range barcodes { // 统计出现频次
mp[v]++
}
val, freq := 0, 0
for k, v := range mp {
if freq < v {
val, freq = k, v
}
}
res, i := make([]int, n), 0
for freq > 0 {
res[i] = val
i += 2
freq--
}
delete(mp, val)
for k, v := range mp {
for v > 0 {
if i >= n {
i = 1
}
res[i] = k
i += 2
v--
}
}
return res
}
func main() {
// Example 1:
// Input: barcodes = [1,1,1,2,2,2]
// Output: [2,1,2,1,2,1]
fmt.Println(rearrangeBarcodes([]int{1,1,1,2,2,2})) // [2,1,2,1,2,1]
// Example 2:
// Input: barcodes = [1,1,1,1,2,2,3,3]
// Output: [1,3,1,3,1,2,1,2]
fmt.Println(rearrangeBarcodes([]int{1,1,1,1,2,2,3,3})) // [1,3,1,3,1,2,1,2]
fmt.Println(rearrangeBarcodes1([]int{1,1,1,2,2,2})) // [2,1,2,1,2,1]
fmt.Println(rearrangeBarcodes1([]int{1,1,1,1,2,2,3,3})) // [1,3,1,3,1,2,1,2]
}