[LeetCode] 2224. Minimum Number of Operations to Convert Time

Chobby·3일 전
1

LeetCode

목록 보기
691/710

😎풀이

  1. 분 단위로 변환
  2. 두 시간의 차이 확인
  3. 60, 15, 5, 1순으로 탐색하며 변환
  4. 변환에 필요한 횟수 반환
function convertTime(current: string, correct: string): number {
    const curMin = convertMin(current)
    const corMin = convertMin(correct)
    let gap = corMin - curMin
    let count = 0
    if(gap >= 60) {
        count += Math.floor(gap / 60)
        gap %= 60
    }
    if(gap >= 15) {
        count += Math.floor(gap / 15)
        gap %= 15
    }
    if(gap >= 5) {
        count += Math.floor(gap / 5)
        gap %= 5
    }
    if(gap >= 1) {
        count += Math.floor(gap / 1)
        gap %= 1
    }
    return count
};

function convertMin(time: string) {
    const [hour, min] = time.split(":").map(Number)
    return min + hour * 60
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글