Skip to content

2024-05-06 v. 5.6.1: added "2373. Largest Local Values in a Matrix" #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2024
Merged
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
@@ -421,3 +421,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 2363. Merge Similar Items | [Link](https://leetcode.com/problems/merge-similar-items/) | [Link](./lib/easy/2363_merge_similar_items.rb) |
| 2367. Number of Arithmetic Triplets | [Link](https://leetcode.com/problems/number-of-arithmetic-triplets/) | [Link](./lib/easy/2367_number_of_arithmetic_triplets.rb) |
| 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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '5.6.0'
s.version = '5.6.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
31 changes: 31 additions & 0 deletions lib/easy/2373_largest_local_values_in_a_matrix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# https://leetcode.com/problems/largest-local-values-in-a-matrix/
# @param {Integer[][]} grid
# @return {Integer[][]}
def largest_local(grid)
n = grid.length - 2
result = ::Array.new(n) { ::Array.new(n, 0) }
(0...n).each do |i|
(0...n).each do |j|
result[i][j] = highest(grid, i, j)
end
end

result
end

# @param {Integer[][]} grid
# @param {Integer} r
# @param {Integer} c
# @return {Integer}
def highest(grid, r, c)
max = 0
(r...(r + 3)).each do |i|
(c...(c + 3)).each do |j|
max = [max, grid[i][j]].max
end
end

max
end
33 changes: 33 additions & 0 deletions test/easy/test_2373_largest_local_values_in_a_matrix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2373_largest_local_values_in_a_matrix'
require 'minitest/autorun'

class LargestLocalValuesInAMatrixTest < ::Minitest::Test
def test_default
assert_equal(
[[9, 9], [8, 6]],
largest_local(
[
[9, 9, 8, 1],
[5, 6, 2, 6],
[8, 2, 6, 4],
[6, 2, 2, 2]
]
)
)
assert_equal(
[[2, 2, 2], [2, 2, 2], [2, 2, 2]],
largest_local(
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 2, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
)
)
end
end