Skip to content

Commit fe7dfe6

Browse files
committed
2024-07-09 v. 6.1.0: added "36. Valid Sudoku"
1 parent 37a359a commit fe7dfe6

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
475475
| 29. Divide Two Integers | [Link](https://leetcode.com/problems/divide-two-integers/) | [Link](./lib/medium/29_divide_two_integers.rb) |
476476
| 33. Search in Rotated Sorted Array | [Link](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Link](./lib/medium/33_search_in_rotated_sorted_array.rb) |
477477
| 34. Find First and Last Position of Element in Sorted Array | [Link](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | [Link](./lib/medium/34_find_first_and_last_position_of_element_in_sorted_array.rb) |
478+
| 36. Valid Sudoku | [Link](https://leetcode.com/problems/valid-sudoku/) | [Link](./lib/medium/36_valid_sudoku.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.0.9'
8+
s.version = '6.1.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/medium/36_valid_sudoku.rb

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/valid-sudoku/
4+
# @param {Character[][]} board
5+
# @return {Boolean}
6+
def is_valid_sudoku(board)
7+
[
8+
has_valid_rows?(board),
9+
has_valid_columns?(board),
10+
has_valid_grids?(board)
11+
].all?
12+
end
13+
14+
private
15+
16+
# @param {Character[][]} board
17+
# @return {Boolean}
18+
def has_valid_rows?(board)
19+
board.all? { |row| is_valid_array?(row) }
20+
end
21+
22+
# @param {Character[]} arr
23+
# @return {Boolean}
24+
def is_valid_array?(arr)
25+
ref = [0] * 10
26+
27+
arr.each do |sq|
28+
next if sq == '.'
29+
30+
num = sq.to_i
31+
32+
return false if num.zero? || num > 9 || ref[num] == 1
33+
34+
ref[num] = 1
35+
end
36+
end
37+
38+
# @param {Character[][]} board
39+
# @return {Boolean}
40+
def has_valid_columns?(board)
41+
(0...9).each do |n|
42+
column = board.map { |row| row[n] }
43+
44+
return false unless is_valid_array?(column)
45+
end
46+
47+
true
48+
end
49+
50+
# @param {Character[][]} board
51+
# @return {Boolean}
52+
def has_valid_grids?(board)
53+
[0, 3, 6].each do |i|
54+
[0, 3, 6].each do |j|
55+
grid = [
56+
board[i][j],
57+
board[i][j + 1],
58+
board[i][j + 2],
59+
board[i + 1][j],
60+
board[i + 1][j + 1],
61+
board[i + 1][j + 2],
62+
board[i + 2][j],
63+
board[i + 2][j + 1],
64+
board[i + 2][j + 2]
65+
]
66+
67+
return false unless is_valid_array?(grid)
68+
end
69+
end
70+
71+
true
72+
end

test/medium/test_36_valid_sudoku.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/36_valid_sudoku'
5+
require 'minitest/autorun'
6+
7+
class ValidSudokuTest < ::Minitest::Test
8+
def test_default
9+
assert(
10+
is_valid_sudoku(
11+
[
12+
%w[5 3 . . 7 . . . .],
13+
%w[6 . . 1 9 5 . . .],
14+
%w[. 9 8 . . . . 6 .],
15+
%w[8 . . . 6 . . . 3],
16+
%w[4 . . 8 . 3 . . 1],
17+
%w[7 . . . 2 . . . 6],
18+
%w[. 6 . . . . 2 8 .],
19+
%w[. . . 4 1 9 . . 5],
20+
%w[. . . . 8 . . 7 9]
21+
]
22+
)
23+
)
24+
assert(
25+
!is_valid_sudoku(
26+
[
27+
%w[8 3 . . 7 . . . .],
28+
%w[6 . . 1 9 5 . . .],
29+
%w[. 9 8 . . . . 6 .],
30+
%w[8 . . . 6 . . . 3],
31+
%w[4 . . 8 . 3 . . 1],
32+
%w[7 . . . 2 . . . 6],
33+
%w[. 6 . . . . 2 8 .],
34+
%w[. . . 4 1 9 . . 5],
35+
%w[. . . . 8 . . 7 9]
36+
]
37+
)
38+
)
39+
end
40+
end

0 commit comments

Comments
 (0)