Skip to content

Commit 30e6af9

Browse files
authored
Merge pull request #618 from fartem/2373_Largest_Local_Values_in_a_Matrix
2024-05-06 v. 5.6.1: added "2373. Largest Local Values in a Matrix"
2 parents a7b2a1d + d23c9ea commit 30e6af9

4 files changed

+66
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
421421
| 2357. Make Array Zero by Subtracting Equal Amounts | [Link](https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/) | [Link](./lib/easy/2357_make_array_zero_by_subtracting_equal_amounts.rb) |
422422
| 2363. Merge Similar Items | [Link](https://leetcode.com/problems/merge-similar-items/) | [Link](./lib/easy/2363_merge_similar_items.rb) |
423423
| 2367. Number of Arithmetic Triplets | [Link](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [Link](./lib/easy/2367_number_of_arithmetic_triplets.rb) |
424+
| 2373. Largest Local Values in a Matrix | [Link](https://leetcode.com/problems/largest-local-values-in-a-matrix/) | [Link](./lib/easy/2373_largest_local_values_in_a_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 = '5.6.0'
8+
s.version = '5.6.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
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+
# https://leetcode.com/problems/largest-local-values-in-a-matrix/
4+
# @param {Integer[][]} grid
5+
# @return {Integer[][]}
6+
def largest_local(grid)
7+
n = grid.length - 2
8+
result = ::Array.new(n) { ::Array.new(n, 0) }
9+
(0...n).each do |i|
10+
(0...n).each do |j|
11+
result[i][j] = highest(grid, i, j)
12+
end
13+
end
14+
15+
result
16+
end
17+
18+
# @param {Integer[][]} grid
19+
# @param {Integer} r
20+
# @param {Integer} c
21+
# @return {Integer}
22+
def highest(grid, r, c)
23+
max = 0
24+
(r...(r + 3)).each do |i|
25+
(c...(c + 3)).each do |j|
26+
max = [max, grid[i][j]].max
27+
end
28+
end
29+
30+
max
31+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2373_largest_local_values_in_a_matrix'
5+
require 'minitest/autorun'
6+
7+
class LargestLocalValuesInAMatrixTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(
10+
[[9, 9], [8, 6]],
11+
largest_local(
12+
[
13+
[9, 9, 8, 1],
14+
[5, 6, 2, 6],
15+
[8, 2, 6, 4],
16+
[6, 2, 2, 2]
17+
]
18+
)
19+
)
20+
assert_equal(
21+
[[2, 2, 2], [2, 2, 2], [2, 2, 2]],
22+
largest_local(
23+
[
24+
[1, 1, 1, 1, 1],
25+
[1, 1, 1, 1, 1],
26+
[1, 1, 2, 1, 1],
27+
[1, 1, 1, 1, 1],
28+
[1, 1, 1, 1, 1]
29+
]
30+
)
31+
)
32+
end
33+
end

0 commit comments

Comments
 (0)