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
76 changes: 76 additions & 0 deletions NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* N-Queens Problem (LeetCode 51: https://leetcode.com/problems/n-queens/)
* Approach: Backtracking - Place queens row by row, checking for column and diagonal conflicts.
* Time Complexity: O(N!), where N is the number of queens (and board size).
* Space Complexity: O(N^2) for the board and result storage.
*/

import java.util.ArrayList;
import java.util.List;

public class NQueens {
private boolean[][] board;
List<List<String>> result;

public List<List<String>> solveNQueens(int n) {
result = new ArrayList<>();
board = new boolean[n][n];
helper(0, n);
return result;
}

private void helper(int r, int n) {
//base
if (r == n) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringBuilder builder = new StringBuilder();
for (int j = 0; j < n; j++) {
if (board[i][j]) builder.append("Q");
else builder.append(".");
}
list.add(builder.toString());
}
result.add(list);
}


//logic
for (int i = 0; i < n; i++) {
if (isSafe(r, i)) {
//action
board[r][i] = true;
//recursion
helper(r + 1, n);
//backtrack
board[r][i] = false;
}
}
}

private boolean isSafe(int r, int c) {
//up direction
for (int i = 0; i < r; i++) {
if (board[i][c]) return false;
}

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

i = r;
j = c;
//Diagonal up right
while (i >= 0 && j < board[0].length) {
if (board[i][j]) return false;
i--;
j++;
}
return true;
}
}
57 changes: 57 additions & 0 deletions WordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Word Search (LeetCode 79: https://leetcode.com/problems/word-search/)
* Approach: Backtracking/DFS - Try to match the word starting from each cell, marking visited cells.
* Time Complexity: O(M*N*3^L), where M,N are board dimensions and L is word length.
* Space Complexity: O(L) for recursion stack.
*/

public class WordSearch {

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

boolean flag;

public boolean exist(char[][] board, String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
dfs(board, 0, i, j, word);
}
}
}
return flag;
}

private void dfs(char[][] board, int idx, int r, int c, String word) {
//base
if (r < 0 || c < 0 || r == board.length || c == board[0].length || flag) {
return;
}
if( board[r][c] == '#') return;

if (idx == word.length()) {
flag = true;
return;
}

//logic
//action
if (word.charAt(idx) == board[r][c]) {
//action
board[r][c] = '#';
for (int[] dir : dirs) {
int cr = r + dir[0];
int cc = c + dir[1];
//recursion
dfs(board, idx + 1, cr, cc, word);
}
//backtrack
board[r][c] = word.charAt(idx);
}
}

public static void main(String[] args) {
System.out.println(new WordSearch().exist(
new char[][]{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}, "ABCCED"));
}
}
56 changes: 56 additions & 0 deletions WordSearch2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Word Search (LeetCode 79: https://leetcode.com/problems/word-search/)
* Approach: Backtracking/DFS - Try to match the word starting from each cell, marking visited cells.
* Time Complexity: O(M*N*3^L), where M,N are board dimensions and L is word length.
* Space Complexity: O(L) for recursion stack.
*/

public class WordSearch2 {

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

public boolean exist(char[][] board, String word) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == word.charAt(0)) {
if (dfs(board, 0, i, j, word))
return true;
}
}
}
return false;
}

private boolean dfs(char[][] board, int idx, int r, int c, String word) {

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

//base
if (r < 0 || c < 0 || r == board.length || c == board[0].length) {
return false;
}

//logic
if (word.charAt(idx) == board[r][c]) {
//action
board[r][c] = '#';
for (int[] dir : dirs) {
int cr = r + dir[0];
int cc = c + dir[1];
//recursion
if (dfs(board, idx + 1, cr, cc, word))
return true;
}
//backtrack
board[r][c] = word.charAt(idx);
}
return false;
}

public static void main(String[] args) {
System.out.println(new WordSearch2().exist(
new char[][]{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}, "ABCB"));
}
}