Wednesday, May 25, 2016

the less times you should press the button

https://discuss.leetcode.com/topic/91/the-less-times-you-should-press-the-button
There is a machine. It has two buttons. They are blue button and red button. The two buttons has the function as follow:
• when you press the red button, the number on the screen minus 1;
• when you press the blue button, the number on the screen multiplied 2.
Now give you two numbers m and n, output the less times you shoud press that change the number m to n(while m and n are integers above zero)
num

Here are my obseravations. When m > n is triavial, cost is m-n. Let's assume m < n
Brute force is not applicable because search space is exponential. We cannot afford to
generate all possible numbers to find the shortest path from m to n.
For example if we have m = 17 , n = 80
state tree would look something like this :
-------------------------------17--------------
--------------16----------------------------------34
-----15-------------32------------------33---------68
-14---30--------31--64---------32---66-----67---136
-----------------------------and--so on---------------------------------
Initially, I mislead myself that it is DP, but the DP properties don't conform. I start thinking for greedy approach taking into consideration that one function is linear (Subraction), the other exponetial(Multiplication by 2). The idea is to start with Mult operations if it is possible, if not start with Sub operations and one Mult at the end.
We have only two actions avaiblabe - multiplicaton by 2 (Mult) and substraction by 1 (Sub). I noticed that we have max cost - k+ (X -n) ,where :
X - is the least number so that m*2^k= X and X>=n . X is equal to m*2^(log(n/m)+1).
X - n - number of Div operations to reach N from X
k - number of Mult operations, to get X
We don't need to concern cost greater than k+ (X -n). The question is : Could we improve cost to get lower than it?
Example :
m = 14
n = 100
X = 112, k = 3 (number of Mult operations), (X - n) = 112 - 100 = 12. Max cost is 3+12 = 15
Let's try to decrease maxCost. This could be done as we do one Sub operation. Each time we do Sub operation , number X comes close to n with 2^k, till X >= n.
You can check it that X in first line is 112 and in the second row is 104 , with 8 less than first which 2^k
14,28,56, 112
13,26,52,104
Here you can see that X is less that n 96<100, new X = 192 is with k = 4
12,24,48,96,192
So we can't find any improvement and stop. Actually if we continue k becomes 4 , not 3 and we have
11,22,44,88,176
10,20,40,80,160
9,18,36,72,144
8,16,32,64,128
7,14,28,56,112 , the same as (14,28,56, 112)  from where we started
Our optimal cost is 1 (1 Sub op from 14 to 13) + 3 (3 Mult op) + 4 (4 Sub op from 104 to 100) = 8
What happens in case :
m = 1000
n = 1002
We see that X will be 2000, so we start with Sub operations (we do not need Mult), because with Mult we cannot get closer to n. We decrease m till X is the least number closer to the n.
In our case there will be do 499 Sub and 1 Mult to get 1002 cost = 500

But how to prove no other better sequence exists than the current?