当前位置:网站首页>[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
[daily problem insight] prefix and -- count the number of fertile pyramids in the farm
2022-07-07 00:26:00 【Code Fox】
️ Winter vacation xinkeng —— Code Fox's daily notes
The winter vacation is about to expire
2088. Count the number of fertile pyramids in the farm -Hard( Prefix and quick detection array )- The first 66 Biweekly competition question 4
There is one Rectangular grid A shapeless farm , Divided into m That's ok n Cell of column . Each grid is either Fertile ( use 1 Express ), Or poor Of ( use 0 Express ). All and grids outside the grid are considered barren .
On the farm The pyramid The area is defined as follows :
- Number of grids in the area Greater than
1And all the grids are Fertile . - The pyramid Apex It's this pyramid The top Lattice of . The height of the pyramid is the number of rows it covers . Make
(r, c)It is the top of the pyramid and its height ish, Then any grid contained in the pyramid area(i, j)Need to meetr <= i <= r + h - 1Andc - (i - r) <= j <= c + (i - r).
One Inverted Pyramid Similar definitions are as follows :
- Number of grids in the area Greater than
1And all the grids are Fertile . - Inverted pyramid Apex It's the inverted pyramid At the bottom Lattice of . The height of the inverted pyramid is the number of rows it covers . Make
(r, c)It is the top of the pyramid and its height ish, Then any grid contained in the pyramid area(i, j)Need to meetr - h + 1 <= i <= rAndc - (r - i) <= j <= c + (r - i).
class Solution {
public int countPyramids(int[][] grid) {
int m=grid.length;
int n=grid[0].length;
for(int i=0;i<m;i++){
for(int k=0;k<n;k++){
if(k!=0){
grid[i][k]+=grid[i][k-1];
}
}
}
int ans=0;
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
int h=1;
if(y==0&&grid[x][y]==0){
continue;
}
if(y!=0&&grid[x][y]-grid[x][y-1]==0){
continue;
}
while(x+h<m&&y+h<n&&y-h>=0){
int sum=0;
if(y-h==0){
sum=grid[x+h][y+h];
}
else{
sum=grid[x+h][y+h]-grid[x+h][y-h-1];
}
if(sum==2*h+1){
ans++;
h++;
}
else{
break;
}
}
h=1;
while(x-h>=0&&y+h<n&&y-h>=0){
int sum=0;
if(y-h==0){
sum=grid[x-h][y+h];
}
else{
sum=grid[x-h][y+h]-grid[x-h][y-h-1];
}
if(sum==2*h+1){
ans++;
h++;
}
else{
break;
}
}
}
}
return ans;
}
}
ending
Title source : Power button (LeetCode) link :https://leetcode-cn.com/problems
️ Focus on the author , Take you to brush the questions , Learn the most commonly used algorithm skills from simple algorithm problems ( Winter vacation every day )
️ Pay attention to the author's questions —— Simple to advanced , Let you unknowingly become a ruthless problem brushing machine , If you have any questions, please send a private letter
边栏推荐
- uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
- PXE server configuration
- Racher integrates LDAP to realize unified account login
- DAY FIVE
- Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
- GPIO简介
- Leecode brushes questions to record interview questions 17.16 massagist
- Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
- 509 certificat basé sur Go
- 在docker中快速使用各个版本的PostgreSQL数据库
猜你喜欢

Introduction au GPIO

509 certificat basé sur Go

Everyone is always talking about EQ, so what is EQ?

基于GO语言实现的X.509证书

VTK volume rendering program design of 3D scanned volume data

iMeta | 华南农大陈程杰/夏瑞等发布TBtools构造Circos图的简单方法

After leaving a foreign company, I know what respect and compliance are

Core knowledge of distributed cache

DAY FOUR

DAY FOUR
随机推荐
web渗透测试是什么_渗透实战
pytest多进程/多线程执行测试用例
17、 MySQL - high availability + read / write separation + gtid + semi synchronous master-slave replication cluster
rancher集成ldap,实现统一账号登录
Use type aliases in typescript
AVL树到底是什么?
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
Introduction to GPIO
DAY SIX
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
【向量检索研究系列】产品介绍
C语言输入/输出流和文件操作【二】
How rider uses nuget package offline
Devops can help reduce technology debt in ten ways
What is AVL tree?
1000 words selected - interface test basis
【自动化测试框架】关于unittest你需要知道的事
微信小程序uploadfile服务器,微信小程序之wx.uploadFile[通俗易懂]
app通用功能測試用例
DAY FIVE