Skip to content

Commit 61d5ed2

Browse files
committed
2025-02-21 v. 8.6.7: added "1504. Count Submatrices With All Ones"
1 parent 1325556 commit 61d5ed2

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
700700
| 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) |
701701
| 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) |
702702
| 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) |
703+
| 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) |
703704
| 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) |
704705
| 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) |
705706
| 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) |

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 = '8.6.6'
8+
s.version = '8.6.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
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+
# https://leetcode.com/problems/count-submatrices-with-all-ones/
4+
# @param {Integer[][]} mat
5+
# @return {Integer}
6+
def num_submat(mat)
7+
n = mat[0].length
8+
result = 0
9+
h = ::Array.new(n, 0)
10+
11+
mat.each do |sub|
12+
f = [-1]
13+
s = [0]
14+
15+
n.times { |j| h[j] = sub[j].zero? ? 0 : h[j] + 1 }
16+
17+
n.times do |j|
18+
while f.first >= 0 && h[f.first] >= h[j]
19+
f.shift
20+
s.shift
21+
end
22+
23+
sum = h[j] * (j - f.first) + s.first
24+
25+
f.unshift(j)
26+
s.unshift(sum)
27+
28+
result += sum
29+
end
30+
end
31+
32+
result
33+
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/medium/1504_count_submatrices_with_all_ones'
5+
require 'minitest/autorun'
6+
7+
class CountSubmatricesWithAllOnesTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
13,
11+
num_submat(
12+
[
13+
[1, 0, 1],
14+
[1, 1, 0],
15+
[1, 1, 0]
16+
]
17+
)
18+
)
19+
end
20+
21+
def test_default_two
22+
assert_equal(
23+
24,
24+
num_submat(
25+
[
26+
[0, 1, 1, 0],
27+
[0, 1, 1, 1],
28+
[1, 1, 1, 0]
29+
]
30+
)
31+
)
32+
end
33+
end

0 commit comments

Comments
 (0)