Skip to content

Commit e0670ee

Browse files
committed
add 752 java
1 parent 6653d80 commit e0670ee

File tree

5 files changed

+286
-9
lines changed

5 files changed

+286
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@
931931
666| [Path Sum IV](https://leetcode.com/problems/path-sum-iv/)| [Python](./leetcode_python/Breadth-First-Search/path-sum-iv.py) | _O(n)_ | _O(w)_ | Medium |🔒 Topological Sort | AGAIN* (3) (not start)
932932
742|[Closest Leaf in a Binary Tree](https://leetcode.com/problems/closest-leaf-in-a-binary-tree/)| [Python](./leetcode_python/Breadth-First-Search/closest-leaf-in-a-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/ClosestLeafInaBinaryTree.java) | _O(n)_ | _O(n)_ | Medium | AGAIN, Graph, bfs+dfs, search, `good trick`, `amazon`| AGAIN********** (6)
933933
743|[Network Delay Time](https://leetcode.com/problems/network-delay-time/)| [Python](./leetcode_python/Breadth-First-Search/network-delay-time.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/NetworkDelayTime.java) | _O(n)_ | _O(n)_ | Medium |Dijlstra, `google`| AGAIN **** (2)
934-
752|[Open the Lock](https://leetcode.com/problems/open-the-lock/)| [Python](./leetcode_python/Breadth-First-Search/open-the-lock.py) | _O(k * n^k + d)_ | _O(k * n^k + d)_ | Medium |LC 863 | AGAIN (not start)
934+
752|[Open the Lock](https://leetcode.com/problems/open-the-lock/)| [Python](./leetcode_python/Breadth-First-Search/open-the-lock.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/OpenTheLock.java)s | _O(k * n^k + d)_ | _O(k * n^k + d)_ | Medium |LC 863, google | AGAIN (not start)
935935
787|[Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| [Python](./leetcode_python/Breadth-First-Search/cheapest-flights-within-k-stops.py) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Medium | `Dijkstra's algorithm`, dfs, bfs, graph, priority queue,`amazon`, apple, google, airbnb | AGAIN****** (3) (not start)
936936
815|[Bus Routes](https://leetcode.com/problems/bus-routes/)| [Python](./leetcode_python/Breadth-First-Search/bus-routes.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/BusRoutes.java) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Hard | shortest route, graph, bfs,`amazon`, google| AGAIN**** (3)
937937
886|[Possible Bipartition](https://leetcode.com/problems/possible-bipartition/)| [Python](./leetcode_python/Breadth-First-Search/possible-bipartition.py) | _O(\|V\| + \|E\|)_ | _O(\|V\| + \|E\|)_| Medium |check `# 785 Is Graph Bipartite?`,`graph`, `AGAIN`,`union find` ,`fb` | AGAIN********** (6)

data/progress.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Progress
22

3+
# 2025-01-10
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
- TODO: go through `neetcode`, note/summary knowledges (by topic) at cheatsheet
6+
37
# 2025-01-09
48
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
59
- TODO: go through `neetcode`, note/summary knowledges (by topic) at cheatsheet

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20250110: 752,117
12
20250109: 279,904
23
20250108: 852,692
34
20250107: 247

data/to_review.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-03-06 -> ['752,117']
12
2025-03-05 -> ['279,904']
23
2025-03-04 -> ['852,692']
34
2025-03-03 -> ['247']
@@ -10,7 +11,7 @@
1011
2025-02-20 -> ['079,212(todo),362,849']
1112
2025-02-15 -> ['369,311']
1213
2025-02-14 -> ['370']
13-
2025-02-13 -> ['815,871,593,1109']
14+
2025-02-13 -> ['752,117', '815,871,593,1109']
1415
2025-02-12 -> ['279,904']
1516
2025-02-11 -> ['852,692']
1617
2025-02-10 -> ['247']
@@ -19,26 +20,26 @@
1920
2025-02-04 -> ['729,731']
2021
2025-02-03 -> ['769,817,855(again)']
2122
2025-02-01 -> ['498(again),934', '304,853,325']
22-
2025-01-31 -> ['379,173']
23+
2025-01-31 -> ['752,117', '379,173']
2324
2025-01-30 -> ['279,904', '079,212(todo),362,849']
2425
2025-01-29 -> ['852,692']
2526
2025-01-28 -> ['247']
2627
2025-01-26 -> ['399,1091', '370(todo)']
2728
2025-01-25 -> ['1031,369', '369,311']
2829
2025-01-24 -> ['370', '34,767']
29-
2025-01-23 -> ['815,871,593,1109']
30+
2025-01-23 -> ['752,117', '815,871,593,1109']
3031
2025-01-22 -> ['279,904', '729,731']
3132
2025-01-21 -> ['852,692', '769,817,855(again)']
3233
2025-01-20 -> ['247', '722,380']
3334
2025-01-19 -> ['498(again),934', '33,81']
34-
2025-01-18 -> ['399,1091', '379,173']
35+
2025-01-18 -> ['752,117', '399,1091', '379,173']
3536
2025-01-17 -> ['279,904', '1031,369', '079,212(todo),362,849', '560,523', '253']
3637
2025-01-16 -> ['852,692', '776,31']
37-
2025-01-15 -> ['247', '004(todo),34(todo),162(todo),275(todo)']
38+
2025-01-15 -> ['752,117', '247', '004(todo),34(todo),162(todo),275(todo)']
3839
2025-01-14 -> ['279,904', '729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
39-
2025-01-13 -> ['852,692', '399,1091', '769,817,855(again)']
40-
2025-01-12 -> ['279,904', '247', '1031,369', '369,311']
41-
2025-01-11 -> ['279,904', '852,692', '498(again),934', '370', '304,853,325', '394']
40+
2025-01-13 -> ['752,117', '852,692', '399,1091', '769,817,855(again)']
41+
2025-01-12 -> ['752,117', '279,904', '247', '1031,369', '369,311']
42+
2025-01-11 -> ['752,117', '279,904', '852,692', '498(again),934', '370', '304,853,325', '394']
4243
2025-01-10 -> ['279,904', '852,692', '247', '399,1091', '379,173', '815,871,593,1109', '833,950']
4344
2025-01-09 -> ['852,692', '247', '1031,369', '729,731', '079,212(todo),362,849']
4445
2025-01-08 -> ['247', '399,1091', '769,817,855(again)']
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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

Comments
 (0)