[LeetCode] 575. Distribute Candies

Chobby·2025년 4월 15일
1

LeetCode

목록 보기
357/427

😎풀이

  1. canEat: Alice가 먹을 수 있는 캔디의 최대 수
  2. typeOfCandies: candyType의 모든 캔디 종류 (중복 제거)
  3. typeSize: 캔디 종류의 수
  4. 먹을 수 있는 캔디(canEat)보다 캔디 종류(typeSize)가 많다면, 먹을 수 있는 캔디 내에서 각각 다른 캔디를 먹을 수 있으며, 아니라면 모든 종류의 캔디 수를 하나씩 먹을 수 있음
function distributeCandies(candyType: number[]): number {
    const canEat = Math.floor(candyType.length / 2)
    const typeOfCandies = new Set(candyType)
    const typeSize = typeOfCandies.size
    return typeSize > canEat ? canEat : typeSize
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글