Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
38 changes: 38 additions & 0 deletions lib/medium/1706_where_will_the_ball_fall.rb
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions test/medium/test_1706_where_will_the_ball_fall.rb
Original file line number Diff line number Diff line change
@@ -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