From 695722c6e92037186b7cf932f6aca4d1e5e23bdc Mon Sep 17 00:00:00 2001 From: fartem Date: Thu, 3 Apr 2025 07:29:33 +0300 Subject: [PATCH] 2025-04-03 v. 9.1.8: added "2326. Spiral Matrix IV" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/2326_spiral_matrix_iv.rb | 42 +++++++++++++++++++++++ test/medium/test_2326_spiral_matrix_iv.rb | 40 +++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 lib/medium/2326_spiral_matrix_iv.rb create mode 100644 test/medium/test_2326_spiral_matrix_iv.rb diff --git a/README.md b/README.md index 65ef8263..79a85f7d 100644 --- a/README.md +++ b/README.md @@ -752,6 +752,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | | 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) | | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index da6e3499..98ca6d54 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '9.1.7' + s.version = '9.1.8' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/2326_spiral_matrix_iv.rb b/lib/medium/2326_spiral_matrix_iv.rb new file mode 100644 index 00000000..5087207d --- /dev/null +++ b/lib/medium/2326_spiral_matrix_iv.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/spiral-matrix-iv/ +# @param {Integer} m +# @param {Integer} n +# @param {ListNode} head +# @return {Integer[][]} +def spiral_matrix(m, n, head) + matrix = ::Array.new(m) { ::Array.new(n, -1) } + + return matrix unless head + + directions = [ + [0, 1], + [1, 0], + [0, -1], + [-1, 0] + ] + current_dir = 0 + row = 0 + col = 0 + current = head + + while current + matrix[row][col] = current.val + current = current.next + + next_row = row + directions[current_dir][0] + next_col = col + directions[current_dir][1] + + if next_row.negative? || next_row >= m || next_col.negative? || next_col >= n || matrix[next_row][next_col] != -1 + current_dir = (current_dir + 1) % 4 + next_row = row + directions[current_dir][0] + next_col = col + directions[current_dir][1] + end + + row = next_row + col = next_col + end + + matrix +end diff --git a/test/medium/test_2326_spiral_matrix_iv.rb b/test/medium/test_2326_spiral_matrix_iv.rb new file mode 100644 index 00000000..38ab2d5c --- /dev/null +++ b/test/medium/test_2326_spiral_matrix_iv.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/common/linked_list' +require_relative '../../lib/medium/2326_spiral_matrix_iv' +require 'minitest/autorun' + +class SpiralMatrixIVTest < ::Minitest::Test + def test_default_one + assert_equal( + [ + [3, 0, 2, 6, 8], + [5, 0, -1, -1, 1], + [5, 2, 4, 9, 7] + ], + spiral_matrix( + 3, + 5, + ::ListNode.from_array( + [3, 0, 2, 6, 8, 1, 7, 9, 4, 2, 5, 5, 0] + ) + ) + ) + end + + def test_default_two + assert_equal( + [ + [0, 1, 2, -1] + ], + spiral_matrix( + 1, + 4, + ::ListNode.from_array( + [0, 1, 2] + ) + ) + ) + end +end