diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..ba8ddce7 --- /dev/null +++ b/problem1.java @@ -0,0 +1,87 @@ +// Time Complexity :O(n!) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes + +/* + * Approach + * + * There are 3 main components to solve this problem + * + * if is to check if the placed queen on each row is safe of not + * we do that by check 3 component, left diagonal, right diagonal, and the straight column + * + * if the check fails continue of the for loop else mark that portion in the board + * increase the row+1 and do the recursive call on that row + * + * when r == board.length + * we create new list and store the board value using a stringbuilder to make true as Q and false as . + * + */ + +class Solution { + public List> solveNQueens(int n) { + List> result = new ArrayList<>(); + boolean[][] board = new boolean[n][n]; + backtrack(board, 0, result); + return result; + } + + private void backtrack(boolean[][] board, int r, List> result) { + if (r == board.length) { + List li = new ArrayList<>(); + // convert the board into stirg + for (int i = 0; i < board.length; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < board.length; j++) { + if (board[i][j] == true) { + sb.append('Q'); + } else { + sb.append('.'); + } + } + li.add(sb.toString()); + } + result.add(li); + } + // + for (int c = 0; c < board.length; c++) { + if (isSafe(board, r, c)) { + board[r][c] = true; + + backtrack(board, r + 1, result); + + board[r][c] = false; + } + } + } + + private boolean isSafe(boolean[][] board, int r, int c) { + // same col check + for (int i = r - 1; i >= 0; i--) { + if (board[i][c] == true) { + return false; + } + } + // left diagolan + int i = r; + int j = c; + while (i >= 0 && j >= 0) { + if (board[i][j] == true) { + return false; + } + i--; + j--; + } + // right diagolan + i = r; + j = c; + while (i >= 0 && j < board.length) { + if (board[i][j] == true) { + return false; + } + i--; + j++; + } + return true; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..34e5efb9 --- /dev/null +++ b/problem2.java @@ -0,0 +1,64 @@ +// Time Complexity :O(mn3^L) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes + +/* + * Approach + * here are are using DFS and backtracking to solve the problem + * + * we will first find the starting letter in of the word on the board + * after the word is found we start the dfs + * + * where will will use the dirs array to find the next char of the word + * if not found then return false,come back one step and change the board to its previous state + * if found, make that node visited and start the search for there again + * + * + */ + +class Solution { + int[][] dirs; + + public boolean exist(char[][] board, String word) { + int m = board.length; + int n = board[0].length; + this.dirs = new int[][] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == word.charAt(0)) { + if (backtrack(board, word, i, j, 0)) + return true; + } + } + } + return false; + } + + private boolean backtrack(char[][] board, String word, int r, int c, int idx) { + // base + if (idx == word.length()) { + return true; + } + if (r < 0 || c < 0 || r == board.length || c == board[0].length || board[r][c] == '#') { + return false; + } + + // logic + if (board[r][c] == word.charAt(idx)) { + // action + board[r][c] = '#'; + + // recurse + for (int[] dir : dirs) { + int nr = r + dir[0]; + int nc = c + dir[1]; + if (backtrack(board, word, nr, nc, idx + 1)) { + return true; + } + } + // backtrack + board[r][c] = word.charAt(idx); + } + return false; + } +} \ No newline at end of file