相关题型

问题一(最大和子矩阵) : 有一个 m x n 的矩阵,矩阵的元素可正可负。请找出该矩阵的一个子矩阵(方块),使得其所有元素之和在所有子矩阵中最大。(问题来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=1050)

问题二( 最大 0/1 方块) :有一个 m x n 的矩阵,元素为 0 或 1。一个子矩阵,如果它所有的元素都是 0, 或者都是 1,则称其为一个 0-聚类 或 1-聚类,统称聚类(Cluster)。请找出最大的聚类(元素最多的聚类)(面试题)

这两个问题,除了都是在矩阵上操作之外,似乎没有什么共同之处。其实不然。事实上,它们可以用同一个思路解决。该思路来源于下面的一个问题,具体地说,就是把前两个问题化归成多个问题三:

问题三(和最大的段) :有 n 个有正有负的数排成一行,求某个连续的段,使得其元素之和最大。(问题来源:某面试题。事实上,这也是一道经典题目,具体参考 http://en.wikipedia.org/wiki/Maximum_subarray_problem)

问题四(最大长方形) : 有一个有 n 个项的统计直方图,假定所有的直方条 (bar) 的宽度一样。在所有边与 x 轴 和 y 轴平行的长方形中,求该被该直方图包含的面积最大的长方形。(问题来源:面试经典题目)

参考

分析:动态规划,从左上角开始,如果当前位置为1,那么到当前位置包含的最大正方形边长为左/左上/上的值中的最小值加一,因为边长是由短板控制的。
最大子矩阵和问题可以类比于最大字段和问题,从一维变成二维,dp思路,在输入的时候做一个处理,让a[i,j]变为存放前i行j列的和,降低复杂度。状态转移方程为sum[k+1]=sum[k]<0?0:sum[k]+a[i,j];表示第k行i到j的和。因为a[i,j]为存放前i行j列的和,所以a[i,j]=a[k,j]-a[k,i-1];
// Program to find maximum sum subarray in a given 2D array
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define ROW 4
#define COL 5 // Implementation of Kadane's algorithm for 1D array. The function
// returns the maximum sum and stores starting and ending indexes of the
// maximum sum subarray at addresses pointed by start and finish pointers
// respectively.
int kadane(int* arr, int* start, int* finish, int n)
{
// initialize sum, maxSum and
int sum = 0, maxSum = INT_MIN, i; // Just some initial value to check for all negative values case
*finish = -1; // local variable
int local_start = 0; for (i = 0; i < n; ++i)
{
sum += arr[i];
if (sum < 0)
{
sum = 0;
local_start = i+1;
}
else if (sum > maxSum)
{
maxSum = sum;
*start = local_start;
*finish = i;
}
} // There is at-least one non-negative number
if (*finish != -1)
return maxSum; // Special Case: When all numbers in arr[] are negative
maxSum = arr[0];
*start = *finish = 0; // Find the maximum element in array
for (i = 1; i < n; i++)
{
if (arr[i] > maxSum)
{
maxSum = arr[i];
*start = *finish = i;
}
}
return maxSum;
} // The main function that finds maximum sum rectangle in M[][]
void findMaxSum(int M[][COL])
{
// Variables to store the final output
int maxSum = INT_MIN, finalLeft, finalRight, finalTop, finalBottom; int left, right, i;
int temp[ROW], sum, start, finish; // Set the left column
for (left = 0; left < COL; ++left)
{
// Initialize all elements of temp as 0
memset(temp, 0, sizeof(temp)); // Set the right column for the left column set by outer loop
for (right = left; right < COL; ++right)
{
// Calculate sum between current left and right for every row 'i'
for (i = 0; i < ROW; ++i)
temp[i] += M[i][right]; // Find the maximum sum subarray in temp[]. The kadane()
// function also sets values of start and finish. So 'sum' is
// sum of rectangle between (start, left) and (finish, right)
// which is the maximum sum with boundary columns strictly as
// left and right.
sum = kadane(temp, &start, &finish, ROW); // Compare sum with maximum sum so far. If sum is more, then
// update maxSum and other output values
if (sum > maxSum)
{
maxSum = sum;
finalLeft = left;
finalRight = right;
finalTop = start;
finalBottom = finish;
}
}
} // Print final values
printf("(Top, Left) (%d, %d)\n", finalTop, finalLeft);
printf("(Bottom, Right) (%d, %d)\n", finalBottom, finalRight);
printf("Max sum is: %d\n", maxSum);
} // Driver program to test above functions
int main()
{
int M[ROW][COL] = {{1, 2, -1, -4, -20},
{-8, -3, 4, 2, 1},
{3, 8, 10, 1, 3},
{-4, -1, 1, 7, -6}
}; findMaxSum(M); return 0;
}

Maximum Submatrix & Largest Rectangle的更多相关文章

  1. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  2. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  3. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  4. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  6. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  7. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  8. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. 漏洞利用查询工具sandi

    漏洞利用查询工具sandi   在渗透测试中,一旦发现漏洞,就需要查找该漏洞的利用方式.由于漏洞众多,就需要渗透测试人员从海量的漏洞信息找出可用攻击载荷.Kali Linux内置了一个查询工具sand ...

  2. [CF580E]Kefa and Watch

    题目大意: 维护一个由'0'~'9'构成的字符串,支持以下两种操作: 1.将指定区间内的所有字符修改为同一指定字符. 2.询问$x$是否为指定区间内的循环节. 思路: 建立一棵线段树,维护每个子串的哈 ...

  3. 【BZOJ-4261】建设游乐场 最大费用最大流

    4261: 建设游乐场 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 21  Solved: 8[Submit][Status][Discuss] D ...

  4. RabbitMQ 延时消息队列

    消息延时在日常随处可见: 1.订单创建10min之后不发起支付,自动取消. 2.30min定时推送一次邮件信息. 最常用到方式后台定时任务轮训,量小的时候可以使用,量大会出现数据读取会性能问题.Rab ...

  5. j.u.c系列(03)---之AQS:AQS简介

    写在前面 Java的内置锁一直都是备受争议的,在JDK 1.6之前,synchronized这个重量级锁其性能一直都是较为低下,虽然在1.6后,进行大量的锁优化策略,但是与Lock相比synchron ...

  6. 基于Servlet+JSP+JavaBean开发模式的用户登录注册

    http://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...

  7. stm32f103串口实现映射功能

    在实际开发中,常常遇到串口的默认输出IO口被其它模块占用了,所以我们要用到串口IO口映射功能.是指将原来实现功能的IO口映射到其它指定IO口,其它不变.详细操作例如以下: 先贴出默认下的串口初始化设置 ...

  8. TVS二极管和稳压二极管的区别

    TVS二极管和稳压二极管的区别 TVS管超过它的耐压值后,会瞬间导通短路,反应速度在ns级, 而稳压管是稳压作用的,超过它的稳压值,只要功率不超过它的耐受值,就会稳定在它的稳压值范围内. TVS是瞬态 ...

  9. mexHttpBinding协议 【发布元数据终结点】

    我们需要知道很多东西才能使用微软通信基础架构(WCF)来开发应用程序.尽管这本书已经试着囊括普通开发人员需要了解的WCF所有内容,也还是有一些内容没有讨论到.附录的主要目的是填充这些罅隙. 发布元数据 ...

  10. C程序设计的抽象思维-递归过程-砝码称重

    [问题] 在狄更斯时代,商人们用砝码和天平来称量商品的重量,假设你仅仅有几个砝码,就仅仅能精确地称出一定的重量.比如,假定仅仅有两个砝码:各自是1kg和3kg. 仅仅用1kg的砝码能够称出1kg重量的 ...