|
| 1 | +package LeetCodeJava.BFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/open-the-lock/description/ |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * 752. Open the Lock |
| 9 | + * Medium |
| 10 | + * Topics |
| 11 | + * Companies |
| 12 | + * Hint |
| 13 | + * You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot. |
| 14 | + * |
| 15 | + * The lock initially starts at '0000', a string representing the state of the 4 wheels. |
| 16 | + * |
| 17 | + * You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it. |
| 18 | + * |
| 19 | + * Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible. |
| 20 | + * |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202" |
| 26 | + * Output: 6 |
| 27 | + * Explanation: |
| 28 | + * A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202". |
| 29 | + * Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid, |
| 30 | + * because the wheels of the lock become stuck after the display becomes the dead end "0102". |
| 31 | + * Example 2: |
| 32 | + * |
| 33 | + * Input: deadends = ["8888"], target = "0009" |
| 34 | + * Output: 1 |
| 35 | + * Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009". |
| 36 | + * Example 3: |
| 37 | + * |
| 38 | + * Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888" |
| 39 | + * Output: -1 |
| 40 | + * Explanation: We cannot reach the target without getting stuck. |
| 41 | + * |
| 42 | + * |
| 43 | + * Constraints: |
| 44 | + * |
| 45 | + * 1 <= deadends.length <= 500 |
| 46 | + * deadends[i].length == 4 |
| 47 | + * target.length == 4 |
| 48 | + * target will not be in the list deadends. |
| 49 | + * target and deadends[i] consist of digits only. |
| 50 | + * Seen this question in a real interview before? |
| 51 | + * 1/5 |
| 52 | + * Yes |
| 53 | + * No |
| 54 | + * Accepted |
| 55 | + * 341.9K |
| 56 | + * Submissions |
| 57 | + * 564.9K |
| 58 | + * Acceptance Rate |
| 59 | + * 60.5% |
| 60 | + */ |
| 61 | +public class OpenTheLock { |
| 62 | + |
| 63 | + // V0 |
| 64 | +// public int openLock(String[] deadends, String target) { |
| 65 | +// |
| 66 | +// } |
| 67 | + |
| 68 | + // V1 |
| 69 | + // https://leetcode.com/problems/open-the-lock/editorial/ |
| 70 | + // IDEA: BFS |
| 71 | + public int openLock_1(String[] deadends, String target) { |
| 72 | + |
| 73 | +// // Map the next slot digit for each current slot digit. |
| 74 | +// Map<Character, Character> nextSlot = Map.of( |
| 75 | +// '0', '1', |
| 76 | +// '1', '2', |
| 77 | +// '2', '3', |
| 78 | +// '3', '4', |
| 79 | +// '4', '5', |
| 80 | +// '5', '6', |
| 81 | +// '6', '7', |
| 82 | +// '7', '8', |
| 83 | +// '8', '9', |
| 84 | +// '9', '0'); |
| 85 | +// // Map the previous slot digit for each current slot digit. |
| 86 | +// Map<Character, Character> prevSlot = Map.of( |
| 87 | +// '0', '9', |
| 88 | +// '1', '0', |
| 89 | +// '2', '1', |
| 90 | +// '3', '2', |
| 91 | +// '4', '3', |
| 92 | +// '5', '4', |
| 93 | +// '6', '5', |
| 94 | +// '7', '6', |
| 95 | +// '8', '7', |
| 96 | +// '9', '8'); |
| 97 | + |
| 98 | + // Map the next slot digit for each current slot digit. |
| 99 | + Map<Character, Character> nextSlot = new HashMap<>(); |
| 100 | + nextSlot.put('0', '1'); |
| 101 | + nextSlot.put('1', '2'); |
| 102 | + nextSlot.put('2', '3'); |
| 103 | + nextSlot.put('3', '4'); |
| 104 | + nextSlot.put('4', '5'); |
| 105 | + nextSlot.put('5', '6'); |
| 106 | + nextSlot.put('6', '7'); |
| 107 | + nextSlot.put('7', '8'); |
| 108 | + nextSlot.put('8', '9'); |
| 109 | + nextSlot.put('9', '0'); |
| 110 | + |
| 111 | + // Map the previous slot digit for each current slot digit. |
| 112 | + Map<Character, Character> prevSlot = new HashMap<>(); |
| 113 | + prevSlot.put('0', '9'); |
| 114 | + prevSlot.put('1', '0'); |
| 115 | + prevSlot.put('2', '1'); |
| 116 | + prevSlot.put('3', '2'); |
| 117 | + prevSlot.put('4', '3'); |
| 118 | + prevSlot.put('5', '4'); |
| 119 | + prevSlot.put('6', '5'); |
| 120 | + prevSlot.put('7', '6'); |
| 121 | + prevSlot.put('8', '7'); |
| 122 | + prevSlot.put('9', '8'); |
| 123 | + |
| 124 | + // Set to store visited and dead-end combinations. |
| 125 | + Set<String> visitedCombinations = new HashSet<>(Arrays.asList(deadends)); |
| 126 | + // Queue to store combinations generated after each turn. |
| 127 | + Queue<String> pendingCombinations = new LinkedList<String>(); |
| 128 | + |
| 129 | + // Count the number of wheel turns made. |
| 130 | + int turns = 0; |
| 131 | + |
| 132 | + // If the starting combination is also a dead-end, |
| 133 | + // then we can't move from the starting combination. |
| 134 | + if (visitedCombinations.contains("0000")) { |
| 135 | + return -1; |
| 136 | + } |
| 137 | + |
| 138 | + // Start with the initial combination '0000'. |
| 139 | + pendingCombinations.add("0000"); |
| 140 | + visitedCombinations.add("0000"); |
| 141 | + |
| 142 | + while (!pendingCombinations.isEmpty()) { |
| 143 | + // Explore all the combinations of the current level. |
| 144 | + int currLevelNodesCount = pendingCombinations.size(); |
| 145 | + for (int i = 0; i < currLevelNodesCount; i++) { |
| 146 | + // Get the current combination from the front of the queue. |
| 147 | + String currentCombination = pendingCombinations.poll(); |
| 148 | + |
| 149 | + // If the current combination matches the target, |
| 150 | + // return the number of turns/level. |
| 151 | + if (currentCombination.equals(target)) { |
| 152 | + return turns; |
| 153 | + } |
| 154 | + |
| 155 | + // Explore all possible new combinations by turning each wheel in both |
| 156 | + // directions. |
| 157 | + for (int wheel = 0; wheel < 4; wheel += 1) { |
| 158 | + // Generate the new combination by turning the wheel to the next digit. |
| 159 | + StringBuilder newCombination = new StringBuilder(currentCombination); |
| 160 | + newCombination.setCharAt(wheel, nextSlot.get(newCombination.charAt(wheel))); |
| 161 | + // If the new combination is not a dead-end and was never visited, |
| 162 | + // add it to the queue and mark it as visited. |
| 163 | + if (!visitedCombinations.contains(newCombination.toString())) { |
| 164 | + pendingCombinations.add(newCombination.toString()); |
| 165 | + visitedCombinations.add(newCombination.toString()); |
| 166 | + } |
| 167 | + |
| 168 | + // Generate the new combination by turning the wheel to the previous digit. |
| 169 | + newCombination = new StringBuilder(currentCombination); |
| 170 | + newCombination.setCharAt(wheel, prevSlot.get(newCombination.charAt(wheel))); |
| 171 | + // If the new combination is not a dead-end and is never visited, |
| 172 | + // add it to the queue and mark it as visited. |
| 173 | + if (!visitedCombinations.contains(newCombination.toString())) { |
| 174 | + pendingCombinations.add(newCombination.toString()); |
| 175 | + visitedCombinations.add(newCombination.toString()); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + // We will visit next-level combinations. |
| 180 | + turns += 1; |
| 181 | + } |
| 182 | + // We never reached the target combination. |
| 183 | + return -1; |
| 184 | + } |
| 185 | + |
| 186 | + // V2 |
| 187 | + // https://leetcode.com/problems/open-the-lock/solutions/5057217/java-solution-by-archivebizzle-dawj/ |
| 188 | + public int openLock_2(String[] deadends, String target) { |
| 189 | + Set<String> seen = new HashSet<>(Arrays.asList(deadends)); |
| 190 | + if (seen.contains("0000")) |
| 191 | + return -1; |
| 192 | + if (target.equals("0000")) |
| 193 | + return 0; |
| 194 | + |
| 195 | + int ans = 0; |
| 196 | + Queue<String> q = new ArrayDeque<>(Arrays.asList("0000")); |
| 197 | + |
| 198 | + while (!q.isEmpty()) { |
| 199 | + ++ans; |
| 200 | + for (int sz = q.size(); sz > 0; --sz) { |
| 201 | + StringBuilder sb = new StringBuilder(q.poll()); |
| 202 | + for (int i = 0; i < 4; ++i) { |
| 203 | + final char cache = sb.charAt(i); |
| 204 | + // Increase the i-th digit by 1. |
| 205 | + sb.setCharAt(i, sb.charAt(i) == '9' ? '0' : (char) (sb.charAt(i) + 1)); |
| 206 | + String word = sb.toString(); |
| 207 | + if (word.equals(target)) |
| 208 | + return ans; |
| 209 | + if (!seen.contains(word)) { |
| 210 | + q.offer(word); |
| 211 | + seen.add(word); |
| 212 | + } |
| 213 | + sb.setCharAt(i, cache); |
| 214 | + // Decrease the i-th digit by 1. |
| 215 | + sb.setCharAt(i, sb.charAt(i) == '0' ? '9' : (char) (sb.charAt(i) - 1)); |
| 216 | + word = sb.toString(); |
| 217 | + if (word.equals(target)) |
| 218 | + return ans; |
| 219 | + if (!seen.contains(word)) { |
| 220 | + q.offer(word); |
| 221 | + seen.add(word); |
| 222 | + } |
| 223 | + sb.setCharAt(i, cache); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return -1; |
| 229 | + } |
| 230 | + |
| 231 | + // V4 |
| 232 | + // https://leetcode.com/problems/open-the-lock/solutions/5057116/fasterlesserdetailed-explainationbfsstep-vsum/ |
| 233 | +// public int openLock_4(String[] deadends, String target) { |
| 234 | +// Set<String> deadendSet = new HashSet<>(Arrays.asList(deadends)); |
| 235 | +// if (deadendSet.contains("0000")) { |
| 236 | +// return -1; |
| 237 | +// } |
| 238 | +// |
| 239 | +// Queue<Pair<String, Integer>> queue = new LinkedList<>(); |
| 240 | +// queue.offer(new Pair<>("0000", 0)); |
| 241 | +// Set<String> visited = new HashSet<>(); |
| 242 | +// visited.add("0000"); |
| 243 | +// |
| 244 | +// while (!queue.isEmpty()) { |
| 245 | +// Pair<String, Integer> current = queue.poll(); |
| 246 | +// String currentCombination = current.getKey(); |
| 247 | +// int moves = current.getValue(); |
| 248 | +// |
| 249 | +// if (currentCombination.equals(target)) { |
| 250 | +// return moves; |
| 251 | +// } |
| 252 | +// |
| 253 | +// for (int i = 0; i < 4; i++) { |
| 254 | +// for (int delta : new int[] { -1, 1 }) { |
| 255 | +// int newDigit = (currentCombination.charAt(i) - '0' + delta + 10) % 10; |
| 256 | +// String newCombination = currentCombination.substring(0, i) + |
| 257 | +// newDigit + |
| 258 | +// currentCombination.substring(i + 1); |
| 259 | +// |
| 260 | +// if (!visited.contains(newCombination) && !deadendSet.contains(newCombination)) { |
| 261 | +// visited.add(newCombination); |
| 262 | +// queue.offer(new Pair<>(newCombination, moves + 1)); |
| 263 | +// } |
| 264 | +// } |
| 265 | +// } |
| 266 | +// } |
| 267 | +// |
| 268 | +// return -1; // Target is not reachable |
| 269 | +// } |
| 270 | + |
| 271 | +} |
0 commit comments