Skip to content

Commit 6c4193f

Browse files
author
yennj12
committed
add 752 java
1 parent 3953cf1 commit 6c4193f

File tree

3 files changed

+239
-31
lines changed

3 files changed

+239
-31
lines changed

leetcode_java/src/main/java/LeetCodeJava/BFS/OpenTheLock.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// https://leetcode.com/problems/open-the-lock/description/
44

5+
import dev.workspace6;
6+
57
import java.util.*;
68

79
/**
@@ -61,8 +63,92 @@
6163
public class OpenTheLock {
6264

6365
// V0
66+
// // TODO: fix below
67+
// public class Move{
68+
// // attr
69+
// int move;
70+
// String state;
71+
//
72+
// // constructor
73+
// public Move(String state, int move){
74+
// this.move = move;
75+
// this.state = state;
76+
// }
77+
//
78+
// // getter, setter
79+
// public int getMove() {
80+
// return move;
81+
// }
82+
//
83+
// public void setMove(int move) {
84+
// this.move = move;
85+
// }
86+
//
87+
// public String getState() {
88+
// return state;
89+
// }
90+
//
91+
// public void setState(String state) {
92+
// this.state = state;
93+
// }
94+
// }
6495
// public int openLock(String[] deadends, String target) {
6596
//
97+
// // edge
98+
// List<String> deadendsList = Arrays.asList(deadends);
99+
// if(deadendsList.contains("0000")){
100+
// return -1;
101+
// }
102+
//
103+
// if (deadends.length == 1){
104+
// if (deadends[0].equals("0000")){
105+
// return -1;
106+
// }
107+
// if (target.equals("0000")){
108+
// return 0;
109+
// }
110+
// // other cases will be processed below
111+
// }
112+
//
113+
// //List<String> deadendsList = Arrays.asList(deadends);
114+
//
115+
// String[] moves = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
116+
// //Map<String, List<String>> moves = new HashMap<>();
117+
// // moves.put("0", new ArrayList<>("1", "9"));
118+
// Queue<workspace6.Move> queue = new LinkedList<>();
119+
// String initState = "0000";
120+
// Set<String> visited = new HashSet<>();
121+
// queue.add(new workspace6.Move(initState, 0));
122+
//
123+
// while(!queue.isEmpty()){
124+
// workspace6.Move cur = queue.poll();
125+
// int move = cur.getMove();
126+
// String curState = cur.getState();
127+
// // if found, return directly, since we use BFS, it should be `shortest` move
128+
// if (curState.equals(target)){
129+
// return move;
130+
// }
131+
//
132+
// // ??? need to loop over idx ??? or we add "4 moved idx string" to queue at first
133+
// for(int i = 0; i < initState.length(); i++){
134+
// for (String x: moves){
135+
// String curNew = updateStringWithIdx(curState, x, i);
136+
// //boolean isEqaulOnIdx = curNew.charAt(i) == target.charAt(i);
137+
// if (!deadendsList.contains(curNew) && !visited.contains(curNew)){
138+
// // add to queue
139+
// visited.add(curNew);
140+
// queue.add(new workspace6.Move(curNew, move+1));
141+
// }
142+
// }
143+
// }
144+
// }
145+
//
146+
// return -1;
147+
// }
148+
//
149+
// private String updateStringWithIdx(String input, String newStr, int idx){
150+
// StringBuilder sb = new StringBuilder(input);
151+
// return sb.replace(idx, idx+1, newStr).toString();
66152
// }
67153

68154
// V1

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,4 +2484,112 @@ public List<String> topKFrequent(String[] words, int k) {
24842484
// return res;
24852485
// }
24862486

2487+
// LC 752
2488+
/**
2489+
* Given a target representing the value of the wheels that will unlock the lock,
2490+
*
2491+
*
2492+
* You are given a list of deadends dead ends,
2493+
* meaning if the lock displays any of these codes, the wheels of
2494+
* the lock will stop turning and you will be unable to open it.
2495+
*
2496+
* -> return the minimum total number of turns required to open the lock, or -1 if it is impossible.
2497+
*
2498+
*
2499+
* IDEA: BFS
2500+
*
2501+
* -> 1) for every idx, move its `digit` one by one via BFS
2502+
* if any combination in deadends, jump out that idx's BFS call,
2503+
* move the next idx
2504+
*
2505+
* -> 2) check if the `least move` exists, if not, return -1
2506+
*
2507+
*/
2508+
public class Move{
2509+
// attr
2510+
int move;
2511+
String state;
2512+
2513+
// constructor
2514+
public Move(String state, int move){
2515+
this.move = move;
2516+
this.state = state;
2517+
}
2518+
2519+
// getter, setter
2520+
public int getMove() {
2521+
return move;
2522+
}
2523+
2524+
public void setMove(int move) {
2525+
this.move = move;
2526+
}
2527+
2528+
public String getState() {
2529+
return state;
2530+
}
2531+
2532+
public void setState(String state) {
2533+
this.state = state;
2534+
}
2535+
}
2536+
public int openLock(String[] deadends, String target) {
2537+
2538+
// edge
2539+
List<String> deadendsList = Arrays.asList(deadends);
2540+
if(deadendsList.contains("0000")){
2541+
return -1;
2542+
}
2543+
2544+
if (deadends.length == 1){
2545+
if (deadends[0].equals("0000")){
2546+
return -1;
2547+
}
2548+
if (target.equals("0000")){
2549+
return 0;
2550+
}
2551+
// other cases will be processed below
2552+
}
2553+
2554+
//List<String> deadendsList = Arrays.asList(deadends);
2555+
2556+
String[] moves = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
2557+
//Map<String, List<String>> moves = new HashMap<>();
2558+
// moves.put("0", new ArrayList<>("1", "9"));
2559+
Queue<Move> queue = new LinkedList<>();
2560+
String initState = "0000";
2561+
Set<String> visited = new HashSet<>();
2562+
queue.add(new Move(initState, 0));
2563+
2564+
while(!queue.isEmpty()){
2565+
Move cur = queue.poll();
2566+
int move = cur.getMove();
2567+
String curState = cur.getState();
2568+
// if found, return directly, since we use BFS, it should be `shortest` move
2569+
if (curState.equals(target)){
2570+
return move;
2571+
}
2572+
2573+
// ??? need to loop over idx ??? or we add "4 moved idx string" to queue at first
2574+
for(int i = 0; i < initState.length(); i++){
2575+
for (String x: moves){
2576+
String curNew = updateStringWithIdx(curState, x, i);
2577+
//boolean isEqaulOnIdx = curNew.charAt(i) == target.charAt(i);
2578+
if (!deadendsList.contains(curNew) && !visited.contains(curNew)){
2579+
// add to queue
2580+
visited.add(curNew);
2581+
queue.add(new Move(curNew, move+1));
2582+
}
2583+
}
2584+
}
2585+
}
2586+
2587+
return -1;
2588+
}
2589+
2590+
private String updateStringWithIdx(String input, String newStr, int idx){
2591+
StringBuilder sb = new StringBuilder(input);
2592+
return sb.replace(idx, idx+1, newStr).toString();
2593+
}
2594+
24872595
}

