-
Notifications
You must be signed in to change notification settings - Fork 4
/
valid-sudoku.py
114 lines (105 loc) · 4.16 KB
/
valid-sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 36. Valid Sudoku
# 🟠 Medium
#
# https://leetcode.com/problems/valid-sudoku/
#
# Tags: Array - Hash Table - Matrix
import timeit
from collections import defaultdict
from typing import List
# Maintain a set for each of: row, column, box. When we visit a new
# cell, check if the same value has been seen before in the same row,
# column or box, if yes, return False, the Sudoku is not valid,
# otherwise push the value into the matching sets and continue.
# If we iterate over all the board without finding any invalid value,
# return True
#
# Time complexity: O(1) - We iterate over the board elements and, for
# each, perform some O(1) operations. Since the number of elements on
# the board is limited to 81, constant time. If the number of elements
# did not have a maximum, it would be O(n).
# Space complexity: O(1) - Same reason as above, the number of elements
# is limited to a max of 81, so then is the size of the sets.
#
# Runtime: 124 ms, faster than 72.73%
# Memory Usage: 14 MB, less than 35.50%
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# Create dictionaries to store numbers that we have seen.
(rows, cols, boxes) = (
defaultdict(set),
defaultdict(set),
defaultdict(set),
)
# Iterate over all the positions on the board adding the value
# of non-empty cells to the corresponding sets, row, col and
# section.
for row_idx in range(len(board)):
for col_idx in range(len(board[0])):
val = board[row_idx][col_idx]
# Skip empty cells.
if val == ".":
continue
sets = [
rows[row_idx],
cols[col_idx],
boxes[(row_idx // 3, col_idx // 3)],
]
# For each corresponding set, check if the same value
# is already in the set. If not seen, add it.
for s in sets:
if val in s:
return False
s.add(val)
# If we can visit all values without finding any conflict, the
# input is a valid Sudoku.
return True
def test():
executors = [Solution]
tests = [
[
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
],
True,
],
[
[
["8", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
],
False,
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(int(float("1"))):
for i, t in enumerate(tests):
sol = executor()
result = sol.isValidSudoku(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for "
+ f"test {i} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()