알고리즘
[알고리즘] Distribute Candies
keel_im
2021. 3. 2. 10:34
반응형
포인트
1. 사탕을 어떻게 나누어야 하는가를 고민을 해보자
사탕을 나누는 방법은 종류가 상한선 보다 적으면 종류를 반환을 하고 상한선보다 같거나 크다면 상한선을 반환을 한다.
🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다.
c++/cpp
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int distributeCandies(vector<int>& candyType) {
unordered_set<int> set1(candyType.begin(), candyType.end());
return min(set1.size(), candyType.size()/2);
}
};
python
from collections import Counter
class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
# dict1 = {ele: candyType.count(ele) for ele in candyType}
# dict1 = dict()
# for ele in candyType:
# dict1[ele] = dict1.get(ele, 0) + 1
# dict1 = Counter(candyType)
# limit = len(candyType)//2
# return limit if len(dict1)>= limit else len(dict1)
# distinctTypes = set(candyType)
# return min(len(distinctTypes), int(len(candyType)/2))
distinctTypes = set(candyType)
return min(len(distinctTypes), len(candyType)//2)
반응형