Skip to content

1277. Count Square Submatrices with All Ones #188

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
Oct 27, 2024
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 @@ -345,6 +345,7 @@
| 1266 | Minimum Time Visiting All Points | [Ruby](./algorithms/ruby/1266-minimum-time-visiting-all-points.rb) | Easy |
| 1268 | Search Suggestions System | [Ruby](./algorithms/ruby/1268-search-suggestions-system.rb) | Medium |
| 1269 | Number of Ways to Stay in the Same Place After Some Steps | [Ruby](./algorithms/ruby/1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.rb) | Hard |
| 1277 | Count Square Submatrices with All Ones | [Ruby](./algorithms/ruby/1277-count-square-submatrices-with-all-ones.rb) | Medium |
| 1282 | Group the People Given the Group Size They Belong To | [Ruby](./algorithms/ruby/1282-group-the-people-given-the-group-size-they-belong-to.rb) | Medium |
| 1287 | Element Appearing More Than 25% In Sorted Array | [Ruby](./algorithms/ruby/1287-element-appearing-more-than-25-in-sorted-array.rb) | Easy |
| 1318 | Minimum Flips to Make a OR b Equal to c | [Ruby](./algorithms/ruby/1318-minimum-flips-to-make-a-or-b-equal-to-c.rb) | Medium |
Expand Down
69 changes: 69 additions & 0 deletions algorithms/ruby/1277-count-square-submatrices-with-all-ones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

# 1277. Count Square Submatrices with All Ones
# Medium
# https://leetcode.com/problems/count-square-submatrices-with-all-ones

=begin
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.

Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.

Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.

Constraints:
* 1 <= matrix.length <= 300
* 1 <= matrix[0].length <= 300
* 0 <= matrix[i][j] <= 1
=end

# @param {Integer[][]} matrix
# @return {Integer}
def count_squares(matrix)
m, n = matrix.length, matrix[0].length
result = 0
(0...m).each { |i|
(0...n).each { |j|
if matrix[i][j] != 0 && i > 0 && j > 0
matrix[i][j] = 1 + [matrix[i - 1][j], matrix[i - 1][j - 1], matrix[i][j - 1]].min
end
result += matrix[i][j]
}
}
result
end

# **************** #
# TEST #
# **************** #

require "test/unit"
class Test_count_squares < Test::Unit::TestCase
def test_
assert_equal 15, count_squares([[0, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1]])
assert_equal 7, count_squares([[1, 0, 1], [1, 1, 0], [1, 1, 0]])
end
end