Java, fixed boolean array, beats 99.03%


  • 0
    V

    Interesting, I've tried (100000 - candies[i]) instead of (100000 + candies[i]) and it gave only 98.05%. Maybe it's just a noise. Frankly, I expected (100000 - candies[i]) to be faster (empirical expectations).

    public class Solution {    
        public int distributeCandies(int[] candies) {
            boolean[] kind = new boolean[200001];
            
            for (int i = 0; i < candies.length; ++i) {
                kind[100000 + candies[i]] = true; 
            }
            
            int n = 0;
            
            int s = candies.length / 2;
            for (int i = 0; i < kind.length && s > 0; ++i) {
                if (kind[i]) {
                    ++n;
                    --s;
                }
            }
            
            return n;
        }
    }
    

Log in to reply
 

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