[LeetCode] 2335. Minimum Amount of Time to Fill Cups

Chobby·2025년 10월 27일
1

LeetCode

목록 보기
714/751

😎풀이

  1. 남은 요소가 존재하는 동안 amount 순회
    1-1. 소요된 시간 추가
    1-2. 남은 요소를 내림차 순 정렬
    1-3. 가장 많이 남은 두 요소 1씩 제거
  2. 총 소요 시간 반환
function fillCups(amount: number[]): number {
    let time = 0
    let remain = [...amount]
    while(remain.some(a => a !== 0)) {
        time++
        remain = remain.toSorted((a, b) => b - a)
        if(remain[0]) remain[0]--
        if(remain[1]) remain[1]--
    }
    return time
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글