Skip to content
Open

solved #1078

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
84 changes: 84 additions & 0 deletions N_Queens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*

Approach: Backtracking is used row by row, trying to place one
queen in each column of the current row only if it is
safe with respect to previously placed queens.

The isValid method checks vertical and diagonal conflicts
above the current cell, and if valid, the queen is placed,
recursion proceeds to the next row, and then it backtracks
by removing the queen.

*/

// Time: O(n.n! + nsqr) --> backtracking + recursion
// Space: O(nsqr) + O(n) --> nsqr for board, n for stack space

class Solution {
List<List<String>> result;
public List<List<String>> solveNQueens(int n) {
this.result = new ArrayList<>();
boolean[][] board = new boolean[n][n];
helper(board, 0, n);

return result;
}

private void helper(boolean[][] board, int row, int n) {

if(row == n) {
List<String> list = new ArrayList<>();
for(int i=0; i<n; i++) {
StringBuilder sb = new StringBuilder();
for(int j=0; j<n; j++) {
if(board[i][j]) {
sb.append("Q");
} else {
sb.append(".");
}

}
list.add(sb.toString());
}
result.add(list);
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);
// backtrack
board[row][j] = false;
}
}
}

private boolean isValid(boolean[][] board, int i, int j, int n) {
int r = i, c = j;
// straight above
while(r >= 0) {
if(board[r][c]) return false;
r--;
}

r = i; c = j;
// diagonal left up
while(r>=0 && c >= 0) {
if(board[r][c]) return false;
r--; c--;
}

r = i; c = j;
// diagonal right up
while(r>=0 && c < n) {
if(board[r][c]) return false;
r--; c++;
}

return true;
}
}
58 changes: 58 additions & 0 deletions Word_Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*

Approach: since we have connected components, we can go for bfs and dfs.
But along with connected components we have the requirement of
backtracking to mark unvisited so we can only use DFS(keep track of the parent).
Because BFS does not maintain hierarchy of check for parent.

*/

/*

Time: O(3^l), l = lenght of word
Space: O(l), where l is the length of the word.

*/

class Solution {
int[][] dirs;
int m,n;
public boolean exist(char[][] board, String word) {
this.dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1}}; // LRTB
this.m = board.length;
this.n = board[0].length;

for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(dfs(board, i, j, word, 0)) return true; // implies that we identified the word
}
}
return false; // not found
}

private boolean dfs(char[][] board, int i, int j, String word, int idx) {

if(idx == word.length()) return true;

if(i < 0 || j < 0 || i == m || j == n || board[i][j] == '#') return false; // base case

if(board[i][j] != word.charAt(idx)) return false;

// action
board[i][j] = '#'; // mark as visited

// recurse
for(int[] dir: dirs) {
int r = dir[0] + i;
int c = dir[1] + j;

if(dfs(board, r, c, word, idx+1)) return true;
}

// backtrack
board[i][j] = word.charAt(idx);

return false;

}
}