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
101 changes: 101 additions & 0 deletions N-Queens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 51. N-Queens
// https://leetcode.com/problems/n-queens/description/

// Solution => DFS + Backtracking

/**
* Time Complexity: O(n * n!) since at every row we have options to place queen in factorial pattern i.e, n then n - 2, then n - 4 and so on
* Also, for checking safety we check above, left diagnl and right diagnl elements which is n
*
* Space Complexity: O(n * n) since in worst case we would have n calls till we reach last row to place queen and n for board space
*/

class Solution {

List<List<String>> result;

public List<List<String>> solveNQueens(int n) {

result = new ArrayList<>();

boolean[][] board = new boolean[n][n];

// start dfs from first element
dfs(board, 0);

return result;
}

private void dfs(boolean[][] board, int r) {

// base condition
if (r >= board.length) { // i.e we placed all queens

List<String> pattern = new ArrayList<>();

for (int i = 0; i < board.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == true) {
sb.append("Q");
} else {
sb.append(".");
}
}

pattern.add(sb.toString());
}

result.add(pattern);
return;
}

// dfs for every col in a row
for (int c = 0; c < board[0].length; c++) {
if (isValid(board, r, c)) {
// action
board[r][c] = true;
// recurse
dfs(board, r + 1);
// backtrack
board[r][c] = false;
}
}
}

private boolean isValid(boolean[][] board, int r, int c) {
// check above
int row = r;
int col = c;
while (row >= 0) {
if (board[row][col] == true) {
return false;
}
row--;
}

// check diagonally right
row = r;
col = c;
while (row >= 0 && col < board[0].length) {
if (board[row][col] == true) {
return false;
}
row--;
col++;
}

// check diagonally left
row = r;
col = c;
while (row >= 0 && col >= 0) {
if (board[row][col] == true) {
return false;
}
row--;
col--;
}

return true;
}
}
71 changes: 71 additions & 0 deletions WordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 79. Word Search
// https://leetcode.com/problems/word-search/description/

// Solution => DFS + Backtracking

/**
* Time Complexity: O(m * n * 3 ^ l) since we iterate over enitre matrix we have m * n
* and at each dfs we take 3 decisions and we go till we find the word, i.e depth is l
*
* Space Complexity: O(l) where l is length of the word. Since in worst case we would have l call in call stack
*/

class Solution {
int m;
int n;
int[][] dirs;
boolean found;

public boolean exist(char[][] board, String word) {
m = board.length;
n = board[0].length;

found = false;

dirs = new int[][] { {1,0}, {-1,0}, {0,1}, {0,-1}};

// start new dfs for every matching first char
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
char ch = word.charAt(0);

if(board[i][j] == ch & !found){
// mark it visited
board[i][j] = '#';
dfs(board, i, j, 1, word);
// backtrack
board[i][j] = ch;
}
}
}

return found;
}

private void dfs(char[][] board, int i, int j, int idx, String word){
// base condition
if(idx >= word.length()){
found = true;
return;
}

// logic

// dfs on all 4 directions
for(int[] dir: dirs){
int r = i + dir[0];
int c = j + dir[1];

char ch = word.charAt(idx);

if(r >= 0 && c >= 0 && r < m && c < n && ch == board[r][c]){
// action
board[r][c] = '#';
// recurse
dfs(board, r, c, idx + 1, word);
// backtrack
board[r][c] = ch;
}
}
}
}