From db86ef49f09f8014754bd0d540c77296f432c058 Mon Sep 17 00:00:00 2001 From: Neel Patel Date: Tue, 30 Sep 2025 16:37:43 -0400 Subject: [PATCH] Backtracking 3 done --- Problem1.java | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem2.java | 54 +++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..cec1bf3a --- /dev/null +++ b/Problem1.java @@ -0,0 +1,90 @@ +// Time Complexity : O(n*n!) +// Space Complexity : O(n^2) for grid +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach + +/** + * Using backtrack approach to start putting the queen in every possible column in each row to check if the grid is valid. + * If not valid, then we backtrack the position and try the next one. + */ +class Solution { + public List> solveNQueens(int n) { + List> result = new ArrayList<>(); + boolean[][] grid = new boolean[n][n]; + backtrack(grid, n, 0, result); + return result; + } + + private void backtrack(boolean[][] grid, int n, int r, List> result) { + //base case + // we reach the last row + if (r == n) { + // converting boolean grid into result string + result.add(createResult(grid)); + return; + } + + for (int c = 0; c < n; c++) { + if (canPlace(grid, r, c, n)) { + // action + grid[r][c] = true; + //recurse + backtrack(grid, n, r + 1, result); + //backtrack + grid[r][c] = false; + } + } + } + + private boolean canPlace(boolean[][] grid, int r, int c, int n) { + // checking if there is any queen in same column + for (int i = 0; i < r; i++) { + if (grid[i][c]) return false; + } + + // checking if any queen in left diagonal + int i = r; + int j = c; + while (i >= 0 && j >= 0) { + if (grid[i][j]) + return false; + i--; + j--; + } + + // checking if any queen in right diagonal + i = r; + j = c; + while (i >= 0 && j < n) { + if (grid[i][j]) { + return false; + } + + i--; + j++; + } + + return true; + } + + List createResult(boolean[][] grid) { + List li = new ArrayList<>(); + for (int i = 0; i < grid.length; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j]) + sb.append('Q'); + else + sb.append('.'); + } + li.add(sb.toString()); + } + + return li; + } + + +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..926266c4 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,54 @@ +// Time Complexity : O(m*n * 4 ^ l) where l is the length of the word +// Space Complexity : O(l) where it will l recursion stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + + +// Your code here along with comments explaining your approach + +/** + * We use the backtracking approach to start finding the word from every possible index. + * Once a character matches, we convet it to #, look for next character. + * If the character was not found, backtrack the character back to its original character to continue the recursion. + */ +class Solution { + int[][] dirs = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; + + public boolean exist(char[][] board, String word) { + // checking for the start of the word from every possible index + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (isPresent(board, word, 0, i, j)) + return true; + } + } + + return false; + } + + private boolean isPresent(char[][] board, String word, int idx, int r, int c) { + //base + // we reached the end of the word + if (idx == word.length()) { + return true; + } + // bounds check and check for the current path + if (r < 0 || r == board.length || c < 0 || 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) { + if (isPresent(board, word, idx + 1, r + dir[0], c + dir[1])) + return true; + } + //backtrack + board[r][c] = word.charAt(idx); + } + + return false; + } +} \ No newline at end of file