Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccf07f5

Browse files
[ACCEPTED][Problem 1974] Minimum Time to Type Word Using Special Typewriter
1 parent 4bd8de4 commit ccf07f5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎src/easy/MinimumTimeToTypeWordUsingSpecialTypewriter1974.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ package easy
22

33
object MinimumTimeToTypeWordUsingSpecialTypewriter1974 {
44
fun minTimeToType(word: String): Int {
5+
var minTime = word.length
6+
minTime += clockwiseDistance(word[0], 'a').coerceAtMost(counterClockwiseDistance(word[0], 'a'))
57

8+
for (i in 1 until word.length)
9+
{
10+
val clockwise = clockwiseDistance(word[i],word[i-1])
11+
val counterClockwise = counterClockwiseDistance(word[i],word[i-1])
12+
minTime+= clockwise.coerceAtMost(counterClockwise)
13+
}
14+
return minTime
15+
}
16+
17+
private fun counterClockwiseDistance(from: Char, to: Char): Int {
18+
val start = from.lowercaseChar() - 'a'
19+
val end = to.lowercaseChar() - 'a'
20+
return (start - end + 26) % 26
21+
}
22+
private fun clockwiseDistance(from: Char, to: Char): Int {
23+
val start = from.lowercaseChar() - 'a'
24+
val end = to.lowercaseChar() - 'a'
25+
return (end - start + 26) % 26
626
}
727
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /