Skip to content

Commit 2e5568a

Browse files
authored
Merge pull request #761 from fartem/378_Kth_Smallest_Element_in_a_Sorted_Matrix
2024-10-31 v. 6.9.3: added "378. Kth Smallest Element in a Sorted Matrix"
2 parents c29039a + 5153895 commit 2e5568a

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
558558
| 347. Top K Frequent Elements | [Link](https://leetcode.com/problems/top-k-frequent-elements/) | [Link](./lib/medium/347_top_k_frequent_elements.rb) | [Link](./test/medium/test_347_top_k_frequent_elements.rb) |
559559
| 371. Sum of Two Integers | [Link](https://leetcode.com/problems/sum-of-two-integers/) | [Link](./lib/medium/371_sum_of_two_integers.rb) | [Link](./test/medium/test_371_sum_of_two_integers.rb) |
560560
| 377. Combination Sum IV | [Link](https://leetcode.com/problems/combination-sum-iv/) | [Link](./lib/medium/377_combination_sum_iv.rb) | [Link](./test/medium/test_377_combination_sum_iv.rb) |
561+
| 378. Kth Smallest Element in a Sorted Matrix | [Link](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [Link](./lib/medium/378_kth_smallest_element_in_a_sorted_matrix.rb) | [Link](./test/medium/test_378_kth_smallest_element_in_a_sorted_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.9.2'
8+
s.version = '6.9.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
4+
# @param {Integer[][]} matrix
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def kth_smallest378(matrix, k)
8+
n = matrix.length
9+
l = matrix[0][0]
10+
h = matrix[n - 1][n - 1]
11+
m = 0
12+
temp = 0
13+
count = 0
14+
15+
while l < h
16+
m = l + (h - l) / 2
17+
temp = n - 1
18+
count = 0
19+
20+
matrix.each do |mat|
21+
temp -= 1 while temp >= 0 && mat[temp] > m
22+
23+
count += (temp + 1)
24+
end
25+
26+
if count < k
27+
l = m + 1
28+
else
29+
h = m
30+
end
31+
end
32+
33+
l
34+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/378_kth_smallest_element_in_a_sorted_matrix'
5+
require 'minitest/autorun'
6+
7+
class KthSmallestElementInASortedMatrixTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
13,
11+
kth_smallest378(
12+
[
13+
[1, 5, 9],
14+
[10, 11, 13],
15+
[12, 13, 15]
16+
],
17+
8
18+
)
19+
)
20+
end
21+
22+
def test_default_two
23+
assert_equal(
24+
-5,
25+
kth_smallest378(
26+
[[-5]],
27+
1
28+
)
29+
)
30+
end
31+
end

0 commit comments

Comments
 (0)