Skip to content

Commit 3c36533

Browse files
committed
2024-07-23 v. 6.2.6: added "74. Search a 2D Matrix"
1 parent dbd137f commit 3c36533

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
491491
| 61. Rotate List | [Link](https://leetcode.com/problems/rotate-list/) | [Link](./lib/medium/61_rotate_list.rb) |
492492
| 62. Unique Paths | [Link](https://leetcode.com/problems/unique-paths/) | [Link](./lib/medium/62_unique_paths.rb) |
493493
| 71. Simplify Path | [Link](https://leetcode.com/problems/simplify-path/) | [Link](./lib/medium/71_simplify_path.rb) |
494+
| 74. Search a 2D Matrix | [Link](https://leetcode.com/problems/search-a-2d-matrix/) | [Link](./lib/medium/74_search_a_2d_matrix.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 = '6.2.5'
8+
s.version = '6.2.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/74_search_a_2d_matrix.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/search-a-2d-matrix/
4+
# @param {Integer[][]} matrix
5+
# @param {Integer} target
6+
# @return {Boolean}
7+
def search_matrix(matrix, target)
8+
i = 0
9+
j = matrix.first.length - 1
10+
11+
while i < matrix.length && j >= 0
12+
curr = matrix[i][j]
13+
14+
return true if curr == target
15+
16+
i += 1 if curr < target
17+
j -= 1 if curr > target
18+
end
19+
20+
false
21+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/74_search_a_2d_matrix'
5+
require 'minitest/autorun'
6+
7+
class SearchA2DMatrixTest < ::Minitest::Test
8+
def test_default
9+
assert(search_matrix([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 3))
10+
assert(!search_matrix([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 13))
11+
end
12+
end

0 commit comments

Comments
 (0)