Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Programmer-Admin
binarysearch-editorials
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
binarysearch-editorials
/
Count Square Submatrices.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
23 lines (21 loc) · 811 Bytes
main
Breadcrumbs
binarysearch-editorials
/
Count Square Submatrices.py
Copy path
Top
File metadata and controls
Code
Blame
23 lines (21 loc) · 811 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Count Square Submatrices
To make a 2x2 square from a 1x1 square, each of its top, left and top-left neighbors must be one:
1 1
1 2
This extends to squares of any dimensions - it is always constraint by the smallest neighbor:
1 2
2 2
Will also give give a square of 2x2, we are missing the top-left cell to make a 3x3.
We can take the min of those three neighbors + 1, get the number of square this cell belongs to, and finally add that number to our answer.
"""
class Solution:
def solve(self, matrix):
ans=0
for i,r in enumerate(matrix):
for j,v in enumerate(r):
if v:
if i>0 and j>0:
matrix[i][j] += min(matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1])
ans += matrix[i][j]
return ans
You can’t perform that action at this time.