-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReachNumber.java
30 lines (26 loc) · 1.02 KB
/
ReachNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package leetcode.math;
public class ReachNumber {
public int reachNumber(int target) {
// The axis is symmetric.
long tar = Math.abs(target);
// Keep summing up: 1 + 2 + 3 + ... + n >= target
long n = (int) Math.ceil((Math.sqrt(tar * 8 + 1) - 1) / 2);
long sum = (n + 1) * n / 2;
if (sum == target) {
// Returns n if the sum is exactly the target.
return (int) n;
} else {
long difference = sum - target;
// We only need to flip one of [1, n] if the difference is even.
if (difference % 2 == 0) {
return (int) n;
} else if ((difference + n + 1) % 2 == 0) {
// Tries to add n + 1 first, if the new difference is even, then flip one of [1, n + 1].
return (int) (n + 1);
} else {
// Adds n + 1 and n + 2 first, then it is guaranteed that the difference is even.
return (int) (n + 2);
}
}
}
}