On a broken calculator that has a number showing on its display, we can perform two operations:
- Double: Multiply the number on the display by 2, or;
- Decrement: Subtract 1 from the number on the display.
Initially, the calculator is displaying the number X
.
Return the minimum number of operations needed to display the number Y
.
Example 1:
Input: X = 2, Y = 3
Output: 2
Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
Example 2:
Input: X = 5, Y = 8
Output: 2
Explanation: Use decrement and then double {5 -> 4 -> 8}.
Example 3:
Input: X = 3, Y = 10
Output: 3
Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
Example 4:
Input: X = 1024, Y = 1
Output: 1023
Explanation: Use decrement operations 1023 times.
Note:
1 <= X <= 10^9
1 <= Y <= 10^9
這道題說是有一個壞了的計算器,其實就顯示一個數字X,現在我們有兩種操作,一種乘以2操作,一種是減1操作,問最少需要多少次操作才能得到目標數字Y。好,現在來分析,由於X和Y的大小關系並不確定,最簡單的當然是X和Y相等,就不需要另外的操作了。當X大於Y時,由於都是正數,肯定就不能再乘2了,所以此時直接就可以返回 X-Y。比較復雜的情況就是Y大於X的情況,此時X既可以減1,又可以乘以2,但是仔細想想,我們的最終目的應該是要減小Y,直至其小於等於X,就可以直接得到結果。這里X乘以2的效果就相當於Y除以2,操作數都一樣,但是Y除以2時還要看Y的奇偶性,如果Y是偶數,那么 OK,可以直接除以2,若是奇數,需要將其變為偶數,由於X可以減1,等價過來就是Y加1,所以思路就有了,當Y大於X時進行循環,然后判斷Y的奇偶性,若是偶數,直接除以2,若是奇數,則加1,當然此時結果 res 也要對應增加。循環退出后,還要加上 X-Y 的值即可,參見代碼如下:
解法一:
class Solution {
public:
int brokenCalc(int X, int Y) {
int res = 0;
while (Y > X) {
Y = (Y % 2 == 0) ? Y / 2 : Y + 1;
++res;
}
return res + X - Y;
}
};
若用遞歸來寫就相當的簡潔了,可以兩行搞定,當然若你夠 geek 的話,也可以壓縮到一行,參見代碼如下:
解法二:
class Solution {
public:
int brokenCalc(int X, int Y) {
if (X >= Y) return X - Y;
return (Y % 2 == 0) ? (1 + brokenCalc(X, Y / 2)) : (1 + brokenCalc(X, Y + 1));
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/991
類似題目:
參考資料:
https://leetcode.com/problems/broken-calculator/