[LeetCode] 1475. Final Prices With a Special Discount in a Shop

Chobby·2025년 9월 1일
1

LeetCode

목록 보기
531/582

😎풀이

  1. prices를 이중 순회한다.
    1-1. 현재 요소와 이후 요소를 비교한다.
    1-2. 현재 요소와 가장 가까우면서 더 낮은 가격만큼 현재 요소에 할인을 적용한다.
    1-3. 만일, 현재 요소 보다 이후 모든 요소가 더 비싸다면, 할인이 적용되지 않는다.
  2. 할인이 적용된 가격을 반환한다.
function finalPrices(prices: number[]): number[] {
    const final = []
    const n = prices.length
    for(let i = 0; i < n; i++) {
        const cur = prices[i]
        for(let j = i + 1; j < n; j++) {
            const next = prices[j]
            if(cur < next) continue
            final.push(cur - next)
            break
        }
        if(final.length <= i) final.push(cur)
    }
    return final
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글