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
60 changes: 60 additions & 0 deletions problem1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Time Complexity : O(n!)
// Space Complexity : O(n^2)
// 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
// Start with row 1 and go upto nth row
// For each cell, try placing a queen after checking there are no queens in the same col or the diagonals
// Backtrack after placing the queen to check other valid configurations

class Solution {
private:
bool isValid(int row, int col, int n, vector<string>& currState) {
// check column
for(int i=row-1; i>=0; i--) {
if(currState[i][col] == 'Q') return false;
}

// check diag - up - right
int i = row-1; int j = col+1;
while(i >=0 && j < n) {
if(currState[i][j] == 'Q') return false;
i--;
j++;
}
// check diag - up - left
i = row-1; j = col-1;
while(i >= 0 && j >= 0) {
if(currState[i][j] == 'Q') return false;
i--;
j--;
}

return true;
}
void helper(int row, int n, vector<string>& currState, vector<vector<string>>& valid) {
if(row == n) {
valid.push_back(currState);
return;
}
string currRow(n, '.');
for(int i=0; i<n; i++){
if(isValid(row, i, n, currState)) {
currRow[i]='Q';
currState.push_back(currRow);
helper(row+1, n, currState, valid);
currRow[i] = '.';
currState.pop_back();
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<string> currState;
vector<vector<string>> valid;
helper(0, n, currState, valid);
return valid;
}
};
47 changes: 47 additions & 0 deletions problem2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Time Complexity : O(m*n*4^L), L = length of word
// Space Complexity : O(L)
// 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
// Start DFS from cells which have the starting char of the word
// Explore valid cells in all 4 directions, use a # symbol to mark a cell as visited
// Backtrack # to unmark as visited once that path has been explored

class Solution {
private:
vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int r, int c, int m, int n, vector<vector<char>>& board, int idx, string word, bool& flag) {
if(idx == word.length()) {
flag = true;
return;
}
if(flag) return;
for(auto& dir : dirs) {
int nr = r + dir[0]; int nc = c + dir[1];
if(nr >= 0 && nr < m && nc >= 0 && nc < n && board[nr][nc] == word[idx] && board[nr][nc] != '#') {
board[nr][nc] = '#';
dfs(nr, nc, m, n, board, idx+1, word, flag);
board[nr][nc] = word[idx];
}
}
}
public:
bool exist(vector<vector<char>>& board, string word) {
int m = board.size();
int n = board[0].size();
bool flag = false;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(board[i][j] == word[0]) {
char temp = board[i][j];
board[i][j] = '#';
dfs(i, j, m, n, board, 1, word, flag);
board[i][j] = temp;
}
}
}
return flag;
}
};