Created
July 27, 2017 09:57
-
-
Save luojiyin1987/d51942e44909e55b4aaf4003525ef663 to your computer and use it in GitHub Desktop.
Distribute Candies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func distributeCandies(candies []int) int { | |
| uniqCount := 0 | |
| uniq := make(map[int]bool, 0) | |
| for _,v := range(candies) { | |
| if (uniq[v] == false) { | |
| uniq[v]=true | |
| uniqCount++ | |
| } | |
| if (uniqCount >= len(candies)/2) { | |
| break | |
| } | |
| } | |
| return uniqCount | |
| } | |
| func min(a int, b int) int { | |
| if a<b { | |
| return a | |
| } else { | |
| return b | |
| } | |
| } | |
| -------------------------------------------------------------------------------------------------- | |
| func distributeCandies(candies []int) int { | |
| var candyTotal int = len(candies) | |
| var kindMax = candyTotal/2; | |
| kindHash := make(map[int]bool) | |
| for i:=0; i< candyTotal;i++ { | |
| kind := candies[i] | |
| kindHash[kind] = true | |
| } | |
| kindTotal := len(kindHash) | |
| return min(kindMax, kindTotal) | |
| } | |
| ----------------------------------------------------------------------------------------------------------------- | |
| func distributeCandies(candies []int) int { | |
| numcount := map[int]int{} | |
| for _ ,v := range candies { | |
| value, ok :=numcount[v] | |
| if ok { | |
| numcount[v] = value +1 | |
| } else { | |
| numcount[v] = 1 | |
| } | |
| } | |
| l1 :=len(numcount) | |
| l2 := len(candies)/2 | |
| if l1 > l2 { | |
| return l2 | |
| } else { | |
| return l1 | |
| } | |
| } | |
| ------------------------------------------------------------------------------------------------- | |
| func distributeCandies(candies []int) int { | |
| numcount := map[int]int{} | |
| for _ ,v := range candies { | |
| _, ok :=numcount[v] | |
| if !ok { | |
| numcount[v] = 1 | |
| } | |
| } | |
| l1 :=len(numcount) | |
| l2 := len(candies)/2 | |
| if l1 > l2 { | |
| return l2 | |
| } else { | |
| return l1 | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment