Skip to content

Commit 6b9e778

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 37
1 parent 6f9f478 commit 6b9e778

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
- [34 Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/)
8989
- [35 Search Insert Position](https://leetcode.com/problems/search-insert-position/description/)
9090
- [36 Valid Sudoku](https://leetcode.com/problems/valid-sudoku/description/)
91+
- [37 Sudoku Solver](https://leetcode.com/problems/sudoku-solver/description/)
9192
- [39 Combination Sum](https://leetcode.com/problems/combination-sum/description/)
9293
- [42 Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)
9394
- [45 Jump Game II](https://leetcode.com/problems/jump-game-ii/description/)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def solveSudoku(self, board: List[List[str]]) -> None:
8+
"""
9+
Write a program to solve a Sudoku puzzle by filling the empty cells.
10+
11+
A sudoku solution must satisfy all of the following rules:
12+
1. Each of the digits 1-9 must occur exactly once in each row.
13+
2. Each of the digits 1-9 must occur exactly once in each column.
14+
3. Each of the digits 1-9 must occur exactly once in each of the 9 3x3
15+
sub-boxes of the grid.
16+
17+
The '.' character indicates empty cells.
18+
"""
19+
# Time complexity: O(9^81)
20+
# Space complexity: O(9^81)
21+
# Compute remaining rows, cols and squares
22+
rows = {i: set([str(val) for val in range(1, 10)]) for i in range(0, 9)}
23+
cols = {i: set([str(val) for val in range(1, 10)]) for i in range(0, 9)}
24+
squares = {i: set([str(val) for val in range(1, 10)]) for i in range(0, 9)}
25+
for row in range(len(board)):
26+
for col in range(len(board[row])):
27+
if board[row][col] == ".":
28+
continue
29+
square = 3 * (row // 3) + col // 3
30+
rows[row].remove(board[row][col])
31+
cols[col].remove(board[row][col])
32+
squares[square].remove(board[row][col])
33+
34+
def dfs(row: int, col: int) -> bool:
35+
new_col = (col + 1) % 9
36+
new_row = row + 1 if new_col == 0 else row
37+
if row >= 9:
38+
return True
39+
if board[row][col] != ".":
40+
return dfs(new_row, new_col)
41+
square = 3 * (row // 3) + col // 3
42+
visited = rows[row].intersection(cols[col], squares[square])
43+
for val in visited:
44+
board[row][col] = val
45+
rows[row].remove(val)
46+
cols[col].remove(val)
47+
squares[square].remove(val)
48+
if dfs(new_row, new_col):
49+
return True
50+
board[row][col] = "."
51+
rows[row].add(val)
52+
cols[col].add(val)
53+
squares[square].add(val)
54+
return False
55+
56+
return dfs(0, 0)

tests/test_37_sudoku_solver.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._37_sudoku_solver import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["board", "expected"],
10+
argvalues=[
11+
(
12+
[
13+
["5", "3", ".", ".", "7", ".", ".", ".", "."],
14+
["6", ".", ".", "1", "9", "5", ".", ".", "."],
15+
[".", "9", "8", ".", ".", ".", ".", "6", "."],
16+
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
17+
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
18+
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
19+
[".", "6", ".", ".", ".", ".", "2", "8", "."],
20+
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
21+
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
22+
],
23+
[
24+
["5", "3", "4", "6", "7", "8", "9", "1", "2"],
25+
["6", "7", "2", "1", "9", "5", "3", "4", "8"],
26+
["1", "9", "8", "3", "4", "2", "5", "6", "7"],
27+
["8", "5", "9", "7", "6", "1", "4", "2", "3"],
28+
["4", "2", "6", "8", "5", "3", "7", "9", "1"],
29+
["7", "1", "3", "9", "2", "4", "8", "5", "6"],
30+
["9", "6", "1", "5", "3", "7", "2", "8", "4"],
31+
["2", "8", "7", "4", "1", "9", "6", "3", "5"],
32+
["3", "4", "5", "2", "8", "6", "1", "7", "9"],
33+
],
34+
)
35+
],
36+
)
37+
def test_func(board: List[List[str]], expected: List[List[str]]):
38+
"""Tests the solution of a LeetCode problem."""
39+
Solution().solveSudoku(board)
40+
assert board == expected

0 commit comments

Comments
 (0)