-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRace Car
More file actions
38 lines (30 loc) · 978 Bytes
/
Race Car
File metadata and controls
38 lines (30 loc) · 978 Bytes
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
31
32
33
34
35
36
37
38
package race.car;
import java.util.*;
public class Solution {
public static int racecar(int target) {
int[] solution = new int[target + 3];
Arrays.fill(solution, Integer.MAX_VALUE);
solution[0] = 0;
solution[1] = 1;
// 0 1 3 3 2
solution[2] = 4;
for (int position = 3; position <= target; position++) {
int ceil = (int) Math.ceil(Math.log(position + 1) / Math.log(2));
int floor = (int) Math.floor(Math.log(position + 1) / Math.log(2));
if (ceil == floor) {
solution[position] = floor;
continue;
}
for (int j = 0; j <= floor; j++) {
// to arrive position : (2^floor + R + 2^j + R) + (rest of position)
solution[position] = Math.min(solution[position],
solution[position - ((1 << floor) - (1 << j))] + floor + 1 + j + 1);
}
if ((1 << ceil) / 2 < position + 1) {
solution[position] = Math.min(solution[position],
solution[(1 << ceil) - 1 - position] + ceil + 1);
}
}
return solution[target];
}
}