Related: Minimum cost to reach a point N from 0 with two different operations allowed
https://leetcode.com/problems/reach-a-number/
X. https://leetcode.com/problems/reach-a-number/discuss/112968/Short-JAVA-Solution-with-Explanation
https://leetcode.com/articles/reach-a-number/
1 you go to the right and reach the goal exactly.
2 you go over the goal, if you over by even number, then you can choose one of the steps that goes right to go left (the step is the half of what you go over), if you go over by odd number, then keep going until you are over by even number.
https://leetcode.com/problems/reach-a-number/
You are standing at position
0
on an infinite number line. There is a goal at position target
.
On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.
Return the minimum number of steps required to reach the destination.
Example 1:
Input: target = 3 Output: 2 Explanation: On the first move we step from 0 to 1. On the second step we step from 1 to 3.
Example 2:
Input: target = 2 Output: 3 Explanation: On the first move we step from 0 to 1. On the second move we step from 1 to -1. On the third move we step from -1 to 2.
Note:
target
will be a non-zero integer in the range [-10^9, 10^9]
X. https://leetcode.com/problems/reach-a-number/discuss/112968/Short-JAVA-Solution-with-Explanation
Step 0: Get positive
Step 1: Find the smallest
Step 2: Find the difference between
Step 2.1: If the difference value is even, we can return the current
Step 2.2: If the difference value is odd, we need to increase the step until the difference is even (at most 2 more steps needed).
Eg:
Step 0:
Step 1:
Step 2: Difference =
Step 2.2: We need to increase
target
value (step
to get negative target
is the same as to get positive value due to symmetry).Step 1: Find the smallest
step
that the summation from 1
to step
just exceeds or equalstarget
.Step 2: Find the difference between
sum
and target
. The goal is to get rid of the difference to reach target
. For ith
move, if we switch the right move to the left, the change in summation will be 2*i
less. Now the difference between sum
and target
has to be an even number in order for the math to check out.Step 2.1: If the difference value is even, we can return the current
step
.Step 2.2: If the difference value is odd, we need to increase the step until the difference is even (at most 2 more steps needed).
Eg:
target = 5
Step 0:
target = 5
.Step 1:
sum = 1 + 2 + 3 = 6 > 5
, step = 3
.Step 2: Difference =
6 - 5 = 1
. Since the difference is an odd value, we will not reach the target by switching any right move to the left. So we increase our step
.Step 2.2: We need to increase
step
by 2 to get an even difference (i.e. 1 + 2 + 3 + 4 + 5 = 15
, now step = 5
, difference = 15 - 5 = 10
). Now that we have an even difference, we can simply switch any move to the left (i.e. change +
to -
) as long as the summation of the changed value equals to half of the difference. We can switch 1 and 4 or 2 and 3 or 5.class Solution {
public int reachNumber(int target) {
target = Math.abs(target);
int step = 0;
int sum = 0;
while (sum < target) {
step++;
sum += step;
}
while ((sum - target) % 2 != 0) {
step++;
sum += step;
}
return step;
}
https://leetcode.com/articles/reach-a-number/
The crux of the problem is to put
+
and -
signs on the numbers 1, 2, 3, ..., k
so that the sum is target
.
When
target < 0
and we made a sum of target
, we could switch the signs of all the numbers so that it equals Math.abs(target)
. Thus, the answer for target
is the same as Math.abs(target)
, and so without loss of generality, we can consider only target > 0
.
Now let's say
k
is the smallest number with S = 1 + 2 + ... + k >= target
. If S == target
, the answer is clearly k
.
If
S > target
, we need to change some number signs. If delta = S - target
is even, then we can always find a subset of {1, 2, ..., k}
equal to delta / 2
and switch the signs, so the answer is k
. (This depends on T = delta / 2
being at most S
.) [The proof is simple: either T <= k
and we choose it, or we choose k
in our subset and try to solve the same instance of the problem for T -= k
and the set {1, 2, ..., k-1}
.]
Otherwise, if
delta
is odd, we can't do it, as every sign change from positive to negative changes the sum by an even number. So let's consider a candidate answer of k+1
, which changes delta
by k+1
. If this is odd, then delta
will be even and we can have an answer of k+1
. Otherwise, delta
will be odd, and we will have an answer of k+2
.
For concrete examples of the above four cases, consider the following:
- If
target = 3
, thenk = 2, delta = 0
and the answer isk = 2
. - If
target = 4
, thenk = 3, delta = 2
, delta is even and the answer isk = 3
. - If
target = 7
, thenk = 4, delta = 3
, delta is odd and addingk+1
makes delta even. The answer isk+1 = 5
. - If
target = 5
, thenk = 3, delta = 1
, delta is odd and addingk+1
keeps delta odd. The answer isk+2 = 5
.
Algorithm
Subtract
Time Complexity: . Our while loop needs this many steps, as .++k
from target
until it goes non-positive. Then k
will be as described, and target
will be delta
as described. We can output the four cases above: if delta
is even then the answer is k
, if delta
is odd then the answer is k+1
or k+2
depending on the parity of k
.
public int reachNumber(int target) {
target = Math.abs(target);
int k = 0;
while (target > 0)
target -= ++k;
return target % 2 == 0 ? k : k + 1 + k % 2;
}
https://leetcode.com/problems/reach-a-number/discuss/112992/Learn-from-other-with-my-explanations.1 you go to the right and reach the goal exactly.
2 you go over the goal, if you over by even number, then you can choose one of the steps that goes right to go left (the step is the half of what you go over), if you go over by odd number, then keep going until you are over by even number.