Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 51 · Constraint Satisfaction

N-Queens

LeetCode 51 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Column-by-column backtracking with row + diagonal hashes.

கடினம்ArrayBacktrackingAlgorithm X
LeetCode இல் திற ↗

நான் N-Queens (LeetCode 51) ஐ இப்படித் தீர்த்தேன் — Constraint Satisfaction பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Place one queen per column. For each row in the current column, place a queen if no earlier queen attacks that square, recurse to the next column, then remove it. Attacks can be checked by scanning, or in O(1) with arrays for rows and both diagonals.

Approach

  1. 1

    Version 1: check left, upper-left and lower-left squares by scanning.

  2. 2

    Version 2: row_hash[row], diag[row+col], anti-diag[n−1+col−row].

  3. 3

    Place, mark, recurse, unmark.

  4. 4

    col == n → record the board.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Scan for attacksO(n!·n) time

Walk three directions.

2
Hash arraysAcceptedO(n!) time

O(1) attack check.

Scan for attacksPythonO(n!·n) time
class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        res=[]
        board = [["."]*n for _ in range(n)]
        def can_place_queen(row,col,board):
            temp_row = row
            temp_col = col

            #move left
            while col>=0:
                if board[row][col]=="Q":
                    return False
                col-=1

            #left diagonal
            row = temp_row
            col = temp_col
            while row>=0 and col>=0:
                if board[row][col]=="Q":
                    return False
                row-=1
                col-=1

            #bottom diagonal
            row = temp_row
            col = temp_col
            while row<n and col>=0:
                if board[row][col]=="Q":
                    return False
                row+=1
                col-=1
            return True
        def backtrack(col,board):
            if col==n:
                res.append(["".join(row) for row in board])
                return
            for row in range(n):
                if can_place_queen(row,col,board):
                    board[row][col]="Q"
                    backtrack(col+1,board)
                board[row][col]="."
        backtrack(0,board)
        return res
Hash arraysPythonO(n!) time
class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        res=[]
        board = [["."]*n for _ in range(n)]
        row_hash = [0]*n
        bottom_left_diagonal = [0]*(2*n-1)
        top_left_diagonal = [0]*(2*n-1)
        def backtrack(col,board):
            if col==n:
                res.append(["".join(row) for row in board])
                return
            for row in range(n):
                if row_hash[row]==0 and bottom_left_diagonal[row+col]==0 and top_left_diagonal[n-1+col-row]==0:
                        board[row][col]="Q"
                        row_hash[row]=1
                        bottom_left_diagonal[row+col]=1
                        top_left_diagonal[n-1+col-row]=1
                        backtrack(col+1,board)
                        board[row][col]="."
                        row_hash[row]=0
                        bottom_left_diagonal[row+col]=0
                        top_left_diagonal[n-1+col-row]=0
        backtrack(0,board)
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

N-Queens — அடிக்கடி கேட்கப்படும் கேள்விகள்

N-Queens பின்னாலுள்ள intuition என்ன?

Place one queen per column. For each row in the current column, place a queen if no earlier queen attacks that square, recurse to the next column, then remove it. Attacks can be checked by scanning, or in O(1) with arrays for rows and both diagonals.

N-Queens (LeetCode 51) ஐ எப்படித் தீர்ப்பது?

1. Version 1: check left, upper-left and lower-left squares by scanning. 2. Version 2: row_hash[row], diag[row+col], anti-diag[n−1+col−row]. 3. Place, mark, recurse, unmark. 4. col == n → record the board.

N-Queens இன் time மற்றும் space complexity என்ன?

Time: O(n!). Space: O(n²) board. (Accepted approach: Hash arrays.)

N-Queens இல் பொதுவான தவறுகள் என்ன?

Only check to the left — columns to the right are still empty.

N-Queens ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Scan for attacks (O(n!·n) time): Walk three directions. Hash arrays (O(n!) time): O(1) attack check.