Recommended Posts

Interviewer: Kids With the Greatest Number of Candies (Typescript, Python, Go)

0 comments

simplestack0.312 months agoPeakD8 min read


https://files.peakd.com/file/peakd-hive/simplestack/23xKtdT7E8EdUkGKJCmM2d977WBJq9H8S7WDZatJx2NLTQruxyPtKD7vtBXsq2WCLEvXS.png

You have n children, each with a certain number of candies. The number of candies each child has is given in the integer array candies, where candies[i] is the number of candies the i-th child has. You also have extraCandies to distribute.

Your task is to determine, for each child, if they will have the most candies after receiving all the extraCandies. Return a boolean array result of length n. result[i] should be true if the i-th child will have the greatest number of candies (or be tied for the greatest) after getting the extraCandies, and false otherwise.

It's possible for multiple children to have the same, largest number of candies.

Cases

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]

Explanation: If you give all extraCandies to:

  • Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
  • Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
  • Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
  • Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
  • Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Example 2:

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false] 

Explanation: There is only 1 extra candy.

Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.

Example 3:

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]

Solution in Typescript

/**
 * Determines which kids will have the greatest number of candies
 * after receiving extra candies.
 *
 * @param candies An array of integers representing the number of candies each kid has.
 * @param extraCandies The number of extra candies to distribute.
 * @returns An array of booleans, where result[i] is true if the i-th kid will have
 *          the greatest number of candies after receiving extraCandies, and false otherwise.
 */
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
    // Find the greatest number of candies among all kids.
    const greatestAmount = Math.max(...candies);

    // Create a boolean array by mapping each kid's candies to a boolean value.
    const booleanList: boolean[] = candies.map(amount => shouldSetToTrue(amount, extraCandies, greatestAmount));

    return booleanList;
}

/**
 * Helper function to determine if a kid will have the greatest number of candies.
 *
 * @param amount The number of candies a kid currently has.
 * @param extraCandies The number of extra candies available.
 * @param greatestAmount The greatest number of candies among all kids.
 * @returns True if the kid will have the greatest number of candies after receiving extraCandies, 
 *          or if they already have the greatest amount, False otherwise.
 */
function shouldSetToTrue(amount: number, extraCandies: number, greatestAmount: number): boolean {
    // If the kid already has the greatest amount, they will still have the greatest amount.
    if (amount === greatestAmount) {
        return true;
    }

    // If the kid's candies plus the extra candies are greater than or equal to the greatest amount,
    // they will have (or tie for) the greatest amount.
    if ((amount + extraCandies) >= greatestAmount) {
        return true;
    }

    // Otherwise, the kid will not have the greatest amount.
    return false;
}

The kidsWithCandies function goes through each child, one by one. For each child, it uses the shouldSetToTrue function to figure out if that child will have the most candies after they get their share of the extra candies.

The shouldSetToTrue function does this by checking if the child already has the most, or if the extra candies will be enough to make them have the most. Finally, kidsWithCandies returns a list of true/false values, one for each child.

Solution in Go

package main

import "fmt"

/**
 * Determines which kids will have the greatest number of candies
 * after receiving extra candies.
 *
 * @param candies An array of integers representing the number of candies each kid has.
 * @param extraCandies The number of extra candies to distribute.
 * @returns An array of booleans, where result[i] is true if the i-th kid will have
 *          the greatest number of candies after receiving extraCandies, and false otherwise.
 */
func kidsWithCandies(candies []int, extraCandies int) []bool {
        // Find the greatest number of candies among all kids.
        greatestAmount := 0
        for _, amount := range candies {
                if amount > greatestAmount {
                        greatestAmount = amount
                }
        }

        // Create a boolean array.
        booleanList := make([]bool, len(candies))
        for i, amount := range candies {
                booleanList[i] = shouldSetToTrue(amount, extraCandies, greatestAmount)
        }

        return booleanList
}

