Skip to content

Commit a9e5a62

Browse files
committed
update 130 java, progress
1 parent e4d1a08 commit a9e5a62

File tree

4 files changed

+142
-11
lines changed

4 files changed

+142
-11
lines changed

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241108: 210,1188
1+
20241108: 210,1188,130
22
20241104: 207,210(todo)
33
20241103: 900
44
20241102: 26,27

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-01-02 -> ['210,1188']
1+
2025-01-02 -> ['210,1188,130']
22
2024-12-29 -> ['207,210(todo)']
33
2024-12-28 -> ['900']
44
2024-12-27 -> ['26,27', '802,1197,26']
@@ -7,32 +7,32 @@
77
2024-12-18 -> ['951,792']
88
2024-12-14 -> ['163,1048']
99
2024-12-13 -> ['298,729']
10-
2024-12-12 -> ['210,1188', '1146']
10+
2024-12-12 -> ['210,1188,130', '1146']
1111
2024-12-08 -> ['207,210(todo)', '737']
1212
2024-12-07 -> ['900', '686,734,737']
1313
2024-12-06 -> ['26,27', '802,1197,26', '353']
1414
2024-12-05 -> ['528,334']
1515
2024-12-03 -> ['1145']
1616
2024-11-30 -> ['855,846', '1145,1219']
17-
2024-11-29 -> ['210,1188', '932']
17+
2024-11-29 -> ['210,1188,130', '932']
1818
2024-11-27 -> ['951,792', '524,221,889']
1919
2024-11-26 -> ['743,889']
2020
2024-11-25 -> ['207,210(todo)', '837']
2121
2024-11-24 -> ['900']
2222
2024-11-23 -> ['26,27', '802,1197,26', '163,1048', '981']
2323
2024-11-22 -> ['298,729', '1087']
24-
2024-11-21 -> ['210,1188', '1146']
24+
2024-11-21 -> ['210,1188,130', '1146']
2525
2024-11-20 -> ['939']
2626
2024-11-18 -> ['430']
2727
2024-11-17 -> ['207,210(todo)', '855,846', '737', '363']
28-
2024-11-16 -> ['210,1188', '900', '932', '686,734,737', '1032,844,1011']
28+
2024-11-16 -> ['210,1188,130', '900', '932', '686,734,737', '1032,844,1011']
2929
2024-11-15 -> ['26,27', '802,1197,26', '353', '947']
3030
2024-11-14 -> ['951,792', '528,334']
31-
2024-11-13 -> ['210,1188']
31+
2024-11-13 -> ['210,1188,130']
3232
2024-11-12 -> ['207,210(todo)', '1145', '753']
33-
2024-11-11 -> ['210,1188', '900', '727']
34-
2024-11-10 -> ['210,1188', '26,27', '802,1197,26', '163,1048']
35-
2024-11-09 -> ['210,1188', '207,210(todo)', '855,846', '298,729', '1145,1219']
33+
2024-11-11 -> ['210,1188,130', '900', '727']
34+
2024-11-10 -> ['210,1188,130', '26,27', '802,1197,26', '163,1048']
35+
2024-11-09 -> ['210,1188,130', '207,210(todo)', '855,846', '298,729', '1145,1219']
3636
2024-11-08 -> ['900', '932', '1146']
3737
2024-11-07 -> ['207,210(todo)', '26,27', '802,1197,26']
3838
2024-11-06 -> ['207,210(todo)', '900', '951,792', '524,221,889']

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,87 @@ public class SurroundedRegions {
7272
// }
7373

7474
// V1
75+
// IDEA : DFS (fixed by gpt)
76+
public void solve_1(char[][] board) {
77+
if (board == null || board.length == 0 || board[0].length == 0) {
78+
return;
79+
}
80+
81+
int rows = board.length;
82+
int cols = board[0].length;
83+
84+
// Mark border 'O's and connected 'O's as temporary 'T'
85+
// NOTE !!! mark as "T" (so we can flip them as "O" in the following step)
86+
for (int i = 0; i < rows; i++) {
87+
if (board[i][0] == 'O') {
88+
dfsMark(i, 0, board);
89+
}
90+
if (board[i][cols - 1] == 'O') {
91+
dfsMark(i, cols - 1, board);
92+
}
93+
}
94+
// Mark border 'O's and connected 'O's as temporary 'T'
95+
// NOTE !!! mark as "T" (so we can flip them as "O" in the following step)
96+
for (int j = 0; j < cols; j++) {
97+
if (board[0][j] == 'O') {
98+
dfsMark(0, j, board);
99+
}
100+
if (board[rows - 1][j] == 'O') {
101+
dfsMark(rows - 1, j, board);
102+
}
103+
}
104+
105+
// Flip all remaining 'O' to 'X' and 'T' back to 'O'
106+
for (int y = 0; y < rows; y++) {
107+
for (int x = 0; x < cols; x++) {
108+
/**
109+
* NOTE !!!
110+
*
111+
* if still 'O', means
112+
*
113+
* 1) they are connected 'O', surrounded by X
114+
* 2) and NOT at boundary
115+
*/
116+
if (board[y][x] == 'O') {
117+
board[y][x] = 'X';
118+
/**
119+
* NOTE !!!
120+
*
121+
* if is 'T', means
122+
*
123+
* 1) they are connected 'O', but at boundary
124+
*
125+
* so need to flip them back as 'O'
126+
*/
127+
} else if (board[y][x] == 'T') {
128+
board[y][x] = 'O';
129+
}
130+
}
131+
}
132+
}
133+
134+
private void dfsMark(int y, int x, char[][] board) {
135+
int rows = board.length;
136+
int cols = board[0].length;
137+
138+
// If out of bounds or not 'O', return
139+
if (y < 0 || y >= rows || x < 0 || x >= cols || board[y][x] != 'O') {
140+
return;
141+
}
142+
143+
// Mark 'O' as temporary 'T'
144+
board[y][x] = 'T';
145+
146+
// Explore all 4 directions
147+
int[][] moves = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
148+
for (int[] move : moves) {
149+
int newY = y + move[0];
150+
int newX = x + move[1];
151+
dfsMark(newY, newX, board);
152+
}
153+
}
154+
155+
// V1_1
75156
// IDEA : DFS
76157
// https://leetcode.com/problems/surrounded-regions/editorial/
77158

@@ -88,7 +169,7 @@ public Pair(U first, V second) {
88169
protected Integer ROWS = 0;
89170
protected Integer COLS = 0;
90171

91-
public void solve_1(char[][] board) {
172+
public void solve_1_1(char[][] board) {
92173
if (board == null || board.length == 0) {
93174
return;
94175
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,56 @@ public int size() {
26472647
}
26482648
}
26492649

2650+
// LC 130
2651+
// https://leetcode.com/problems/surrounded-regions/description/
2652+
// 7.53 - 8.15 PM
2653+
public void solve(char[][] board) {
2654+
if (board.length == 1 && board[0].length == 1){
2655+
return;
2656+
}
2657+
// get all "0"
2658+
List<List<Integer>> collected = new ArrayList<>();
2659+
int l = board.length;
2660+
int w = board[0].length;
2661+
for (int y = 0; y < l; y++){
2662+
for (int x = 0; x < w; x++){
2663+
if (String.valueOf(board[y][x]).equals("O")){
2664+
List<Integer> tmp = new ArrayList<>();
2665+
tmp.add(x);
2666+
tmp.add(y);
2667+
collected.add(tmp);
2668+
}
2669+
}
2670+
}
2671+
2672+
// dfs
2673+
for (List<Integer> element : collected){
2674+
dfsUpdate(element.get(0), element.get(1), board);
2675+
}
2676+
}
2677+
2678+
private void dfsUpdate(int x, int y, char[][] board){
2679+
2680+
int l = board.length;
2681+
int w = board[0].length;
2682+
2683+
if (String.valueOf(board[y][x]).equals("X")){
2684+
return;
2685+
}
2686+
2687+
// update as "X"
2688+
//board[y][x] = new Character("X"); //??
2689+
2690+
int[][] moves = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0} };
2691+
for (int[] move : moves){
2692+
int x_ = x + move[0];
2693+
int y_ = y + move[1];
2694+
if (x_ >= 0 && x_ < w && y_ >= 0 && y_ < l && !String.valueOf(board[y_][x_]).equals("X")){
2695+
dfsUpdate(x_, y_, board);
2696+
}
2697+
}
2698+
}
2699+
26502700

26512701
}
26522702

0 commit comments

Comments
 (0)