Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Time Complexity :O(n*n!) n: checking isValid, n!: to place n queens in n rows
// Space Complexity :O(n^2)+O(n)
// Did this code successfully run on Leetcode :yes
import java.util.ArrayList;
import java.util.List;

class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> answer = new ArrayList<>();
boolean[][] board = new boolean[n][n];
helper(board,0,n,answer);
return answer;
}
private void helper(boolean[][] board, int row, int n,List<List<String>> answer) {
if (row == n) {//there is a queen in each row
List<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();//build a string for each row
for (int j = 0; j < n; j++) {
if (board[i][j]) {
sb.append("Q");
} else {
sb.append(".");
}
}
list.add(sb.toString());//add row to list
}
answer.add(list);//add list to answer
return;
}
for (int j = 0; j < n; j++) {
if (isValid(board, row, j, n)) {
//action
board[row][j] = true;
//recurse
helper(board, row + 1, n,answer);
//backtrack
board[row][j] = false;
}
}
}
private boolean isValid(boolean[][] board, int i, int j, int n) {
int r = i, c = j;
while (r >= 0) {//up
if (board[r][c])
return false;
r--;
}
r = i;
c = j;
while (r >= 0 && c >= 0) {//upper left diagonal
if (board[r][c])
return false;
r--;
c--;
}
r = i;
c = j;
while (r >= 0 && c < n) {//upper right diagonal
if (board[r][c])
return false;
r--;
c++;
}
return true;
}
}
35 changes: 35 additions & 0 deletions WordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time Complexity :O(m*n*4^L) L: Length of word
// Space Complexity :O(L)
// Did this code successfully run on Leetcode :yes
class Solution {
int m,n;
int direction[][];
public boolean exist(char[][] board, String word) {
m = board.length;
n = board[0].length;
direction = new int[][]{{-1,0},{1,0},{0,1},{0,-1}};
for(int i=0;i<m;i++){//loop through all cells(i,j)
for(int j=0;j<n;j++){
if(board[i][j] == word.charAt(0)){//if (i,j) has 1st character of the word
if(helper(board,word,i,j,0)) return true;
//if we find a successful dfs, return true
}
}
}
return false;
}
private boolean helper(char[][] board,String word,int i,int j,int index){
if(index == word.length()) return true;//reached end
if(i < 0 || j < 0 || i == m || j == n || board[i][j] == '#') return false;//if out of grid or cell is already visited
if(board[i][j] != word.charAt(index)) return false;//wrong path
//mark as visited and move to find next index
board[i][j] = '#';
for(int[] dir: direction){
int r = dir[0] + i;
int c = dir[1] + j;
if(helper(board,word,r,c,index+1)) return true;
}
board[i][j] = word.charAt(index);//unmark
return false;
}
}