Skip to content

Commit bb0ede1

Browse files
committed
New Problem Solution "Broken Calculator"
1 parent 467fd31 commit bb0ede1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LeetCode
1111
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/) | [C++](./algorithms/cpp/matrixCellsInDistanceOrder/MatrixCellsInDistanceOrder.cpp)|Easy|
1212
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling/) | [C++](./algorithms/cpp/twoCityScheduling/TwoCityScheduling.cpp)|Easy|
1313
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [C++](./algorithms/cpp/cousinsInBinaryTree/CousinsInBinaryTree.cpp)|Easy|
14+
|991|[Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [C++](./algorithms/cpp/brokenCalculator/BrokenCalculator.cpp)|Medium|
1415
|990|[Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/) | [C++](./algorithms/cpp/satisfiabilityOfEqualityEquations/SatisfiabilityOfEqualityEquations.cpp)|Medium|
1516
|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [C++](./algorithms/cpp/addToArrayFormOfInteger/AddToArrayFormOfInteger.cpp)|Easy|
1617
|988|[Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/) | [C++](./algorithms/cpp/smallestStringStartingFromLeaf/SmallestStringStartingFromLeaf.cpp)|Medium|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Source : https://leetcode.com/problems/broken-calculator/
2+
// Author : Hao Chen
3+
// Date : 2019-05-01
4+
5+
/*****************************************************************************************************
6+
*
7+
* On a broken calculator that has a number showing on its display, we can perform two operations:
8+
*
9+
* Double: Multiply the number on the display by 2, or;
10+
* Decrement: Subtract 1 from the number on the display.
11+
*
12+
* Initially, the calculator is displaying the number X.
13+
*
14+
* Return the minimum number of operations needed to display the number Y.
15+
*
16+
* Example 1:
17+
*
18+
* Input: X = 2, Y = 3
19+
* Output: 2
20+
* Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
21+
*
22+
* Example 2:
23+
*
24+
* Input: X = 5, Y = 8
25+
* Output: 2
26+
* Explanation: Use decrement and then double {5 -> 4 -> 8}.
27+
*
28+
* Example 3:
29+
*
30+
* Input: X = 3, Y = 10
31+
* Output: 3
32+
* Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
33+
*
34+
* Example 4:
35+
*
36+
* Input: X = 1024, Y = 1
37+
* Output: 1023
38+
* Explanation: Use decrement operations 1023 times.
39+
*
40+
* Note:
41+
*
42+
* 1 <= X <= 10^9
43+
* 1 <= Y <= 10^9
44+
******************************************************************************************************/
45+
46+
47+
class Solution {
48+
public:
49+
int brokenCalc(int X, int Y) {
50+
if (X >= Y) return X-Y ;
51+
if ( Y%2 ==0 ) return brokenCalc(X, Y/2) + 1;
52+
return brokenCalc(X, Y+1) + 1;
53+
}
54+
};

0 commit comments

Comments
 (0)