Skip to content

Commit

Permalink
add 753 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 18, 2024
1 parent f9de13b commit 814c68f
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@
549 | [Binary Tree Longest Consecutive Sequence II](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/) | [Python](./leetcode_python/Recursion/binary-tree-longest-consecutive-sequence-ii.py) | _O(n)_ | _O(h)_ | Medium |🔒| AGAIN (not start)
669| [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree/) | [Python](./leetcode_python/Recursion/trim-a-binary-search-tree.py) | _O(n)_ | _O(h)_ | Medium |good basic, BST, dfs, recursion,`amazon`| AGAIN******* (4)
671| [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) |[Python](./leetcode_python/Recursion/second-minimum-node-in-a-binary-tree.py) | _O(n)_ | _O(h)_ | Easy|dfs,tree| OK* (2)

753| [Cracking the Safes](https://leetcode.com/problems/cracking-the-safe/description/) |[Java](./leetcode_java/src/main/java/LeetCodeJava/Recursion/CrackingTheSafes.java) | _O(n)_| _O(h)_ | Hard|dfs,recursion,google| AGAIN (not start)

## Binary Search

Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-09-18
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-09-17
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240918: 753
20240917: 727
20240910: 659
20240909: 801,552
Expand Down
14 changes: 9 additions & 5 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
2024-11-12 -> ['753']
2024-11-11 -> ['727']
2024-11-04 -> ['659']
2024-11-03 -> ['801,552']
2024-11-02 -> ['1057,1066,1110']
2024-10-25 -> ['1110, 1055']
2024-10-22 -> ['753']
2024-10-21 -> ['727']
2024-10-18 -> ['359,1057,1055(todo)']
2024-10-14 -> ['659']
2024-10-13 -> ['801,552']
2024-10-12 -> ['1057,1066,1110']
2024-10-09 -> ['753']
2024-10-08 -> ['727']
2024-10-04 -> ['315', '1110, 1055']
2024-10-01 -> ['659']
2024-10-01 -> ['753', '659']
2024-09-30 -> ['727', '801,552']
2024-09-29 -> ['1057,1066,1110']
2024-09-27 -> ['359,1057,1055(todo)']
2024-09-26 -> ['753']
2024-09-25 -> ['727']
2024-09-23 -> ['659']
2024-09-23 -> ['753', '659']
2024-09-22 -> ['727', '801,552']
2024-09-21 -> ['1057,1066,1110', '1110, 1055']
2024-09-20 -> ['727']
2024-09-19 -> ['727']
2024-09-21 -> ['753', '1057,1066,1110', '1110, 1055']
2024-09-20 -> ['753', '727']
2024-09-19 -> ['753', '727']
2024-09-18 -> ['727', '659']
2024-09-17 -> ['801,552']
2024-09-16 -> ['1057,1066,1110']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package LeetCodeJava.Recursion;

// https://leetcode.com/problems/cracking-the-safe/

import java.util.HashSet;
import java.util.Set;

/**
*
* 753. Cracking the Safe
* Hard
* Topics
* Companies
* Hint
* There is a safe protected by a password. The password is a sequence of n digits where each digit can be in the range [0, k - 1].
*
* The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the most recent n digits that were entered each time you type a digit.
*
* For example, the correct password is "345" and you enter in "012345":
* After typing 0, the most recent 3 digits is "0", which is incorrect.
* After typing 1, the most recent 3 digits is "01", which is incorrect.
* After typing 2, the most recent 3 digits is "012", which is incorrect.
* After typing 3, the most recent 3 digits is "123", which is incorrect.
* After typing 4, the most recent 3 digits is "234", which is incorrect.
* After typing 5, the most recent 3 digits is "345", which is correct and the safe unlocks.
* Return any string of minimum length that will unlock the safe at some point of entering it.
*
*
*
* Example 1:
*
* Input: n = 1, k = 2
* Output: "10"
* Explanation: The password is a single digit, so enter each digit. "01" would also unlock the safe.
* Example 2:
*
* Input: n = 2, k = 2
* Output: "01100"
* Explanation: For each possible password:
* - "00" is typed in starting from the 4th digit.
* - "01" is typed in starting from the 1st digit.
* - "10" is typed in starting from the 3rd digit.
* - "11" is typed in starting from the 2nd digit.
* Thus "01100" will unlock the safe. "10011", and "11001" would also unlock the safe.
*
*
* Constraints:
*
* 1 <= n <= 4
* 1 <= k <= 10
* 1 <= kn <= 4096
*/


public class CrackingTheSafes {

// V0
// TODO : implement below
// public String crackSafe(int n, int k) {
//
// }

// V1
// https://leetcode.com/problems/cracking-the-safe/solutions/314906/java-dfs-not-a-fast-solution-but-easy-to-understand-with-explanation/
public String crackSafe_1(int n, int k) {
Set<String> visited = new HashSet<String>();
//*start from string "00.."
String res = "";
for(int j = 0; j < n; j++){
res+=0;
}
//*calculate target length, which is k^n+n-1
int total = 1;
for(int i = 0; i < n; i++){
total *= k;
}
total += n-1;
//*run DFS
res=DFS(res, n, k, visited, total);
return res;
}
private String DFS(String res, int n, int k, Set<String> visited, int total){
int len = res.length();
visited.add(res.substring(len-n, len));
for(int i = 0; i < k; i++){
if(!visited.contains(res.substring(len-n+1, len)+i)){
String tmp = DFS(res+i, n, k, visited, total);
//*if length of result is less than total length, remove substring from visited and continue loop, else we are done! break the loop!
if(tmp.length() == total){
res = tmp;
break;
}
visited.remove(res.substring(len-n+1, len)+i);
}
}
return res;
}

// V2
// IDEA : DFS + BACKTRACK (gpt)
// https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/
public String crackSafe_2(int n, int k) {
int totalLen = (int) Math.pow(k, n) + n - 1;
StringBuilder ans = new StringBuilder();
for (int i = 0; i < n; i++) {
ans.append('0');
}
Set<String> visited = new HashSet<>();
visited.add(ans.toString());

if (dfs(ans, totalLen, n, k, visited)) {
return ans.toString();
}
return "";
}

private boolean dfs(StringBuilder ans, int totalLen, int n, int k, Set<String> visited) {
if (ans.length() == totalLen) {
return true;
}

String node = ans.substring(ans.length() - n + 1);
for (char c = '0'; c < '0' + k; c++) {
String next = node + c;
if (!visited.contains(next)) {
ans.append(c);
visited.add(next);
if (dfs(ans, totalLen, n, k, visited)) {
return true;
}
visited.remove(next);
ans.deleteCharAt(ans.length() - 1);
}
}

return false;
}

// V2-1
// IDEA : DFS + BACKTRACK
// https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/
public String crackSafe_2_1(int n, int k) {
int totalLen = (int) Math.pow(k, n) + n - 1;
StringBuilder node = new StringBuilder();
for (int i = 0; i < n - 1; i++) {
node.append('0');
}
StringBuilder ans = new StringBuilder();
Set<String> visited = new HashSet<>();
dfs_2_1(node.toString(), k, visited, ans);
return ans.toString() + node;
}

private void dfs_2_1(String node, int k, Set<String> visited, StringBuilder ans) {
for (char c = '0'; c < '0' + k; c++) {
String next = node + c;
if (visited.contains(next)) continue;
visited.add(next);
dfs_2_1(next.substring(1), k, visited, ans);
ans.append(c);
}
}

}
17 changes: 17 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -7781,6 +7781,23 @@ public List<String> decode(String s) {
// return null;
// }

// LC 753
// 7.34 - 8.00 PM
// https://leetcode.com/problems/cracking-the-safe/
/**
* n digit
* each digit in [0, k-1] range
* check recent n digits if it same as expect pwd
*
* There is a safe protected by a password.
* The password is a sequence of n digits where each digit
* can be in the range [0, k - 1].
*
*/
public String crackSafe(int n, int k) {
return null;
}




Expand Down

0 comments on commit 814c68f

Please sign in to comment.