File tree Expand file tree Collapse file tree 1 file changed +24
-13
lines changed
Algorithms/0822.card-flipping-game Expand file tree Collapse file tree 1 file changed +24
-13
lines changed Original file line number Diff line number Diff line change 11package problem0822
22
33func 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}
You can’t perform that action at this time.
0 commit comments