Skip to content

Commit

Permalink
add 1087 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 28, 2024
1 parent 32a53cb commit 67663ca
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@
802| [Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/) | [Python](./leetcode_python/Depth-First-Search/find-eventual-safe-states.py) | _O(\|V\| + \|E\|)_ | _O(\|V\|)_ | Medium |`Topological Sort`| AGAIN (3) (not start)
841| [Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/) | [Python](./leetcode_python/Depth-First-Search/keys-and-rooms.py) | _O(n!)_ | _O(n)_ | Medium |`google`, `bfs`, `dfs`, `good basic`| AGAIN*** (3)
851| [Loud and Rich](https://leetcode.com/problems/loud-and-rich/) | [Python](./leetcode_python/Depth-First-Search/loud-and-rich.py) | _O(q + r)_ | _O(q + r)_ | Medium |`amazon`,`defaultdict`, `good basic`| AGAIN*** (3)
1087| [Brace Explansion](https://leetcode.com/problems/brace-expansion/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DFS/BraceExpansion.java) | _O(q + r)_ | _O(q + r)_ | Medium |dfs, google| AGAIN*** (1)
1192| [Critical Connections in a Network](https://leetcode.com/problems/critical-connections-in-a-network/) | [Python](./leetcode_python/Depth-First-Search/critical-connections-in-a-network.py) | _O(q + r)_ | _O(q + r)_ | Hard |Tarjan's algorithm,dfs, graph, fb, amazon| AGAIN** (1) (not start)
1644| [Lowest Common Ancestor of a Binary Tree II](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/) | [Python](./leetcode_python/Depth-First-Search/lowest-common-ancestor-of-a-binary-tree-ii.py) | _O(q + r)_ | _O(q + r)_ | Medium |LCA, LC 236, m$, fb| AGAIN (not start)
1650| [Lowest Common Ancestor of a Binary Tree III](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/) | [Python](./leetcode_python/Depth-First-Search/lowest-common-ancestor-of-a-binary-tree-iii.py) | _O(q + r)_ | _O(q + r)_ | Medium |`parent node`, good trick, dfs, set, dict, LCA, LC 236, amazon, google, spotify m$, fb| AGAIN******** (2) (MUST)
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-28
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-09-26
- 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 @@
20240928: 1087
20240926: 939
20240924: 430
20240923: 363
Expand Down
13 changes: 9 additions & 4 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-11-22 -> ['1087']
2024-11-20 -> ['939']
2024-11-18 -> ['430']
2024-11-17 -> ['363']
Expand All @@ -8,29 +9,33 @@
2024-11-04 -> ['659']
2024-11-03 -> ['801,552']
2024-11-02 -> ['1057,1066,1110']
2024-11-01 -> ['1087']
2024-10-30 -> ['939']
2024-10-28 -> ['430']
2024-10-27 -> ['363']
2024-10-26 -> ['1032,844,1011']
2024-10-25 -> ['947', '1110, 1055']
2024-10-22 -> ['753']
2024-10-21 -> ['727']
2024-10-19 -> ['1087']
2024-10-18 -> ['359,1057,1055(todo)']
2024-10-17 -> ['939']
2024-10-15 -> ['430']
2024-10-14 -> ['363', '659']
2024-10-13 -> ['1032,844,1011', '801,552']
2024-10-12 -> ['947', '1057,1066,1110']
2024-10-11 -> ['1087']
2024-10-09 -> ['939', '753']
2024-10-08 -> ['727']
2024-10-07 -> ['430']
2024-10-06 -> ['363']
2024-10-06 -> ['1087', '363']
2024-10-05 -> ['1032,844,1011']
2024-10-04 -> ['939', '947', '315', '1110, 1055']
2024-10-03 -> ['1087']
2024-10-02 -> ['430']
2024-10-01 -> ['939', '363', '753', '659']
2024-09-30 -> ['1032,844,1011', '727', '801,552']
2024-09-29 -> ['939', '430', '947', '1057,1066,1110']
2024-10-01 -> ['1087', '939', '363', '753', '659']
2024-09-30 -> ['1087', '1032,844,1011', '727', '801,552']
2024-09-29 -> ['1087', '939', '430', '947', '1057,1066,1110']
2024-09-28 -> ['939', '363']
2024-09-27 -> ['939', '430', '1032,844,1011', '359,1057,1055(todo)']
2024-09-26 -> ['430', '363', '947', '753']
Expand Down
147 changes: 147 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/DFS/BraceExpansion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package LeetCodeJava.DFS;

// https://leetcode.com/problems/brace-expansion/description/
// https://leetcode.ca/2018-11-21-1087-Brace-Expansion/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* 1087. Brace Expansion
* Description
* You are given a string s representing a list of words. Each letter in the word has one or more options.
*
* If there is one option, the letter is represented as is.
* If there is more than one option, then curly braces delimit the options. For example, "{a,b,c}" represents options ["a", "b", "c"].
* For example, if s = "a{b,c}", the first character is always 'a', but the second character can be 'b' or 'c'. The original list is ["ab", "ac"].
*
* Return all words that can be formed in this manner, sorted in lexicographical order.
*
*
*
* Example 1:
*
* Input: s = "{a,b}c{d,e}f"
* Output: ["acdf","acef","bcdf","bcef"]
* Example 2:
*
* Input: s = "abcd"
* Output: ["abcd"]
*
*
* Constraints:
*
* 1 <= s.length <= 50
* s consists of curly brackets '{}', commas ',', and lowercase English letters.
* s is guaranteed to be a valid input.
* There are no nested curly brackets.
* All characters inside a pair of consecutive opening and ending curly brackets are different.
*
*/
public class BraceExpansion {

// V0
// TODO : implement, validate
// public String[] expand(String s) {
// }

// V1
// IDEA : DFS
// https://leetcode.ca/2018-11-21-1087-Brace-Expansion/
private List<String> ans;
private List<String[]> items;

public String[] expand_1(String s) {
ans = new ArrayList<>();
items = new ArrayList<>();
convert(s);
dfs(0, new ArrayList<>());
Collections.sort(ans);
return ans.toArray(new String[0]);
}

private void convert(String s) {
if ("".equals(s)) {
return;
}
if (s.charAt(0) == '{') {
int j = s.indexOf("}");
items.add(s.substring(1, j).split(","));
convert(s.substring(j + 1));
} else {
int j = s.indexOf("{");
if (j != -1) {
items.add(s.substring(0, j).split(","));
convert(s.substring(j));
} else {
items.add(s.split(","));
}
}
}

private void dfs(int i, List<String> t) {
if (i == items.size()) {
ans.add(String.join("", t));
return;
}
for (String c : items.get(i)) {
t.add(c);
dfs(i + 1, t);
t.remove(t.size() - 1);
}
}


// V2
// IDEA : DFS
// https://walkccc.me/LeetCode/problems/1087/#__tabbed_1_2
public String[] expand_2(String s) {
List<String> ans = new ArrayList<>();

dfs_2(s, 0, new StringBuilder(), ans);
Collections.sort(ans);
return ans.toArray(new String[0]);
}

private void dfs_2(final String s, int i, StringBuilder sb, List<String> ans) {
if (i == s.length()) {
ans.add(sb.toString());
return;
}
if (s.charAt(i) == '{') {
final int nextRightBraceIndex = s.indexOf("}", i);
for (final String c : s.substring(i + 1, nextRightBraceIndex).split(",")) {
sb.append(c);
dfs_2(s, nextRightBraceIndex + 1, sb, ans);
sb.deleteCharAt(sb.length() - 1);
}
} else { // s[i] != '{'
sb.append(s.charAt(i));
dfs_2(s, i + 1, sb, ans);
sb.deleteCharAt(sb.length() - 1);
}
}

// V3
// IDEA : DFS
// https://blog.csdn.net/qq_46105170/article/details/108840420

// V4
// IDEA : DFS
// https://blog.csdn.net/qq_21201267/article/details/107541722



// testing
// public static void main(String[] args) {
// BraceExpansion be = new BraceExpansion();
// String input = "{a,b}c{d,e}f";
// String[] res = be.expand(input);
// for(String x : Arrays.asList(res)){
// System.out.println(x);
// }
// }

}
9 changes: 9 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace4.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ public int compare(Integer o1, Integer o2) {
// int d1 = getDays(w_1, 3);
// System.out.println(">>> d1 = " + d1);


System.out.println("split test --------------");


s = "{a,b}c{d,e}f";
for(String z_ : s.split("}")){
System.out.println(z_);
}

}

private static int getDays(List<Integer> weightsList, int speed){
Expand Down
76 changes: 74 additions & 2 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev;

import java.util.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class workspace5 {

Expand Down Expand Up @@ -214,7 +217,7 @@ public int minAreaRect(int[][] points) {
// if (points.length == 0 && points[0].length == 0){
// return 0;
// }
if (points.length == 1 || points[0].length == 1){
if (points.length == 1 || points[0].length == 1) {
return points.length * points[0].length;
}
// init
Expand All @@ -225,4 +228,73 @@ public int minAreaRect(int[][] points) {
return x_len * y_len;
}

// LC 1087
// https://leetcode.ca/2018-11-21-1087-Brace-Expansion/
// 5.02 pm - 5.20 pm
// public static void main(String[] args) {
// System.out.println(this.expand(s));
// }

List<String> collected = new ArrayList<>();
List<String> regular = new ArrayList<>();
public String[] expand(String s) {

// no possible to have bracket ("{}")
if (s.length() < 3){
return new String[]{s};
}
List<String> candidates = new ArrayList<>();
for (String x : s.split("")){
Queue<String> q = new LinkedList<>();
boolean tooAdd = false;
if (x == "{"){
tooAdd = true;
break;
}
else if (!tooAdd){
q.add(x);
break;
}
else if (x == "}"){
tooAdd = false;
StringBuilder sb = new StringBuilder();
while(!q.isEmpty()){
sb.append(q.poll());
candidates.add(sb.toString());
}
}else{
}


}


// dfs
//List<String> collected = new ArrayList<>();
String[] x= (String[]) collected.toArray();
// order with lexicographical

return (String[]) collected.toArray(); // TODO : double check
}

int strLen = 5; // TODO : fix
private void dfs(List<String> regular, List<String> candidates, int startIdx, String cur){
if (candidates.size()==0){
return;
}
if (cur.length() == strLen){
this.collected.add(cur);
cur = "";
return;
}
if (cur.length() > strLen){
cur = "";
return;
}

}




}

0 comments on commit 67663ca

Please sign in to comment.