Skip to content

Commit 14c0342

Browse files
aQuaaQua
authored andcommitted
822 finish
1 parent b0c2c64 commit 14c0342

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
package problem0822
22

33
func flipgame(fronts []int, backs []int) int {
4-
count := make(map[int]int, len(fronts)+len(backs))
4+
size := len(fronts)
55

6-
for i := range fronts {
7-
count[fronts[i]]++
8-
}
9-
10-
for i := range backs {
11-
count[backs[i]]++
6+
isBoth := make(map[int]bool, size)
7+
for i := 0; i < size; i++ {
8+
if fronts[i] == backs[i] {
9+
isBoth[fronts[i]] = true
10+
}
1211
}
1312

1413
upLimit := 2001
15-
min := upLimit
16-
for n, c := range count {
17-
if c == 1 && min > n {
18-
min = n
14+
15+
res := upLimit
16+
17+
for i := 0; i < size; i++ {
18+
if !isBoth[fronts[i]] {
19+
res = min(res, fronts[i])
20+
}
21+
if !isBoth[backs[i]] {
22+
res = min(res, backs[i])
1923
}
2024
}
2125

22-
if min == upLimit {
26+
if res == upLimit {
2327
return 0
2428
}
25-
return min
29+
return res
30+
31+
}
2632

33+
func min(a, b int) int {
34+
if a < b {
35+
return a
36+
}
37+
return b
2738
}

0 commit comments

Comments
 (0)