We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 92ca3d5 commit 9519924Copy full SHA for 9519924
src/medium/991. Broken Calculator .kt
@@ -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
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments