Algorithm/LeetCode

[LeetCode] Kids With the Greatest Number of Candies

import java.util.*;

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int max = candies[0];

        for (int i = 0; i < candies.length; i++) {
            if (candies[i] > max){
                max = candies[i];
            }
        }
        
        ArrayList<Boolean> result = new ArrayList<Boolean>(candies.length);

        for (int i = 0; i < candies.length; i++) {
            if (candies[i] + extraCandies >= max) {
                result.add(true);
            } else {
                result.add(false);
            }
        }
        return result;
    }    
}

Runtime: 0 ms
Memory Usage: 39.2 MB

 

import java.util.*;

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> kids=new ArrayList<Boolean>();     
        int maxc=candies[0];
        for(int i=0;i<candies.length;i++){
            if(candies[i]>maxc){
                maxc=candies[i];
            }
        }
        for(int i=0;i < candies.length; i++)
        {
            if ( candies[i] + extraCandies >= maxc)
            {
                kids.add(true);
            }
            else
            {
                kids.add(false);
            }
        }     
        System.gc();
        return kids;
    }    
}

Runtime: 1 ms, faster than 47.50% of Java online submissions for Kids With the Greatest Number of Candies.
Memory Usage: 38.4 MB, less than 99.22% of Java online submissions for Kids With the Greatest Number of Candies.