Leetcode Note: Go - Minimum Time to Type Word Using Special Typewriter

Minimum Time to Type Word Using Special Typewriter - LeetCode
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/

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

回答

Golang 100% fastest 100% easiest - Minimum Time to Type Word Using Special Typewriter - LeetCode
https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/solutions/3273645/golang-100-fastest-100-easiest/

func minTimeToType(word string) (sum int) {
    cur := 0
    for _,v := range word {
        c := int(v - 97)
        sum += min(abs(c - cur), abs(abs(c - cur) - 26)) + 1
        cur = c
    }
    return sum
}
func min (a,b int) int {
    if a < b {return a }   
    return b
}
func abs(a int) int {
    if a < 0 { return -a} 
    return a
}