Skip to content

Commit e922c83

Browse files
authored
2025-04-03 v. 9.1.8: added "2326. Spiral Matrix IV"
2 parents 58ccf02 + 695722c commit e922c83

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
752752
| 2265. Count Nodes Equal to Average of Subtree | [Link](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [Link](./lib/medium/2265_count_nodes_equal_to_average_of_subtree.rb) | [Link](./test/medium/test_2265_count_nodes_equal_to_average_of_subtree.rb) |
753753
| 2266. Count Number of Texts | [Link](https://leetcode.com/problems/count-number-of-texts/) | [Link](./lib/medium/2266_count_number_of_texts.rb) | [Link](./test/medium/test_2266_count_number_of_texts.rb) |
754754
| 2295. Replace Elements in an Array | [Link](https://leetcode.com/problems/replace-elements-in-an-array/) | [Link](./lib/medium/2295_replace_elements_in_an_array.rb) | [Link](./test/medium/test_2295_replace_elements_in_an_array.rb) |
755+
| 2326. Spiral Matrix IV | [Link](https://leetcode.com/problems/spiral-matrix-iv/) | [Link](./lib/medium/2326_spiral_matrix_iv.rb) | [Link](./test/medium/test_2326_spiral_matrix_iv.rb) |
755756
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
756757
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
757758
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '9.1.7'
8+
s.version = '9.1.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/2326_spiral_matrix_iv.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/spiral-matrix-iv/
4+
# @param {Integer} m
5+
# @param {Integer} n
6+
# @param {ListNode} head
7+
# @return {Integer[][]}
8+
def spiral_matrix(m, n, head)
9+
matrix = ::Array.new(m) { ::Array.new(n, -1) }
10+
11+
return matrix unless head
12+
13+
directions = [
14+
[0, 1],
15+
[1, 0],
16+
[0, -1],
17+
[-1, 0]
18+
]
19+
current_dir = 0
20+
row = 0
21+
col = 0
22+
current = head
23+
24+
while current
25+
matrix[row][col] = current.val
26+
current = current.next
27+
28+
next_row = row + directions[current_dir][0]
29+
next_col = col + directions[current_dir][1]
30+
31+
if next_row.negative? || next_row >= m || next_col.negative? || next_col >= n || matrix[next_row][next_col] != -1
32+
current_dir = (current_dir + 1) % 4
33+
next_row = row + directions[current_dir][0]
34+
next_col = col + directions[current_dir][1]
35+
end
36+
37+
row = next_row
38+
col = next_col
39+
end
40+
41+
matrix
42+
end
Lines changed: 40 additions & 0 deletions
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/common/linked_list'
5+
require_relative '../../lib/medium/2326_spiral_matrix_iv'
6+
require 'minitest/autorun'
7+
8+
class SpiralMatrixIVTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
[
12+
[3, 0, 2, 6, 8],
13+
[5, 0, -1, -1, 1],
14+
[5, 2, 4, 9, 7]
15+
],
16+
spiral_matrix(
17+
3,
18+
5,
19+
::ListNode.from_array(
20+
[3, 0, 2, 6, 8, 1, 7, 9, 4, 2, 5, 5, 0]
21+
)
22+
)
23+
)
24+
end
25+
26+
def test_default_two
27+
assert_equal(
28+
[
29+
[0, 1, 2, -1]
30+
],
31+
spiral_matrix(
32+
1,
33+
4,
34+
::ListNode.from_array(
35+
[0, 1, 2]
36+
)
37+
)
38+
)
39+
end
40+
end

0 commit comments

Comments
 (0)