Skip to content

2025-02-21 v. 8.6.7: added "1504. Count Submatrices With All Ones" #956

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
Feb 21, 2025
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 @@ -700,6 +700,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1472. Design Browser History | [Link](https://leetcode.com/problems/design-browser-history/) | [Link](./lib/medium/1472_design_browser_history.rb) | [Link](./test/medium/test_1472_design_browser_history.rb) |
| 1481. Least Number of Unique Integers after K Removals | [Link](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [Link](./lib/medium/1481_least_number_of_unique_integers_after_k_removals.rb) | [Link](./test/medium/test_1481_least_number_of_unique_integers_after_k_removals.rb) |
| 1492. The kth Factor of n | [Link](https://leetcode.com/problems/the-kth-factor-of-n/) | [Link](./lib/medium/1492_the_kth_factor_of_n.rb) | [Link](./test/medium/test_1492_the_kth_factor_of_n.rb) |
| 1504. Count Submatrices With All Ones | [Link](https://leetcode.com/problems/count-submatrices-with-all-ones/) | [Link](./lib/medium/1504_count_submatrices_with_all_ones.rb) | [Link](./test/medium/test_1504_count_submatrices_with_all_ones.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
Expand Down
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 = '8.6.6'
s.version = '8.6.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
33 changes: 33 additions & 0 deletions lib/medium/1504_count_submatrices_with_all_ones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# https://leetcode.com/problems/count-submatrices-with-all-ones/
# @param {Integer[][]} mat
# @return {Integer}
def num_submat(mat)
n = mat[0].length
result = 0
h = ::Array.new(n, 0)

mat.each do |sub|
f = [-1]
s = [0]

n.times { |j| h[j] = sub[j].zero? ? 0 : h[j] + 1 }

n.times do |j|
while f.first >= 0 && h[f.first] >= h[j]
f.shift
s.shift
end

sum = h[j] * (j - f.first) + s.first

f.unshift(j)
s.unshift(sum)

result += sum
end
end

result
end
33 changes: 33 additions & 0 deletions test/medium/test_1504_count_submatrices_with_all_ones.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/medium/1504_count_submatrices_with_all_ones'
require 'minitest/autorun'

class CountSubmatricesWithAllOnesTest < ::Minitest::Test
def test_default_one
assert_equal(
13,
num_submat(
[
[1, 0, 1],
[1, 1, 0],
[1, 1, 0]
]
)
)
end

def test_default_two
assert_equal(
24,
num_submat(
[
[0, 1, 1, 0],
[0, 1, 1, 1],
[1, 1, 1, 0]
]
)
)
end
end