-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution2610.go
60 lines (47 loc) · 1.16 KB
/
solution2610.go
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
package solution2610
// ============================================================================
// 2610. Convert an Array Into a 2D Array With Conditions
// URL: https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/2610
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_findMatrix
Benchmark_findMatrix-24 6725511 182.9 ns/op 264 B/op 7 allocs/op
PASS
*/
import (
"slices"
)
func findMatrix(nums []int) [][]int {
output := make([][]int, len(nums))
for _, num := range nums {
if len(output) == 0 {
output[0] = append(output[0], num)
}
idx, ok := find(output, num)
if ok {
output[idx] = append(output[idx], num)
continue
}
output[len(output)] = append(output[len(output)], num)
}
for i := range output {
if len(output[i]) == 0 {
output = output[:i]
break
}
}
return output
}
func find(output [][]int, num int) (int, bool) {
idx := 0
for i, row := range output {
if !slices.Contains(row, num) {
return i, true
}
}
return idx, false
}