LeetCode 75

Kids With the Greatest Number of Candies

Problem
LC 1431
Topic
Array / String
File
official75_LC1431KidsWithTheGreatestNumberOfCandies.java
Path
pkg5leetcode/official75/official75_LC1431KidsWithTheGreatestNumberOfCandies.java
Package
pkg5leetcode.official75
Command
java pkg5leetcode/official75/official75_LC1431KidsWithTheGreatestNumberOfCandies.java
Approach
Compare each kid + extra against max existing.
Complexity
Time O(n), Space O(n)

LeetCode solutions

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg5leetcode/official75/official75_LC1431KidsWithTheGreatestNumberOfCandies.java
1package pkg5leetcode.official75;2 3/*4 * Kids With the Greatest Number of Candies | LC 14315 * APPROACH: Compare each kid + extra against max existing.6 * COMPLEXITY: Time O(n), Space O(n)7 */8import java.util.*;9 10public class official75_LC1431KidsWithTheGreatestNumberOfCandies {11    static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {12        int max = 0;13        for (int c : candies) max = Math.max(max, c);14        List<Boolean> res = new ArrayList<>();15        for (int c : candies) res.add(c + extraCandies >= max);16        return res;17    }18 19    public static void main(String[] args) {20        check(kidsWithCandies(new int[]{2,3,5,1,3}, 3).equals(Arrays.asList(true,true,true,false,true)), "case1");21        check(kidsWithCandies(new int[]{4,2,1,1,2}, 1).equals(Arrays.asList(true,false,false,false,false)), "case2");22        System.out.println("all tests passed");23    }24 25    static void check(boolean cond, String name) {26        if (!cond) throw new AssertionError("FAILED: " + name);27        System.out.println("  PASS " + name);28    }29}