Created
March 25, 2022 17:20
-
-
Save esase/f9d0e6d453e9d51201546d66796a3556 to your computer and use it in GitHub Desktop.
Broken calculator [https://leetcode.com/problems/broken-calculator]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} startValue | |
* @param {number} target | |
* @return {number} | |
*/ | |
var brokenCalc = function(startValue, target) { | |
let steps = 0; | |
while (startValue < target) { | |
steps++; | |
if (target % 2) { | |
target++; | |
continue; | |
} | |
target /= 2; | |
} | |
// process the rest | |
return startValue - target + steps; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment