-
Notifications
You must be signed in to change notification settings - Fork 0
/
36_valid_sudoku.rb
72 lines (58 loc) · 1.28 KB
/
36_valid_sudoku.rb
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
# frozen_string_literal: true
# https://leetcode.com/problems/valid-sudoku/
# @param {Character[][]} board
# @return {Boolean}
def is_valid_sudoku(board)
[
has_valid_rows?(board),
has_valid_columns?(board),
has_valid_grids?(board)
].all?
end
private
# @param {Character[][]} board
# @return {Boolean}
def has_valid_rows?(board)
board.all? { |row| is_valid_array?(row) }
end
# @param {Character[]} arr
# @return {Boolean}
def is_valid_array?(arr)
ref = [0] * 10
arr.each do |sq|
next if sq == '.'
num = sq.to_i
return false if num.zero? || num > 9 || ref[num] == 1
ref[num] = 1
end
end
# @param {Character[][]} board
# @return {Boolean}
def has_valid_columns?(board)
(0...9).each do |n|
column = board.map { |row| row[n] }
return false unless is_valid_array?(column)
end
true
end
# @param {Character[][]} board
# @return {Boolean}
def has_valid_grids?(board)
[0, 3, 6].each do |i|
[0, 3, 6].each do |j|
grid = [
board[i][j],
board[i][j + 1],
board[i][j + 2],
board[i + 1][j],
board[i + 1][j + 1],
board[i + 1][j + 2],
board[i + 2][j],
board[i + 2][j + 1],
board[i + 2][j + 2]
]
return false unless is_valid_array?(grid)
end
end
true
end