From 4f75c81564f8919492b17c02abfa38b45bfb9345 Mon Sep 17 00:00:00 2001 From: michaeldonato Date: Mon, 27 Oct 2025 20:27:38 -0500 Subject: [PATCH] Backtracking-3 Completed --- exist.java | 45 +++++++++++++++++++++++++++++++++ solveNQueens.java | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 exist.java create mode 100644 solveNQueens.java diff --git a/exist.java b/exist.java new file mode 100644 index 00000000..61d6ef23 --- /dev/null +++ b/exist.java @@ -0,0 +1,45 @@ +// Time Complexity : O(m * n * 4^L) +// Space Complexity : O(L) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None +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}}; + this.m = board.length; + this.n = board[0].length; + + for(int i=0; i> result; + public List> 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 list = new ArrayList<>(); + for(int i=0; i=0){ + if(board[r][c]) return false; + r--; + } + + r = i; c = j; + while(r>=0 && c>=0){ + if(board[r][c]) return false; + r--; c--; + } + + r = i; c = j; + while(r>=0 && c < n){ + if(board[r][c]) return false; + r--; c++; + } + + return true; + } +}