๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

[leetcode] 36. Valid Sudoku ํ’€์ด, ํ•ด์„ค (python)

ttoance 2023. 10. 18. 06:49

[leetcode] 49. Group Anagrams ํ’€์ด, ํ•ด์„ค (python)

๋ฌธ์ œ ๋งํฌ

https://leetcode.com/problems/valid-sudoku/

 

Valid Sudoku - LeetCode

Can you solve this real interview question? Valid Sudoku - Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: 1. Each row must contain the digits 1-9 without repetition. 2. Each c

leetcode.com

 

์Šค๋„์ฟ ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. 

์™„์ „ํ•œ ์Šค๋„์ฟ ์˜ ๊ทœ์น™์„ ๋ชจ๋‘ ์ฒดํฌํ•˜์ง€๋Š” ์•Š๊ณ , ๊ฐ€๋กœ, ์„ธ๋กœ, 3*3์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋งŒ ์ฒดํฌํ•˜๋ฉด ๋œ๋‹ค. 

 

1์ฐจ ํ’€์ด

๋ฉ‹์žˆ๊ฒŒ ํ’€๊ณ  ์‹ถ์—ˆ์ง€๋งŒ, ์‹œ๊ฐ„์ƒ ๊ฐ€๋กœ,์„ธ๋กœ,3*3์„ ๊ฐ๊ฐ for๋ฌธ์„ ๋Œ๋ ค์„œ ํ™•์ธํ•˜๋Š” ํ’€์ด๋กœ ์ ‘๊ทผํ–ˆ๋‹ค. 

(์•„๋ž˜ 2์ฐจ ํ’€์ด๋กœ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ, ์ด์ค‘ for๋ฌธ ํ•˜๋‚˜๋กœ ๋ชจ๋“  ๊ฒƒ์„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋‚˜์ด์Šคํ•œ ํ’€์ด๊ฐ€ ์žˆ๋‹ค. )

 

๊ฐ€๋กœ์™€ ์„ธ๋กœ๋Š” ๋น„๊ต์  ๋‹จ์ˆœํ•˜์ง€๋งŒ, 3*3์„ ์ฒดํฌํ•˜๋Š” ๋กœ์ง์ด ๊นŒ๋‹ค๋กœ์› ๋Š”๋ฐ, 

์ด ๋ถ€๋ถ„์€ ์ค‘๊ฐ„์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ x -1,0,1 y -1,0,1์„ ํ•ด์„œ ๊ตฌํ•˜๋Š” ๋กœ์ง์œผ๋กœ ํ–ˆ๋‹ค. 

๊ทธ ๊ณผ์ •์—์„œ 4์ค‘ for๋ฌธ์„ ์จ๋ฒ„๋ ธ๋‹ค ;; 

 

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        
        # ๊ฐ€๋กœ์ค„ ํ™•์ธ 
        for row in board : 
            checks = set()
            for w in row :
                if w != '.' and w in checks:
                    return False
                checks.add(w)
                
        # ์„ธ๋กœ์ค„ ํ™•์ธ 
        for c in range(9) : 
            checks = set()
            for row in board:
                if row[c] != '.' and row[c] in checks:
                    return False
                checks.add(row[c])
        
                
        # 3 * 3 ํ™•์ธ : ์ค‘์‹ฌ ์ขŒํ‘œ ๊ธฐ์ค€์œผ๋กœ ํ™•์ธ 
        for x in range(1,8,3):
            for y in range(1,8,3) :
                checks = set() 
                for dx in range(-1,2):
                    for dy in range(-1,2):
                        if board[x + dx][y + dy] != '.' and  board[x + dx][y + dy] in checks:
                            return False
                        checks.add( board[x + dx][y + dy])
                
                
                
                
        
        return True

 

์ดํ›„ ๋ฐ”๋กœ O(9*9), for๋ฌธ ํ•˜๋‚˜๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ํ’€์ด๋ฅผ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค. 

 

 

2์ฐจ ํ’€์ด (ํ•ด์„ค ์ฐธ๊ณ )

ํ•ด๋‹น ํ’€์ด๋Š” ์•„๋ž˜ ์œ ํˆฌ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค. 

https://www.youtube.com/watch?v=TjFXEUCMqI8

 

์ด ๋ฐฉ์‹์€ for๋ฌธ์„ ๋Œ๋ฉด์„œ, ๊ฐ€๋กœ,์„ธ๋กœ,์‚ฌ๊ฐํ˜•์„ ๋ชจ๋‘ ํ™•์ธํ•˜๋ฉด์„œ ์ค‘๋ณต์ด ์žˆ๋Š”์ง€ ํ™•์ธ ๋ฐ ๊ฐ’์„ ์„ธํŒ…ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค. 

๋ˆˆ์—ฌ๊ฒจ๋ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์€ ์‚ฌ๊ฐํ˜• ์ฒดํฌ ๊ฐ™์€ ๊ฒฝ์šฐ ์•„๋ž˜ ์ขŒํ‘œ์—์„œ ๋ชจ๋‘ ๋™์ผํ•œ ํ‚ค ๊ฐ’์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ (r // 3, c // 3)์œผ๋กœ ์„ค์ •ํ•œ ๋ถ€๋ถ„์ด๋‹ค. 

(0,0) (0,1) (0,2)

(1,0) (1,1) (1,2)

(2,0) (2,1) (2,2)

 

rows = defaultdict(set)
cols = defaultdict(set)
squares = defaultdict(set)
for r in range(9):
    for c in range(9):
        if board[r][c] == '.':
            continue
        if (board[r][c] in rows[r] 
            or board[r][c] in cols[c]
            or board[r][c] in squares[(r//3,c//3)]):
            return False

        rows[r].add(board[r][c])
        cols[c].add(board[r][c])
        squares[(r//3,c//3)].add(board[r][c])
        
        
                
        
        return True
๋ฐ˜์‘ํ˜•