LeetCode-36
Valid Sudoku : A Sudoku board (partially filled) could be valid but is not necessarily solvable.
note : Only the filled cells need to be validated according to the mentioned rules.
Answer
:::python
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def valid_row(i, j, row):
if row==1:
item = board[i]
else:
item = [board[n][j] for n in range(0,9)]
p = []
box = [board[i//32 + r][j//32 + c] for r in range(i//3, i//3+3) for c in range(j//3, j//3+3)]
b = []
for i in range(0,9):
if item[i] !='.':
p.append(item[i])
if box[i] !='.':
b.append(box[i])
if len(set(box))-1 == len(b):
pass
else:
return False
if len(set(item))-1 == len(p):
return True
else:
return False
for i in range(0,9):
for j in range(0,9):
if valid_row(i,j,0) and valid_row(i,j,1):
pass
else:
return False
return True
Result : 272ms Memory: 16.5mb
λ¬Έμ : μ ν¨ν μ€λμΏ
μ΄ λ¬Έμ λ 9x9 ν¬κΈ°μΈ μ€λμΏ κ²μμ΄ μ ν¨νμ§ νμΈνλ λ¬Έμ μ λλ€. κ° ν, μ΄, κ·Έλ¦¬κ³ μμ 3x3 μ¬κ°νμ 1~9μ μ«μκ° ν λ²μ©λ§ λ±μ₯νλ©΄ μ ν¨ν μ€λμΏ μ λλ€. μ λ ₯μΌλ‘ μ΄λ₯Ό λνλ΄λ 9x9 λ¬Έμμ΄ λ°°μ΄μ΄ μ£Όμ΄μ§λλ€.
ν΄κ²°μ± :
μ΄ λ¬Έμ μ ν΅μ¬μ κ° ν, μ΄, μ¬κ°νμ μ€λ³΅λ μ«μκ° μλμ§λ₯Ό νμΈνλ κ²μ λλ€. μ°λ¦¬λ κ° ν, μ΄, κ·Έλ¦¬κ³ μμ μ¬κ°νμ 9κ°μ© μ΄ 27κ°μ κ·Έλ£ΉμΌλ‘ λλ μ μμ΅λλ€. μ΄ κ·Έλ£Ήλ€ μ€ ν κ·Έλ£Ήμ μ€λ³΅λ μ«μκ° μλ€λ©΄ μ€λμΏ λ μ ν¨νμ§ μμ΅λλ€.
λ°λΌμ, κ° κ·Έλ£Ήμ μ€λ³΅ μ¬λΆλ₯Ό 체ν¬νλ helper function valid_row
λ₯Ό λ§λ€κ³ μ΄ ν¨μλ₯Ό μ΄μ©ν΄μ κ° ν, μ΄, μμ μ¬κ°νμ λν΄ μ ν¨μ±μ νλ¨ν μ μμ΅λλ€.
valid_row
ν¨μμλ 3κ°μ μΈμκ° νμν©λλ€. i
μ j
λ νμ¬ κ²μ¬νκ³ μλ μμμ μμΉλ₯Ό λνλ
λλ€. row
λ 0, 1λ‘ λλ μ μμΌλ©°, κ°κ° ν΄λΉ μμμ νκ³Ό μ΄μ κ²μ¬νλ κ²½μ°μ
λλ€.
ν¨μλ ν΄λΉ κ·Έλ£Ήμ μν λͺ¨λ μμλ₯Ό λͺ¨μ item
리μ€νΈμ κ°μ μμ μ¬κ°νμ μν λͺ¨λ μμλ₯Ό λͺ¨μ box
리μ€νΈλ₯Ό λ§λλλ€. item
κ³Ό box
리μ€νΈμμ .
λ₯Ό μ μΈν λͺ¨λ μμλ₯Ό p
μ b
리μ€νΈμ μ μ₯ν©λλ€.
κ·Έλ¦¬κ³ λμ set
μ μ΄μ©ν΄μ μ€λ³΅λ μ«μκ° μλμ§ νμΈν©λλ€. λ§μ½ μ€λ³΅λ μ«μκ° μλ€λ©΄, ν΄λΉ κ·Έλ£Ήμ΄ μ ν¨νμ§ μμ΅λλ€. λ§μ§λ§μΌλ‘, ν΄λΉ κ·Έλ£Ήμ΄ μ ν¨νλ€λ©΄ True
λ₯Ό λ°νν©λλ€.
κ·Έλ¦¬κ³ isValidSudoku
λ©μλλ κ° ν, μ΄, μμ μ¬κ°νμ λν΄μ valid_row
ν¨μλ₯Ό νΈμΆνκΈ°λ§ νλ©΄ λ©λλ€.
μ΄λ κ² μμ±ν ν΄κ²°μ± μ μκ°λ³΅μ‘λλ $O(81 \times (3 \times 9)) = O(2187)$ μ΄λ―λ‘ μΆ©λΆν ν¨μ¨μ μ λλ€.
μλλ μ£Όμ΄μ§ μ½λμ λλ€.
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def valid_row(i, j, row):
if row==1:
item = board[i]
else:
item = [board[n][j] for n in range(0,9)]
p = []
box = [board[i//3*3 + r][j//3*3 + c] for r in range(0,3) for c in range(0,3)]
b = []
for i in range(0,9):
if item[i] !='.':
p.append(item[i])
if box[i] !='.':
b.append(box[i])
if len(set(box))-1 == len(b):
pass
else:
return False
if len(set(item))-1 == len(p):
return True
else:
return False
for i in range(0,9):
for j in range(0,9):
if valid_row(i,j,0) and valid_row(i,j,1):
pass
else:
return False
return True
μ½λ 리뷰:
μ£Όμ΄μ§ μ½λλ μμμ μ μν ν΄κ²°μ± κ³Ό κ΅μ₯ν μ μ¬ν©λλ€.
λ€λ§, μμ μ¬κ°νμ λν box
λ₯Ό λ§λ€ λ μλͺ»λ μΈλ±μ±μ μ¬μ©νλ μ€λ₯κ° μμ΅λλ€. box
μ μμμ μΈλ±μ€λ₯Ό μ°Ύμ λμλ 3μ κ³±ν΄μΌ νλλ° 2λ‘ μ°Ύκ² λμ΄μ λ¬Έμ κ° μκΉλλ€.
μ¬λ°λ₯Έ μ½λ:
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
def valid_row(i, j, row):
if row==1:
item = board[i]
else:
item = [board[n][j] for n in range(0,9)]
p = []
box = [board[i//3*3 + r][j//3*3 + c] for r in range(0,3) for c in range(0,3)]
b = []
for i in range(0,9):
if item[i] !='.':
p.append(item[i])
if box[i] !='.':
b.append(box[i])
if len(set(box))-1 == len(b):
pass
else:
return False
if len(set(item))-1 == len(p):
return True
else:
return False
for i in range(0,9):
for j in range(0,9):
if valid_row(i,j,0) and valid_row(i,j,1):
pass
else:
return False
return True
'π’ One step' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[ngrok] ngrok 502 Bad Gateway μμΈμ λνν ~ (0) | 2024.01.10 |
---|---|
[phpAdmin] (0) | 2023.07.29 |
[leetcode-242] Valid Anagram (0) | 2023.05.13 |
[leetcode-217] Contains Duplicate (0) | 2023.05.11 |
How to LeetCode effectively (0) | 2023.05.10 |