M959 Regions Cut By Slashes
Union Find
DFS Transformed to Number of Islands problem.
int transformFactor = 3;
public int regionsBySlashes(String[] grid) {
int length = grid.length;
int[][] islands = fillIslands(grid);
int count = 0;
for(int i = 0; i<islands.length; i++){
for(int j = 0; j<islands.length; j++){
if(islands[i][j] == 0) {
count++;
dfs(i, j, islands);
}
}
}
return count;
}
public int[][] fillIslands(String[] grid) {
int length = grid.length;
int[][] islands = new int[length*transformFactor][length*transformFactor];
for(int i = 0; i<grid.length; i++) {
for(int j = 0; j<grid.length; j++) {
char each = grid[i].charAt(j);
int baseIndexi = 3*i;
int baseIndexj = 3*j;
if(each == '/') {
islands[baseIndexi][baseIndexj+2] = 1;
islands[baseIndexi+1][baseIndexj+1] = 1;
islands[baseIndexi+2][baseIndexj] = 1;
} else if(each == '\\') {
islands[baseIndexi][baseIndexj] = 1;
islands[baseIndexi+1][baseIndexj+1] = 1;
islands[baseIndexi+2][baseIndexj+2] = 1;
}
}
}
System.out.println(Arrays.deepToString(islands));
return islands;
}
public void dfs(int i, int j, int[][] islands) {
if(i>=islands.length || j>= islands.length
|| i<0 || j<0
|| islands[i][j] == 1) return;
islands[i][j] = 1;
dfs(i, j-1, islands);
dfs(i, j+1, islands);
dfs(i+1, j, islands);
dfs(i-1, j, islands);
}
}
1254. Number of Closed Islands
class Solution {
public int closedIsland(int[][] grid) {
int count = 0;
for(int i =0; i<grid.length; i++){
for(int j = 0; j<grid[0].length; j++) {
if(grid[i][j] == 0 && isClosedIslands(i, j, grid)) {
System.out.println("i: " + i + ", j: " + j);
count ++;
}
}
}
return count;
}
public boolean isClosedIslands(int i, int j, int[][] grid) {
if(grid[i][j] == 1) {
return true;
}
if( i >= grid.length-1 || j >= grid[0].length-1
|| i <= 0 || j<= 0 ) return false;
grid[i][j] = 1;
return isClosedIslands(i, j-1, grid)
& isClosedIslands(i, j+1, grid)
& isClosedIslands(i+1, j, grid)
& isClosedIslands(i-1, j, grid);
}
}
694. Number of Distinct Islands
Set directionsSet = new HashSet();
public int numDistinctIslands(int[][] grid) {
for(int i = 0; i<grid.length; i++) {
for(int j = 0; j<grid[0].length; j++) {
if(grid[i][j] == 1) {
StringBuilder directions = new StringBuilder();
drawPath(i, j, grid, directions, "");
directionsSet.add(directions.toString());
}
}
}
return directionsSet.size();
}
public void drawPath(int i, int j, int[][] grid, StringBuilder directions, String direction) {
if(i<0 || j<0|| i>= grid.length || j>= grid[0].length
|| grid[i][j] == 0) return;
grid[i][j] = 0;
directions.append(direction);
drawPath(i, j+1, grid, directions, "R");
drawPath(i, j-1, grid, directions, "L");
drawPath(i+1, j, grid, directions, "D");
drawPath(i-1, j, grid, directions, "U");
directions.append("B");
}
}
1110. Delete Nodes And Return Forest
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<TreeNode> res = new LinkedList<TreeNode>();
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
List<Integer> to_del = Arrays.stream(to_delete).boxed().collect(Collectors.toList());
Set set = new HashSet<Integer>(to_del);
TreeNode newRoot = dfs(root, set);
if(newRoot != null) res.add(newRoot);
return res;
}
public TreeNode dfs(TreeNode node, Set to_delete) {
if(node == null) return null;
node.left = dfs(node.left, to_delete);
node.right = dfs(node.right, to_delete);
if(to_delete.contains(node.val)){
if(node.left != null) res.add(node.left);
if(node.right != null) res.add(node.right);
to_delete.remove(node.val);
return null;
}
return node;
}
}
130. Surrounded Regions
class Solution {
List<String> toPaint = new LinkedList<String>();
List<String> list = new LinkedList<String>();
public void solve(char[][] board) {
for(int i = 0; i<board.length; i++) {
for(int j =0; j<board[0].length; j++){
list = new LinkedList<String>();
if(board[i][j] == 'O'){
boolean isSurronded = dfs(i, j, board);
if(!isSurronded) {
toPaint.addAll(list);
}
}
}
}
reversePaintBoard(board);
}
public boolean dfs(int i, int j, char[][] board){
if(i<0 || j<0 || i>= board.length
|| j>= board[0].length) return false;
if(board[i][j] == 'X') return true;
list.add(i+":"+j);
board[i][j] = 'X';
boolean isSurrounded
= dfs(i, j-1, board)
& dfs(i, j+1, board)
& dfs(i-1, j, board)
& dfs(i+1, j, board);
return isSurrounded;
}
public void reversePaintBoard(char[][] board) {
for(String eachIndex: toPaint) {
String[] indexes = eachIndex.split(":");
int i = Integer.parseInt(indexes[0] + "");
int j = Integer.parseInt(indexes[1] + "");
board[i][j] = 'O';
}
}
}