[LeetCode] 1431. Kids With the Greatest Number of Candies

Semidragon·2023년 8월 8일
0

CodingTest

목록 보기
3/80

1. Question

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

Note that multiple kids can have the greatest number of candies.

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.

2. Thoughts

if list = [2,3,5,1,3], extraCandies = 3
1. get the max candy (in this case 5)
2. For all elements in the list, if candy + extraCandies >=max : true, else: false
3. return list

3. Tips learned

3.1 Boolean

For boolean, use "True", and "False".
Not, TRUE or true or FALSE or false.

3.2 Max, len

we can get the Max value of the list using max(),
we can get the len of list using len()

Makes:

4. My solution

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        max_candy = max(candies)
        canMax = []
        for i in range(len(candies)):
            # print(i)
            canMax.append(True if candies[i] + extraCandies >= max_candy else False)

        return canMax

5. AI Solution and Improvements

Your solution is already quite straightforward and clear. It uses the algorithm of finding the maximum number of candies a kid currently has and then checking for each kid if they can have candies greater than or equal to this number after getting the extra candies. The algorithm runs in (O(n)) time where (n) is the length of the candies list.

However, here are a few suggestions to make your code more Pythonic:

  1. Use list comprehension for more concise code.
  2. There's no need for the class Solution unless you're working within the confines of a platform like LeetCode.

Here's a refactored version of your code:

def kidsWithCandies(candies: [int], extraCandies: int) -> [bool]:
    max_candy = max(candies)
    return [candy + extraCandies >= max_candy for candy in candies]

Regarding a better algorithm: the given algorithm is already efficient for this problem. Since you need to look at every kid and determine their status after getting the extra candies, the time complexity will remain (O(n)).

profile
Semidragon's network [CS undergrad @ Sungkyunkwan University | Networks + System @ CSI]

0개의 댓글