当前位置:网站首页>leetcode:1504. 统计全 1 子矩形的个数
leetcode:1504. 统计全 1 子矩形的个数
2022-06-24 19:28:00 【OceanStar的学习笔记】
题目来源
题目描述


class Solution {
public:
int numSubmat(vector<vector<int>>& mat) {
}
};
题目解析
问题:
- 必须以第0行为底的矩阵,其内部全是1的有多少个
- 必须以第1行为底的矩阵,其内部全是1的有多少个
- 必须以第2行为底的矩阵,其内部全是1的有多少个
- …
- 然后将上面的全部加起来,就是答案
举个例子:
- 必须以第0行为第的矩阵,其内部全是1的子矩阵有:
{a}、{b}、{a,b]- 一共
3个
- 必须以第1行为底的矩阵,其内部全是1的有多少个
{c}、{d}、{c,d}、{a,c}、{b,d}、{a,b,c,d}
那,以某一行为底的矩阵,其内部全是1的有多少个,应该怎么求呢?
- 在求以某一行打底时,可以按照数组直方图的思路,将当前行连同上面各行的数据处理下列,形成一个直方图,然后拿着直方图数组,去求个数
- 拿数组直方图求答案时,【单调栈】:
- 统计每个位置index,左侧比自己小、离自己最近的元素位置 leftIndex;
- 右侧比自己小、离自己最近的元素位置 rightIndex;
- 则,形成的数组直方图长度L = rightIndex - leftIndex + 1
- 如果只考虑当前打底行,个数 = L + (L-1) + (L - 2) + … + 1 = (L * (L + 1))/2;
- 还需要考虑直方图的高度 h ,总个数 = h * ((L * (L + 1))/2);
- 但这样会重复,为了避免重复,这里的 h = [index] - max([leftIndex], [rightIndex]); ??? (待补充)
- 所以,计算公式 =
( (L * (L + 1))/2 ) * ( [index] - max([leftIndex], [rightIndex]) )
class Solution {
// 计算以某一行打底(形成的直方图数组height)的全1子矩阵的数量
int process(std::vector<int> & height){
int m = height.size();
int count = 0;
std::stack<int> stack; // 单调栈:栈底 -> 栈顶: 由小 -> 大
// 每个元素依次入栈,维持单调栈的严格单调递增结构,不符合时,弹出元素,弹出即结算
for (int i = 0; i < m; ++i) {
// 维持单调栈的严格单调递增结构,不符合时,弹出元素,弹出即结算
while (!stack.empty() && height[i] <= height[stack.top()]){
int index = stack.top(); stack.pop();
int leftIndex = stack.empty() ? -1 : stack.top();// 左侧比[index]小、离index最近的元素位置
int rightIndex = i; // 右侧比[index]小、离index最近的元素位置
int wid = rightIndex - leftIndex - 1; // 形成的直方图长度
int hei = height[index] - std::max(
leftIndex == -1 ? 0 : height[leftIndex], height[rightIndex]
);
count += ((wid * (wid+1))/2) * hei; // 公式计算个数
}
stack.push(i);
}
// 结算单调栈中最后剩下的元素,弹出元素,弹出即结算
while (!stack.empty()) {
int index = stack.top(); stack.pop();
int leftIndex = stack.empty() ? -1 : stack.top();// 左侧比[index]小、离index最近的元素位置
int rightIndex = m; // 右侧比[index]小、离index最近的元素位置
int l = rightIndex - leftIndex - 1; // 形成的直方图长度
// 形成的直方图的有效结算高度 h :
int h = height[index] - (leftIndex == -1 ? 0 : height[leftIndex]);
count += ((l * (l+1))/2) * h; // 公式计算个数
}
return count;
}
public:
int numSubmat(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
int ans = 0;
std::vector<int> height(m); // 以每一行打底的直方图数组
for (int i = 0; i < m; ++i) {
// 计算以每一行打底的直方图
for (int j = 0; j < n; ++j) {
height[j] = mat[i][j] == 0 ? 0 : height[j] + 1;
}
// 计算以每一行打底时的全1子矩阵的数量
int cnt = process(height);
ans += cnt;
}
return ans;
}
};
优化方法是使用数组来实现栈
边栏推荐
- Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
- 建木持续集成平台v2.5.0发布
- leetcode_1470_2021.10.12
- memcached完全剖析–1. memcached的基础
- 即构「畅直播」上线!提供全链路升级的一站式直播服务
- 数据链路层 && 一些其他的协议or技术
- [camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
- [Web Security Basics] some details
- Network layer & IP
- Remove the screen recording reminder (seven cattle cloud demo)
猜你喜欢

Memcached comprehensive analysis – 5 Memcached applications and compatible programs

(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识

The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich

memcached全面剖析–5. memcached的应用和兼容程序

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter

Tutorial on obtaining JD cookies by mobile browser
Visit Amazon memorydb and build your own redis memory database

Remove the screen recording reminder (seven cattle cloud demo)

leetcode-201_2021_10_17

Wireshark packet capturing skills summarized by myself
随机推荐
Intelligent fish tank control system based on STM32 under Internet of things
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Analysis of tcpdump packet capturing kernel code
Alibaba cloud lightweight servers open designated ports
Memcached full profiling – 1 Fundamentals of memcached
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
Multi view function in blender
About transform InverseTransformPoint, transform. InverseTransofrmDirection
RFC 793 why to send reset and when to send reset
Li Kou daily question - day 25 -496 Next larger element I
Three more days
Prompt that the device has no permission when using ADB to connect to the device
Antdb database online training has started! More flexible, professional and rich
Station B takes goods to learn from New Oriental
Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
02---纵波不可能产生的现象
Understanding openstack network
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
推荐模型之多任务模型:ESMM、MMOE
Docking of arkit and character creator animation curves