当前位置:网站首页>[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
边栏推荐
- 37頁數字鄉村振興智慧農業整體規劃建設方案
- 一图看懂对程序员的误解:西方程序员眼中的中国程序员
- Automatic test tool katalon (WEB) test operation instructions
- 2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
- ldap创建公司组织、人员
- Interesting wine culture
- 48页数字政府智慧政务一网通办解决方案
- How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
- 2022/2/12 summary
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
猜你喜欢

工程师如何对待开源 --- 一个老工程师的肺腑之言

量子时代计算机怎么保证数据安全?美国公布四项备选加密算法

JWT signature does not match locally computed signature. JWT validity cannot be asserted and should

Liuyongxin report | microbiome data analysis and science communication (7:30 p.m.)

DAY SIX

互动滑轨屏演示能为企业展厅带来什么

准备好在CI/CD中自动化持续部署了吗?

Business process testing based on functional testing

What is a responsive object? How to create a responsive object?

rancher集成ldap,实现统一账号登录
随机推荐
ldap创建公司组织、人员
Leecode brush question record sword finger offer 56 - ii Number of occurrences of numbers in the array II
System activity monitor ISTAT menus 6.61 (1185) Chinese repair
JS import excel & Export Excel
Compilation of kickstart file
okcc呼叫中心的订单管理时怎么样的
Building lease management system based on SSM framework
Everyone is always talking about EQ, so what is EQ?
App general function test cases
uniapp中redirectTo和navigateTo的区别
Encryption algorithm - password security
基於GO語言實現的X.509證書
DAY FIVE
GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
Data operation platform - data collection [easy to understand]
什么是响应式对象?响应式对象的创建过程?
数据运营平台-数据采集[通俗易懂]
Racher integrates LDAP to realize unified account login
How engineers treat open source -- the heartfelt words of an old engineer