LeetCode: Broken Calculator Solution
Approach
Work backward
Implementation
1/**2 * @param {number} X3 * @param {number} Y4 * @return {number}5 */6var brokenCalc = function (X, Y) {7 let res = 08 while (Y > X) {9 res++10 if (Y % 2 === 0) {11 Y /= 212 } else {13 Y++14 }15 }16 res += X - Y1718 return res19}
Comments
Loading comments...
Tags
leetcode
math
greedy
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.