leetcode_java/src/main/java/dev/workspace7.java

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,55 @@ public static void main(String[] args) {
6565
//
6666
// System.out.println(">>> (after) map = " + map);
6767

68+
//
69+
// // ------------------- TEST 6 : sort map on val, key len
70+
// Map<String, Integer> map = new HashMap<>();
71+
// map.put("apple", 3);
72+
// map.put("banana", 2);
73+
// map.put("lem", 3);
74+
// map.put("kiwi", 5);
75+
// map.put("peachhh", 3);
76+
//
77+
// System.out.println("Original map: " + map);
78+
//
79+
// //Map<String, Integer> sortedMap = sortMapByValueAndKeyLength(map);
80+
// //Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
81+
// // List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
82+
// //List<Set<Map.Entry<String, Integer>>> entryList = Arrays.asList(map.entrySet());
83+
// List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
84+
//
85+
// // sort map
86+
// Collections.sort(entryList, (Comparator<Map.Entry<String, Integer>>) (entry1, entry2) -> {
87+
// // if(o1.)
88+
// int valueCompare = entry2.getValue().compareTo(entry1.getValue());
89+
// if (valueCompare == 0) {
90+
// return entry1.getKey().length() - entry2.getKey().length();
91+
// }
92+
// return valueCompare;
93+
// });
94+
//
95+
//
96+
// // NOTE !!! use `LinkedHashMap` keep ordering in map
97+
// Map<String, Integer> sortedMap = new LinkedHashMap<>();
98+
// for (Map.Entry<String, Integer> entry : entryList) {
99+
// sortedMap.put(entry.getKey(), entry.getValue());
100+
// }
101+
//
102+
// System.out.println("Sorted map: " + sortedMap);
68103

69-
// ------------------- TEST 6 : sort map on val, key len
70-
Map<String, Integer> map = new HashMap<>();
71-
map.put("apple", 3);
72-
map.put("banana", 2);
73-
map.put("lem", 3);
74-
map.put("kiwi", 5);
75-
map.put("peachhh", 3);
76-
77-
System.out.println("Original map: " + map);
78-
79-
//Map<String, Integer> sortedMap = sortMapByValueAndKeyLength(map);
80-
//Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
81-
// List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
82-
//List<Set<Map.Entry<String, Integer>>> entryList = Arrays.asList(map.entrySet());
83-
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
84-
85-
// sort map
86-
Collections.sort(entryList, (Comparator<Map.Entry<String, Integer>>) (entry1, entry2) -> {
87-
// if(o1.)
88-
int valueCompare = entry2.getValue().compareTo(entry1.getValue());
89-
if (valueCompare == 0) {
90-
return entry1.getKey().length() - entry2.getKey().length();
91-
}
92-
return valueCompare;
93-
});
94104

95105

96-
// NOTE !!! use `LinkedHashMap` keep ordering in map
97-
Map<String, Integer> sortedMap = new LinkedHashMap<>();
98-
for (Map.Entry<String, Integer> entry : entryList) {
99-
sortedMap.put(entry.getKey(), entry.getValue());
100-
}
106+
// ------------------- TEST 7: replace idx in string
107+
String myStr = "amazon";
108+
System.out.println(myStr);
109+
System.out.println(updateStringWithIdx(myStr, "xxxx", 1));
110+
System.out.println(updateStringWithIdx(myStr, "xxxx", 0));
111+
}
112+
101113

102-
System.out.println("Sorted map: " + sortedMap);
114+
private static String updateStringWithIdx(String input, String newStr, int idx){
115+
StringBuilder sb = new StringBuilder(input);
116+
return sb.replace(idx, idx+1, newStr).toString();
103117
}
104118

105119

0 commit comments

Comments
 (0)