|
| 1 | +package breadth_first_search; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by gouthamvidyapradhan on 19/08/2018 |
| 7 | + * Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.) |
| 8 | + * |
| 9 | + * Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse). |
| 10 | + * |
| 11 | + * When you get an instruction "A", your car does the following: position += speed, speed *= 2. |
| 12 | + * |
| 13 | + * When you get an instruction "R", your car does the following: if your speed is positive then speed = -1 , |
| 14 | + * otherwise speed = 1. (Your position stays the same.) |
| 15 | + * |
| 16 | + * For example, after commands "AAR", your car goes to positions 0->1->3->3, and your speed goes to 1->2->4->-1. |
| 17 | + * |
| 18 | + * Now for some target position, say the length of the shortest sequence of instructions to get there. |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * Input: |
| 22 | + * target = 3 |
| 23 | + * Output: 2 |
| 24 | + * Explanation: |
| 25 | + * The shortest instruction sequence is "AA". |
| 26 | + * Your position goes from 0->1->3. |
| 27 | + * Example 2: |
| 28 | + * Input: |
| 29 | + * target = 6 |
| 30 | + * Output: 5 |
| 31 | + * Explanation: |
| 32 | + * The shortest instruction sequence is "AAARA". |
| 33 | + * Your position goes from 0->1->3->7->7->6. |
| 34 | + * |
| 35 | + * |
| 36 | + * Note: |
| 37 | + * |
| 38 | + * 1 <= target <= 10000. |
| 39 | + * |
| 40 | + * Solution: O(n log n) Do a BFS and visit every possible state. Prune the search space by avoiding negative vertices |
| 41 | + * and keep a boundary target of approximately (target * 2) - beyond this boundary target the race car should not |
| 42 | + * progress in the forward direction. |
| 43 | + */ |
| 44 | +public class RaceCar { |
| 45 | + |
| 46 | + /** |
| 47 | + * Main method |
| 48 | + * @param args |
| 49 | + */ |
| 50 | + public static void main(String[] args) { |
| 51 | + System.out.println(new RaceCar().racecar(1000)); |
| 52 | + } |
| 53 | + |
| 54 | + private class Node{ |
| 55 | + int v, s, d; |
| 56 | + Node(int v, int s, int d){ |
| 57 | + this.v = v; |
| 58 | + this.s = s; |
| 59 | + this.d = d; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean equals(Object o) { |
| 64 | + if (this == o) return true; |
| 65 | + if (!(o instanceof Node)) return false; |
| 66 | + Node node = (Node) o; |
| 67 | + return v == node.v && |
| 68 | + s == node.s; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int hashCode() { |
| 73 | + return Objects.hash(v, s); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public int racecar(int target) { |
| 78 | + if(target == 0) return 0; |
| 79 | + Queue<Node> queue = new ArrayDeque<>(); |
| 80 | + Set<Node> done = new HashSet<>(); |
| 81 | + Node start = new Node(0, 1, 0); |
| 82 | + done.add(start); |
| 83 | + queue.offer(start); |
| 84 | + while(!queue.isEmpty()){ |
| 85 | + Node curr = queue.poll(); |
| 86 | + if(curr.v < (target * 2)){ |
| 87 | + Node c1 = new Node(curr.v + curr.s, curr.s * 2, curr.d + 1); |
| 88 | + if(c1.v >= 0){ |
| 89 | + if(!done.contains(c1)){ |
| 90 | + queue.offer(c1); |
| 91 | + done.add(c1); |
| 92 | + if(target == c1.v){ |
| 93 | + return c1.d; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + Node c2 = new Node(curr.v, curr.s < 0 ? 1 : -1, curr.d + 1); |
| 99 | + if(!done.contains(c2)){ |
| 100 | + done.add(c2); |
| 101 | + queue.offer(c2); |
| 102 | + } |
| 103 | + } |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments