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 9519924

Browse files
author
konstantin
committed
Medium991 challenge
1 parent 92ca3d5 commit 9519924

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package medium
2+
3+
import GreedyTopic
4+
import MathTopic
5+
6+
7+
/**
8+
* 991. Broken Calculator
9+
* https://leetcode.com/problems/broken-calculator/
10+
*
11+
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
12+
multiply the number on display by 2, or
13+
subtract 1 from the number on display.
14+
Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
15+
BULLSHIT
16+
*/
17+
18+
class Medium991 : MathTopic, GreedyTopic {
19+
20+
fun brokenCalc(startValue: Int, target: Int): Int {
21+
var value = target
22+
var ans = 0
23+
while (value > startValue) {
24+
ans++
25+
value = if (value % 2 == 1) value + 1 else value / 2
26+
}
27+
return ans + startValue - value
28+
}
29+
}
30+
31+
fun main() {
32+
println(Medium991().brokenCalc(2, 3))
33+
println(Medium991().brokenCalc(5, 8))
34+
println(Medium991().brokenCalc(3, 10))
35+
}

0 commit comments

Comments
(0)

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