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 @@ -543,3 +543,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 236. Lowest Common Ancestor of a Binary Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Link](./lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb) | [Link](./test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb) |
| 237. Delete Node in a Linked List | [Link](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Link](./lib/medium/237_delete_node_in_a_linked_list.rb) | [Link](./test/medium/test_237_delete_node_in_a_linked_list.rb) |
| 238. Product of Array Except Self | [Link](https://leetcode.com/problems/product-of-array-except-self/) | [Link](./lib/medium/238_product_of_array_except_self.rb) | [Link](./test/medium/test_238_product_of_array_except_self.rb) |
| 240. Search a 2D Matrix II | [Link](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [Link](./lib/medium/240_search_a_2d_matrix_ii.rb) | [Link](./test/medium/test_240_search_a_2d_matrix_ii.rb) |
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 = '6.7.7'
s.version = '6.7.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
27 changes: 27 additions & 0 deletions lib/medium/240_search_a_2d_matrix_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# https://leetcode.com/problems/search-a-2d-matrix-ii/
# @param {Integer[][]} matrix
# @param {Integer} target
# @return {Boolean}
def search_matrix240(matrix, target)
matrix.each do |matr|
l = 0
r = matrix.first.length - 1

while l <= r
m = (l + r) / 2
curr = matr[m]

return true if curr == target

if curr > target
r = m - 1
else
l = m + 1
end
end
end

false
end
37 changes: 37 additions & 0 deletions test/medium/test_240_search_a_2d_matrix_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/240_search_a_2d_matrix_ii'
require 'minitest/autorun'

class SearchA2DMatrixIITest < ::Minitest::Test
def test_default_one
assert(
search_matrix240(
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
],
5
)
)
end

def test_default_two
assert(
!search_matrix240(
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
],
20
)
)
end
end