From bddc0e79110e20e8824dca79f9fe0c3d52519b2d Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 3 Mar 2025 09:35:24 +0300 Subject: [PATCH] 2025-03-03 v. 8.7.9: added "1706. Where Will the Ball Fall" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/1706_where_will_the_ball_fall.rb | 38 +++++++++++++++ .../test_1706_where_will_the_ball_fall.rb | 47 +++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1706_where_will_the_ball_fall.rb create mode 100644 test/medium/test_1706_where_will_the_ball_fall.rb diff --git a/README.md b/README.md index 3a6e0ce2..adbfc391 100644 --- a/README.md +++ b/README.md @@ -712,6 +712,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1670. Design Front Middle Back Queue | [Link](https://leetcode.com/problems/design-front-middle-back-queue/) | [Link](./lib/medium/1670_design_front_middle_back_queue.rb) | [Link](./test/medium/test_1670_design_front_middle_back_queue.rb) | | 1679. Max Number of K-Sum Pairs | [Link](https://leetcode.com/problems/max-number-of-k-sum-pairs/) | [Link](./lib/medium/1679_max_number_of_k_sum_pairs.rb) | [Link](./test/medium/test_1679_max_number_of_k_sum_pairs.rb) | | 1685. Sum of Absolute Differences in a Sorted Array | [Link](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/) | [Link](./lib/medium/1685_sum_of_absolute_differences_in_a_sorted_array.rb) | [Link](./test/medium/test_1685_sum_of_absolute_differences_in_a_sorted_array.rb) | +| 1706. Where Will the Ball Fall | [Link](https://leetcode.com/problems/where-will-the-ball-fall/) | [Link](./lib/medium/1706_where_will_the_ball_fall.rb) | [Link](./test/medium/test_1706_where_will_the_ball_fall.rb) | | 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index acfa1379..3bfa0af0 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 = '8.7.8' + s.version = '8.7.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1706_where_will_the_ball_fall.rb b/lib/medium/1706_where_will_the_ball_fall.rb new file mode 100644 index 00000000..cc588474 --- /dev/null +++ b/lib/medium/1706_where_will_the_ball_fall.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/where-will-the-ball-fall/ +# @param {Integer[][]} grid +# @return {Integer[]} +def find_ball(grid) + c = grid[0].size + result = ::Array.new(c, 0) + + (0...c).each { |i| result[i] = dfs_for_find_ball(grid, 0, i) } + + result +end + +private + +# @param {Integer[][]} grid +# @param {Integer} i +# @param {Integer} j +# @return {Integer} +def dfs_for_find_ball(grid, i, j) + curr = grid[i][j] + + return -1 if (j.zero? && curr == -1) || (j == grid[i].size - 1 && curr == 1) + + return -1 if curr == 1 && curr != grid[i][j + 1] + + return -1 if curr != 1 && curr != grid[i][j - 1] + + return curr == 1 ? j + 1 : j - 1 if i == grid.size - 1 + + return curr == 1 ? j + 1 : j - 1 if i == grid.size - 1 + + i += 1 + j += curr == 1 ? 1 : -1 + + dfs_for_find_ball(grid, i, j) +end diff --git a/test/medium/test_1706_where_will_the_ball_fall.rb b/test/medium/test_1706_where_will_the_ball_fall.rb new file mode 100644 index 00000000..1742ce0b --- /dev/null +++ b/test/medium/test_1706_where_will_the_ball_fall.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1706_where_will_the_ball_fall' +require 'minitest/autorun' + +class WhereWillTheBallFallTest < ::Minitest::Test + def test_default_one + assert_equal( + [1, -1, -1, -1, -1], + find_ball( + [ + [1, 1, 1, -1, -1], + [1, 1, 1, -1, -1], + [-1, -1, -1, 1, 1], + [1, 1, 1, 1, -1], + [-1, -1, -1, -1, -1] + ] + ) + ) + end + + def test_default_two + assert_equal( + [-1], + find_ball( + [ + [-1] + ] + ) + ) + end + + def test_default_three + assert_equal( + [0, 1, 2, 3, 4, -1], + find_ball( + [ + [1, 1, 1, 1, 1, 1], + [-1, -1, -1, -1, -1, -1], + [1, 1, 1, 1, 1, 1], + [-1, -1, -1, -1, -1, -1] + ] + ) + ) + end +end