当前位置:网站首页>[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
边栏推荐
- PostgreSQL使用Pgpool-II实现读写分离+负载均衡
- SQL的一种写法,匹配就更新,否则就是插入
- Encryption algorithm - password security
- Leecode brush questions record sword finger offer 44 A digit in a sequence of numbers
- Rails 4 asset pipeline vendor asset images are not precompiled
- Supersocket 1.6 creates a simple socket server with message length in the header
- 数据运营平台-数据采集[通俗易懂]
- MySQL主从之多源复制(3主1从)搭建及同步测试
- Automatic test tool katalon (WEB) test operation instructions
- PXE server configuration
猜你喜欢
AVL树到底是什么?
刘永鑫报告|微生物组数据分析与科学传播(晚7点半)
X.509 certificate based on go language
互动滑轨屏演示能为企业展厅带来什么
Building lease management system based on SSM framework
VTK volume rendering program design of 3D scanned volume data
Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
基於GO語言實現的X.509證書
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
2022年PMP项目管理考试敏捷知识点(9)
随机推荐
pinia 模块划分
【精品】pinia 基于插件pinia-plugin-persist的 持久化
37页数字乡村振兴智慧农业整体规划建设方案
【自动化测试框架】关于unittest你需要知道的事
华为mate8电池价格_华为mate8换电池后充电巨慢
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
openresty ngx_lua子请求
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
【CVPR 2022】目标检测SOTA:DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection
DAY FOUR
Why should a complete knapsack be traversed in sequence? Briefly explain
What is a responsive object? How to create a responsive object?
Random类的那些事
Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)
Core knowledge of distributed cache
Data operation platform - data collection [easy to understand]
什么是响应式对象?响应式对象的创建过程?
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
How engineers treat open source -- the heartfelt words of an old engineer