Game Tech Blog

1431. Kids With the Greatest Number of Candies 본문

Algorithm/LeetCode

1431. Kids With the Greatest Number of Candies

jonghow 2023. 7. 7. 02:52
반응형

[문제]

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.

 

[TC]

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]

Constraints:

  • n == candies.length
  • 2 <= n <= 100
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50

[접근]

문제가 서두가 긴 편으로 느껴져서 대충 읽은다음에, TC 부터 봤다. TC 설명이 너무 이해가 잘되게 써져있어서 문제를 안봐도 TC 만 봐도 풀 수 있는 문제였다.

그냥 핵심은 Max 빨리뽑고 그 중에 Extra 랑 배열 변수들 인덱싱 접근해서 더해보고 내가 최고 많다! 이거를 뽑는 문제가 Easy 카테고리 그자체의 문제였다.

 

[코드] C#

-> 이 코드에 의문이 있는데, 최대한 최적화 해본다고 해본코드가 오히려 성능이 낮았다.

1. 첫번째 트라이

public class Solution {
    public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {

        List<bool> list = new List<bool>();
        int value = candies.Max();

        for(int i = 0; i < candies.Length; ++i)
        {
            if((candies[i] + extraCandies) >= value) 
                list.Add(true);
            else
                list.Add(false);
        }
        return list;
    }
}

2. 두번째 트라이 => 자료구조 없이 bool 배열로 런타임 최적화를 해봤다..

public class Solution {
    public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {

        int value = candies.Max();
        bool[] returnsBool = new bool[candies.Length];

        for(int i = 0; i < candies.Length; ++i)
        {
            if((candies[i] + extraCandies) >= value) 
                returnsBool[i] = true;
            else
                returnsBool[i] = false;
        }
        return returnsBool.ToList();
    }
}

3. 세번째 트라이 => array.Max가 성능이 낮나? 라는 의구심에서 하나하나 만들었는데 결과가 더 느렸다.

public class Solution {
    public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {

        int value = 0;

        for(int i = 0; i < candies.Length; ++i)
        {
            if(value <= candies[i])
                value = candies[i];
        }

        bool[] returnsBool = new bool[candies.Length];

        for(int i = 0; i < candies.Length; ++i)
        {
            if((candies[i] + extraCandies) >= value) 
                returnsBool[i] = true;
            else
                returnsBool[i] = false;
        }
        return returnsBool.ToList();
    }
}

[결과] -> 코드 순서대로 결과다.

1.

2.

3.

[후기]

1일 1문제를 풀고있어서 내일은 무슨 문제지 하면서 걸어다니면서 방법론 생각해보려고 열어본 문제인데, 읽는시간1분 코드작성 1분 TC 체크 1 분 Submit 까지 총 3분 걸린거같다.

코테에선 이런문제 거의 안나오거나 1번으로 나올거같은데, 저 의문의 런타임은 한번 왜그런지 생각해봐야겠다.

 

반응형

'Algorithm > LeetCode' 카테고리의 다른 글

151. Reverse Words in a String  (0) 2023.07.11
345. Reverse Vowels of a String  (0) 2023.07.10
605. Can Place Flowers  (0) 2023.07.10
1071. Greatest Common Divisor of Strings  (0) 2023.07.07
1768. Merge Strings Alternately  (0) 2023.07.06
Comments