/**
 * Helper function to determine if a kid will have the greatest number of candies.
 *
 * @param amount The number of candies a kid currently has.
 * @param extraCandies The number of extra candies available.
 * @param greatestAmount The greatest number of candies among all kids.
 * @returns True if the kid will have the greatest number of candies after receiving extraCandies,
 *          or if they already have the greatest amount, False otherwise.
 */
func shouldSetToTrue(amount int, extraCandies int, greatestAmount int) bool {
        // If the kid already has the greatest amount, they will still have the greatest amount.
        if amount == greatestAmount {
                return true
        }

        // If the kid's candies plus the extra candies are greater than or equal to the greatest amount,
        // they will have (or tie for) the greatest amount.
        if (amount + extraCandies) >= greatestAmount {
                return true
        }

        // Otherwise, the kid will not have the greatest amount.
        return false
}

The kidsWithCandies function goes through each child, one by one. For each child, it uses the shouldSetToTrue function to figure out if that child will have the most candies after they get their share of the extra candies.

The shouldSetToTrue function does this by checking if the child already has the most, or if the extra candies will be enough to make them have the most. Finally, kidsWithCandies returns a list of true/false values, one for each child.

Solution in Python

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        """
        Determines which kids will have the greatest number of candies 
        after receiving extra candies.

        Args:
            candies: A list of integers representing the number of candies each kid has.
            extraCandies: An integer representing the number of extra candies available.

        Returns:
            A list of booleans, where result[i] is True if the i-th kid will have 
            the greatest number of candies after receiving extraCandies, and False otherwise.
        """

        # Find the greatest number of candies among all kids initially.
        greatest_amount = max(candies)

        # Create a boolean list using list comprehension.
        # For each kid's candies, call the helper function to check if they will have the greatest amount.
        boolean_list = [self.shouldSetToTrue(amount, extraCandies, greatest_amount) for amount in candies]

        return boolean_list

    def shouldSetToTrue(self, amount: int, extraCandies: int, greatestAmount: int):
        """
        Helper function to determine if a kid will have the greatest number of candies.

        Args:
            amount: The number of candies a kid currently has.
            extraCandies: The number of extra candies available.
            greatestAmount: The greatest number of candies among all kids.

        Returns:
            True if the kid will have the greatest number of candies after receiving extraCandies, False otherwise.
        """

        # If the kid already has the greatest amount, they will still have the greatest amount.
        if amount == greatestAmount:
            return True

        # If the kid's candies plus the extra candies are greater than or equal to the greatest amount,
        # they will have (or tie for) the greatest amount.
        if (amount + extraCandies) >= greatestAmount:
            return True

        # Otherwise, the kid will not have the greatest amount.
        return False

The kidsWithCandies method goes through each child, one by one. For each child, it uses the shouldSetToTrue method to figure out if that child will have the most candies after they get their share of the extra candies.

The shouldSetToTrue method does this by checking if the child already has the most, or if the extra candies will be enough to make them have the most. Finally, kidsWithCandies returns a list of True/False values, one for each child. The use of a class is a way to group these two related functions together.


If you liked this content I’d appreciate an upvote or a comment. That helps me improve the quality of my posts as well as getting to know more about you, my dear reader.

Muchas gracias!

Follow me for more content like this.

X | PeakD | Rumble | YouTube | Linked In | GitHub | PayPal.me | Medium

Down below you can find other ways to tip my work.

BankTransfer: "710969000019398639", // CLABE
BAT: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
ETH: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
BTC: "33xxUWU5kjcPk1Kr9ucn9tQXd2DbQ1b9tE",
ADA: "addr1q9l3y73e82hhwfr49eu0fkjw34w9s406wnln7rk9m4ky5fag8akgnwf3y4r2uzqf00rw0pvsucql0pqkzag5n450facq8vwr5e",
DOT: "1rRDzfMLPi88RixTeVc2beA5h2Q3z1K1Uk3kqqyej7nWPNf",
DOGE: "DRph8GEwGccvBWCe4wEQsWsTvQvsEH4QKH",
DAI: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875"

Comments

Sort byBest