[LeetCode] 2144. Minimum Cost of Buying Candies With Discount

Chobby·7일 전
1

LeetCode

목록 보기
675/728

😎풀이

  1. cost를 내림차 순 정렬
  2. 정렬된 가격 배열 순회
    2-1. 세 번째 구매하는 캔디는 무료로 구매
    2-2. 첫 번째, 두 번째 구매 캔디의 가격을 합산
  3. 총 가격 반환
function minimumCost(cost: number[]): number {
    const sorted = cost.toSorted((a, b) => b - a)
    const price = sorted.reduce((acc, cur, idx) => {
        if((idx + 1) % 3 === 0) return acc
        return acc + cur
    }, 0)
    return price
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글