-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution3289.go
60 lines (52 loc) · 1.27 KB
/
solution3289.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 solution3289
import (
"slices"
)
// ============================================================================
// 3289. The Two Sneaky Numbers of Digitville
// URL: https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/
// ============================================================================
/*
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/3289---The-Two-Sneaky-Numbers-of-Digitville
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_getSneakyNumbersV2
Benchmark_getSneakyNumbersV2-24 203654988 9.014 ns/op 0 B/op 0 allocs/op
Benchmark_getSneakyNumbersV1
Benchmark_getSneakyNumbersV1-24 11519113 111.7 ns/op 96 B/op 2 allocs/op
PASS
*/
func getSneakyNumbersV2(nums []int) []int {
var a, b, c int
freq := make([]int, 101)
for _, n := range nums {
freq[n]++
switch {
case c == 0 && freq[n] == 2:
a = n
c++
case c == 1 && freq[n] == 2:
b = n
if a > b {
a, b = b, a
}
}
}
return []int{a, b}
}
func getSneakyNumbersV1(nums []int) []int {
ans := make([]int, len(nums))
freq := make(map[int]int, len(nums))
for _, n := range nums {
freq[n]++
v, ok := freq[n]
if ok {
if v == 2 {
ans = append(ans, n)
}
}
}
slices.Sort(ans)
return ans
}