当前位置:网站首页>[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
1
And 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 - 1
Andc - (i - r) <= j <= c + (i - r)
.
One Inverted Pyramid Similar definitions are as follows :
- Number of grids in the area Greater than
1
And 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 <= r
Andc - (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
边栏推荐
- A way of writing SQL, update when matching, or insert
- MySQL master-slave multi-source replication (3 master and 1 slave) setup and synchronization test
- MIT 6.824 - raft Student Guide
- Encryption algorithm - password security
- Leecode brushes questions to record interview questions 17.16 massagist
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- 基于SSM框架的文章管理系统
- 【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
- ldap创建公司组织、人员
- DAY FOUR
猜你喜欢
37页数字乡村振兴智慧农业整体规划建设方案
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
48页数字政府智慧政务一网通办解决方案
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
DAY ONE
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
2022/2/10 summary
37頁數字鄉村振興智慧農業整體規劃建設方案
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
随机推荐
MIT 6.824 - Raft学生指南
Jenkins' user credentials plug-in installation
什么是响应式对象?响应式对象的创建过程?
uniapp中redirectTo和navigateTo的区别
Testers, how to prepare test data
Compilation of kickstart file
37頁數字鄉村振興智慧農業整體規劃建設方案
Use source code compilation to install postgresql13.3 database
在Docker中分分钟拥有Oracle EMCC 13.5环境
2022年PMP项目管理考试敏捷知识点(9)
1000 words selected - interface test basis
Random类的那些事
vector的使用方法_vector指针如何使用
Pinia module division
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
Introduction to GPIO
Why should a complete knapsack be traversed in sequence? Briefly explain
Business process testing based on functional testing
"Latex" Introduction to latex mathematical formula "suggestions collection"
DAY ONE