Leetcode Note: Go - Minimum Number of Operations to Convert Time

Minimum Number of Operations to Convert Time - LeetCode
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/

  • Go 言語で取り組んだメモ

回答

Solution in 6 languages | O(1) Time and O(1) Space Complexity |0ms | implementation | - Minimum Number of Operations to Convert Time - LeetCode
https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/solutions/1929316/solution-in-6-languages-o-1-time-and-o-1-space-complexity-0ms-implementation/

func HHMMToMinutes(s string) int{
    sr := strings.Split(s,":")
    hrs,_ := strconv.Atoi(sr[0])
    minutes,_ := strconv.Atoi(sr[1]) 
    return  hrs*60 + minutes 
}

func convertTime(current string, correct string) int {
    diff := HHMMToMinutes(correct) - HHMMToMinutes(current)
    order := [4]int{60,15,5,1}
    ops := 0
    for i := 0 ; i < 4 ; i++ {
        ops+=(diff/order[i])
        diff%=order[i]
    }
    return ops
}