Skip to content

completed problem 1 & problem 2#1089

Open
aditya18m wants to merge 1 commit intosuper30admin:masterfrom
aditya18m:master
Open

completed problem 1 & problem 2#1089
aditya18m wants to merge 1 commit intosuper30admin:masterfrom
aditya18m:master

Conversation

@aditya18m
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

N-Queens (problem1.cpp)

Your solution shows a good understanding of the backtracking approach for the N-Queens problem. However, there are a few key issues to address:

  • The main issue is with how you manage the currState. Instead of pushing and popping rows, you should pre-allocate the board or use a fixed structure. For example, you can initialize currState as a vector of n strings, each initialized to n '.' characters. Then, for each row, you set the queen in the appropriate column and proceed recursively. This avoids the need for pushing and popping, which is error-prone.

  • In your helper function, you are creating a string currRow for each call, which is then modified and pushed to currState. However, when you backtrack, you pop the row. This causes the state to be inconsistent because the row index row corresponds to the size of currState. Instead, you should have currState as a vector of size n, and you directly set the row content for the current row index.

  • Consider using additional data structures to optimize the validity checks. For instance, you can use a boolean array for columns, and two arrays for the two diagonals (left diagonal and right diagonal). This would make the validity check O(1) and improve overall performance.

  • Your current validity check function is correct, but it can be improved. Also, note that you don't need to check below the current row because you are placing queens row by row from top to bottom.

Here is a corrected version of your approach without pushing/popping:

class Solution {
private:
    bool isValid(int row, int col, int n, vector<string>& board) {
        // Check column above
        for (int i = 0; i < row; i++) {
            if (board[i][col] == 'Q') return false;
        }
        // Check top-left diagonal
        for (int i = row-1, j = col-1; i >=0 && j>=0; i--, j--) {
            if (board[i][j] == 'Q') return false;
        }
        // Check top-right diagonal
        for (int i = row-1, j = col+1; i>=0 && j<n; i--, j++) {
            if (board[i][j] == 'Q') return false;
        }
        return true;
    }

    void helper(int row, int n, vector<string>& board, vector<vector<string>>& solutions) {
        if (row == n) {
            solutions.push_back(board);
            return;
        }
        for (int col = 0; col < n; col++) {
            if (isValid(row, col, n, board)) {
                board[row][col] = 'Q';
                helper(row+1, n, board, solutions);
                board[row][col] = '.';
            }
        }
    }
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<string> board(n, string(n, '.'));
        vector<vector<string>> solutions;
        helper(0, n, board, solutions);
        return solutions;
    }
};

This version initializes the board with n rows of n dots. Then, in the helper function, it directly modifies the current row and column. This avoids the pushing and popping and ensures correctness.

VERDICT: NEEDS_IMPROVEMENT


Word Search (problem2.cpp)

Note: The verdict should be based on whether the solution is correct and efficient enough to pass the problem constraints. Minor issues can be addressed in feedback but may not necessarily require a NEEDS_IMPROVEMENT if the solution is fundamentally correct.

Now, evaluate the student's solution.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants