13.4—动态规划—Maximal Rectangle
来源:互联网 发布:linux不保存退出 编辑:程序博客网 时间:2024/05/29 07:46
描述
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and
return its area.
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and
return its area.
#include<iostream>#include<vector>#include<stack>using namespace std;int mymax(int a, int b){return a > b ? a : b;}int MaxRectangle(vector<int> input){if (input.size() < 1)return 0;int result = 0;input.push_back(0);stack<int>s;for (int i = 0; i < input.size();){if (s.empty() || input[i] > input[s.top()])s.push(i++);else{int tmp = s.top();s.pop();int index = s.empty() ? i : i - s.top() - 1;result = mymax(result, input[tmp] * index);}}return result;}int MaxRectangle(const vector<vector<int>> &input){if (input.size() < 1)return 0;vector<int> sum(input[0].size(), 0);sum = input[0];int col = input[0].size();int row = input.size();for (int i = 1; i < row; i++){for (int j = 0; j < col; j++){if (input[i][j] == 0)sum[j] = 0;elsesum[j] += input[i][j];}}int maxsum = MaxRectangle(sum);return maxsum;}int main(){vector<int> vec1 = { 0, 1, 0, 1, 1 };vector<int> vec2 = { 0, 1, 0, 1, 1 };vector<int> vec3 = { 0, 1, 1, 1, 1 };vector<int> vec4 = { 0, 0, 1, 1, 1 };vector<vector<int>> input = { vec1, vec2, vec3, vec4 };int maxsum=MaxRectangle(input);cout << "MaxRectangle:" << maxsum << endl;}
阅读全文
0 0
- 13.4—动态规划—Maximal Rectangle
- LeetCode 85. Maximal Rectangle&221. Maximal Square--动态规划
- 算法学习之动态规划(leetcode 85. Maximal Rectangle)
- leetcode——Maximal Rectangle
- LeetCode85——Maximal Rectangle
- leetcode——Maximal Rectangle
- 13.4 Maximal Rectangle
- leetcode 081 —— Maximal Rectangle
- 【动态规划】leetcode - Maximal Square
- 【leetcode】Array——Maximal Rectangle(85)
- 算法课作业系列12——Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- Maximal Rectangle
- 在URL中传递中文参数时传递失败的解决方案
- 13.2—动态规划—Maximum Subarray
- 【博弈】 各种博弈的搬运整理
- 大学如何组织大型信息系统研发的外协?
- Trie树解决字典中查找单词问题
- 13.4—动态规划—Maximal Rectangle
- 正则表达详解及例题
- 13.5—动态规划—Best Time to Buy and Sell Sto III
- MySQL数据类型char与varchar中数字代表的究竟是字节数还是字符数?
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
- D
- android 6.0权限全面详细分析和解决方案
- 13.6—动态规划—Interleaving String
- 《蘑菇街编程题》回文串