Java Solution,HashMap


  • 0
    
    public class Solution {
        public int distributeCandies(int[] candies) {
            int len=candies.length;
            
            Map<Integer,Integer> hashK=new HashMap<>(1024);
            
            for(int i=0;i<len;i++){
                if(!hashK.containsKey(candies[i])){
                    hashK.put(candies[i],i);
                }
            }
            
            int kindSize=hashK.size();
            
            return kindSize>=len/2?len/2:kindSize;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.