-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_sudoku.py
74 lines (48 loc) · 1.68 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
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
we need to figure out if the sudoku board is valid
first, what is a valid sudoku board?
each row 1-9 without repetition
each colmn 1-9
each square 1-9
JUST checking if valid, not trying to solve it!
so what do we need to check?
row
column
square
this would mean n*(3n) worth of work to check each of these, which is
quadratic time
space is constant
can we do better?
go through every row and hash
go through every column and hash
go through every square and hash
then check to see if valid
that would be n work for each hash so 3*n = O(n)
then also N memory because O(n) to hold each hashtable,
squares are
0 1 2
3 4 5
6 7 8
rows are
0
-
8
columns
0 - 8
"""
hashed_rows = {}
hashed_cols = {}
hashed_sqrs = {}
# hash rows first
for index, val in enumerate(board):
hashed_rows[index] = {}
for char in val:
hashed_rows[index][char] = char
# now hash columns
for i in range(len(board)):
def add_to_hash(how, seen_numbers, )
if how == "column":