c语言直方图最大矩形面积,直方图内最大矩形

23

/**

* 类似快排的算法。计算当前子数组的最大矩阵面积,是子数组的长度*子数组的最小值。

* 然后以最小值的下标为分割点,递归的计算左右子数组的最大矩阵面积。

* 时间复杂度O(nlgn),空间复杂度O(1)。 *

**/

public class MaxInnerRec {

public int countArea(int[] A, int n) {

// write code here

return countCore(A,0,n-1);

}

private int countCore(int[] A , int left , int right){

if(left>right)

return 0;

if(left==right)

return A[left];

int highIndex = findMin(A,left,right);

int max = (right-left+1)*A[highIndex];

max = Math.max(max, countCore(A,left,highIndex-1));

max = Math.max(max, countCore(A,highIndex+1,right));

return max;

}

private int findMin(int[] A , int left , int right){

int min = left;

for(int i=left+1;i<=right;i++)

if(A[i]

min = i;

return min;

}

}

编辑于 2016-08-06 11:59:13

回复(11)

43

int i,j,L1,L2;

int max=0;

for(i=0;i

{

L1=0;L2=0;

for(j=i;j

{

if(A[j]>=A[i])L1++;

else break;

}

for(j=i-1;j>=0;--j)

{

if(A[j]>=A[i])L2++;

else break;

}

area=(L1+L2)*A[i];

if(area>max)max=area;

}

//printf("max area:%d\n",max);

return max;

发表于 2015-09-26 21:38:37

回复(9)

21

/*

对于这道题目,我看到题目的标签是动态规划,对于我这种刚刚接触算法的渣渣来说,我并不是很懂动态规划,所以我就凭着感觉写代码,思路如下:

求直方图矩形面积的最大值就是求数组中相邻元素(单个元素,两两相邻,三三相邻。。。

)中最小值乘以他们的数量得到面积,再取最大值,通过分析可以发现实际第一个元素比较一次,第二个元素比较两次,第三个元素比较三次。。。以此类推就可,对于A[n]只需记录A[n-1]之前最大面积的即可,所以不需要设置额外的数组,只需设置两个int变量即可求出面积最大值。

原谅我语文差,说的不明不白的,下面上代码,希望能帮到大家。

*/

public class MaxInnerRec {

public int countArea(int[] A, int n) {

int maxArea=0;

int min;

for(int i=0;i

min=Integer.MAX_VALUE;

for(int j=i;j>=0;j--){

min=Math.min(min, A[j]);

maxArea=Math.max(maxArea,(i-j+1)*min);

}

}

return maxArea;

}

}

发表于 2016-09-03 23:04:42

回复(16)

12

//思路:对于【2,7,9,4,1】,分别求出2*4,7*2,9*1,4*3,1*5.

//就是求每个元素所能组成最大矩形的大小,拿4举例子,先向左查找大于4的元素,

//如果>4,width++,否则break,同理向右查找。

import java.util.*;

public class MaxInnerRec {

public int countArea(int[] A, int n) {

// write code here

int max = 0;

for(int i = 0; i < n; i++){

int width = 1;//初始为1,算自己

//向左查

for(int right = i - 1; right >= 0; right--){

if(A[right] >= A[i])

width++;

else

break;

}

//向右查

for(int left = i + 1; left < n; left++){

if(A[left] >= A[i])

width++;

else

break;

}

if(max < A[i] * width)

max = A[i] * width;

}

return max;

}

}

发表于 2017-08-07 21:09:54

回复(2)

9

python一种解法如下: # -*- coding:utf-8 -*-

class MaxInnerRec:

def countArea(self, A, n):

maxArea = 0

if A == sorted(A): # 如果列表是有序的,直接走下面的循环。

for i, v in enumerate(A):

maxArea = max(maxArea, v * (n - i))

for i in range(n):

count = 1

#向右、向左看,直到找到比它小的数,每找到一个,count都加1

for j in range(0, i):

index = i - 1 - j

if A[index] >= A[i]:

count += 1

else:

break

for j in range(i + 1, n):

if A[j] >= A[i]:

count += 1

else:

break

maxArea = max(maxArea, A[i] * count)

return maxArea

发表于 2017-09-12 15:03:55

回复(0)

9

思路:根据动态规划的思路,创建一个dp表,dp[i][j]的意思是i到j之间的最大矩形面积。

dp[i][j]的计算有三种可能:

1.取最小面积乘以总个数

2.取(dp[i][j-1]

3.取dp[i+1][j]

总结来说dp的值为:

dp[i][i+k]=Math.max(dp[i][i+k-1], dp[i+1][i + k]);

dp[i][i+k]=Math.max(min*(k+1),dp[i][i+k]);

import java.util.*;

public class MaxInnerRec {

/*

* 思路:根据动态规划的思路,创建一个dp表,dp[i][j]的意思是i到j之间的最大矩形面积。

dp[i][j]的计算有三种可能:

1.取最小面积乘以总个数

2.取(dp[i][j-1]

3.取dp[i+1][j]

总结来说dp的值为:

dp[i][i+k]=Math.max(dp[i][i+k-1], dp[i+1][i + k]);

dp[i][i+k]=Math.max(min*(k+1),dp[i][i+k]);

* */

public int countArea(int[] A, int n) {

// write code here

int [][] dp=new int[n][n];

for(int i=0;i

{

dp[i][i]=A[i];

}

for(int i=1;i<=n;i++)

{

for(int j=0;j+i

{

int min=A[j];

for(int k=j+1;k<=i+j;k++)

{

min=Math.min(min, A[k]);

}

dp[j][j+i]=Math.max(dp[j][j+i-1], dp[j+1][j+i]);

dp[j][j+i]=Math.max(min*(i+1), dp[j][j+i]);

}

}

return dp[0][n-1];

}

}

发表于 2016-09-22 08:36:20

回复(4)

9

class MaxInnerRec {

public:

int countArea(vector A, int n) {

// write code here

stack sta;

vector spread_left(n, 0);

// 计算向左最远扩展几格

for (int j = 0; j < n; j++) {

while (!sta.empty() && A[j] <= A[sta.top()]) {

sta.pop();

}

if (!sta.empty()) {

spread_left[j] = j - sta.top() - 1;

} else {

spread_left[j] = j;

}

sta.push(j);

}

// 计算向右最远扩展几格

reverse(A.begin(), A.end());

vector spread_right(n, 0);

stack empty_stack;

sta.swap(empty_stack);

for (int j = 0; j < n; j++) {

while (!sta.empty() && A[j] <= A[sta.top()]) {

sta.pop();

}

if (!sta.empty()) {

spread_right[n - 1 - j] = j - sta.top() - 1;

} else {

spread_right[n - 1 - j] = j;

}

sta.push(j);

}

reverse(A.begin(), A.end());

int max = 0;

for (int j = 0; j < n; j++) {

if ((spread_left[j] + spread_right[j] + 1) * A[j] > max) {

max = (spread_left[j] + spread_right[j] + 1) * A[j];

}

}

return max;

}

};

考察栈的使用,每个元素最多进栈一次,出栈一次。时间复杂度O(n).

发表于 2016-07-10 10:28:48

回复(1)

5

O(n^2)的暴力 对直方图的每个柱子求能够延伸的最大宽度,放到数组里,最后求出最大值 int arr[501];

for(int i=0;i

int cnt = 1, r = i+1, l = i-1;

while(r<=n-1 && A[i]<=A[r]) {cnt ++; r ++; }

while(l>=0 && A[i]<=A[l]) {cnt ++; l --; }

arr[i] = cnt;

}

int the_max = 0;

for(int i=0;i

if(arr[i] * A[i] > the_max) the_max = arr[i] * A[i];

return the_max;

发表于 2018-08-26 19:42:52

回复(0)

3

很简单的一种方法,运行效率很高,主要通过栈,比较巧妙 class MaxInnerRec {

public:

int countArea(vector A, int n) {

// write code here

stack s;

A.insert(A.begin(),0);

A.push_back(0);

s.push(0);

int temp,maxarea=0;

for(int i=1;i

while(A[temp=s.top()]>A[i]){

s.pop();

maxarea=max(maxarea,A[temp]*(i-s.top()-1));

}

s.push(i);

}

return maxarea;

}

};

发表于 2018-11-07 10:22:54

回复(2)

3

class MaxInnerRec {

public:

int countArea(vector A, int n) {

int max_area = 0;

for(int i=0;i

int h = A[i];  //长方体高

int j=i+1;

int len = 1; //长方体宽

while(j=A[i]){ //宽向右移动

j++;

len++;

}

int k=i-1;

while(k>=0 && A[k]>=A[i]){ //宽向左移动

k--;

len++;

}

if(max_area < h*len){

max_area = h*len;

}

}

return max_area;

// write code here

}

};

发表于 2015-10-08 12:00:19

回复(0)

2

class MaxInnerRec {

public:

int countArea(vector A, int n) {

//write code here

int maxArea = INT_MIN;

for(int i = 0; i < n; ++i){

int tempArea = A[i];

for(int j = i-1; j >= 0; --j){

if(A[j] >= A[i])

tempArea += A[i];

else

break;

}

for(int j = i+1; j < n; ++j){

if(A[j] >= A[i])

tempArea += A[i];

else

break;

}

if(tempArea > maxArea)

maxArea = tempArea;

}

return maxArea;

}

};

发表于 2017-05-02 21:06:39

回复(0)

2

typedef struct Rect

{

int high;

int width;

}Rect;

class MaxInnerRec

{

public:

int countArea(vector A,int n)

{

int max = 0;

Rect r;

A.push_back(0);

stack s;

for(int i=0;i

{

Rect tmp2,tmp1 = {0,0};

while(!s.empty() && s.top().high >= A[i])

{

tmp2 = s.top();

s.pop();

tmp1.width = tmp1.width + tmp2.width;

tmp1.high = tmp2.high;

int m = tmp1.width*tmp1.high;

if(m >max)

max = m;

}

r.high = A[i];

r.width = tmp1.width + 1;

s.push(r);

}

return max;

}

};

发表于 2016-04-10 16:16:01

回复(0)

3

//动态规划思想:状态转移就是n个连续的矩形,取他们直方图中高的最小值乘n。 public int countArea(int[] A, int n) {

int max=0;

int[] array=new int[A.length]; //从i开始,记录i~n中的最小值。

for (int i = 0; i < A.length; i++) {

for (int j = i; j < array.length; j++)

{

if (j==i)

{

array[j]=A[i];

max=max>(A[j])?max:(A[j]);

continue;

}

array[i]=Math.min(array[i],A[j]);

max=max>(array[i]*(j-i+1))?max:(array[i]*(j-i+1));

}

}

return max; }

编辑于 2016-09-01 20:54:02

回复(2)

1

import java.util.*;

public class MaxInnerRec {

public int countArea(int[] A, int n) {

// write code here

int[]left=new int[n];

int[]right=new int[n];

LinkedListstk=new LinkedList<>();

Arrays.fill(right,n);

for(int i=0;i

while(!stk.isEmpty()&&A[i]<=A[stk.peekLast()]){

right[stk.peekLast()]=i;

stk.pollLast();

}

left[i]=stk.isEmpty()?-1: stk.peekLast();

stk.addLast(i);

}

int res=0;

for(int i=0;i

res=Math.max(res,(right[i]-left[i]-1)*A[i]);

}

return res;

}

} 单调栈 找出每一元素的左边和右边的 最近 最小值

发表于 2021-05-14 00:10:41

回复(0)

1

import java.util.*;

public class MaxInnerRec {

public int countArea(int[] A, int n) {

int[][] map = new int[n][A.length];

for (int i = 0; i < n; i++) {

map[i][0] = A[0];

for (int j = 1; j < n; j++) {

if (i == 0) {

map[i][j] = Math.max(map[i][j - 1], A[j]);

} else {

int min = A[j];

for (int k = j; k >= j - i && k >= 0; k--) {

min = Math.min(min, A[k]);

}

if (i <= j) {

map[i][j] = Math.max(Math.max(map[i][j - 1], map[i - 1][j]), (1 + i) * min);

} else {

map[i][j] = Math.max(Math.max(map[i][j - 1], map[i - 1][j]), (1 + j) * min);

}

}

}

}

return map[n - 1][A.length - 1];

}

}

发表于 2018-09-30 09:23:54

回复(0)

1

写一份C++的单调栈解法:

对于每一个元素,都先加入到单调栈中,在加入过程中,按以下操作进行:

1.如果该元素大于栈顶元素,直接加入到栈中;

2.如果该元素小于或等于栈顶元素,则把栈顶元素pop出来,同时更新答案=(height*width),要注意对width的修正;

所有元素加入到栈中后,再把栈中元素一一pop出来,每次pop出来的时候都更新答案;

最终代码如下: class MaxInnerRec {

public:

int countArea(vector A, int n) {

int ans=0,sun=0;

pair temp;

stack > s;

for(int i=0;i

while(!s.empty() && s.top().first>=A[i]){

temp=s.top();s.pop();

ans=max(ans,temp.first*(temp.second+sun));

sun+=temp.second;

}

s.push(make_pair(A[i],1+sun));

sun=0;

}

while(!s.empty()){

temp=s.top();s.pop();

ans=max(ans,temp.first*temp.second);

if(!s.empty()) s.top().second+=temp.second;

}

return ans;

}

};

编辑于 2018-09-06 22:04:34

回复(0)

1

写一个java版本单调栈的做法,时间复杂度O(N)。

思路还是取,每个位置自己为最低处,向左和向右扩展。用单调栈可以把向左向右的值在O(N)范围内做到。 public class MaxInnerRec {

public int countArea(int[] A, int n) {

Stack s = new Stack<>();

int[] left = new int[n];//左边可到达的位数

int[] right = new int[n];//右边可到达的位数

int max=0;

for(int i=0;i

while (!s.empty()&&A[i]<=A[s.peek()]){//单调栈,比栈顶的值大压栈,小于等于出栈。

s.pop();

}

if (!s.empty()){//栈不为空时,左移位数为当前下标和栈中下一个的下标只差

left[i]=i-s.peek()-1;

}else {

left[i]=i;//为空,即下面没有值,向左可到底,就是下标

}

s.push(i);

}

int [] B=new int [n];//这里是将A翻转,用和之前类似的方法。

for (int i=0;i

B[i]=A[n-1-i];

}

while (!s.empty()){

s.pop();

}

for (int i=0;i

while (!s.empty()&&B[i]<=B[s.peek()]){

s.pop();

}

if (!s.empty()){

right[n-1-i]=i-s.peek()-1;//注意right的下标就行

}else {

right[n-1-i]=i;

}

s.push(i);

};

for (int i=0;i

max=Math.max((left[i]+right[i]+1)*A[i],max);

}

return max;

}

}

编辑于 2018-06-26 22:58:29

回复(0)

1

import java.util.*;

public class MaxInnerRec {

public int countArea(int[] A, int n) {

int max=0;

//循环,以每次A[i]为最低点,求面积,保存最大值

for(int i=0;i

//宽度初始化为1

int width=1;

//左边的宽度

for(int j=i-1;j>=0&&A[j]>=A[i];j--)

width++;

//右边的宽度

for(int j=i+1;j<=n-1&&A[j]>=A[i];j++)

width++;

max=Math.max(max,A[i]*width);

}

return max;

}

}

发表于 2018-03-27 18:29:14

回复(0)

1

class MaxInnerRec {

public:

int countArea(vector A, int n) {

// write code here

stack s;

int result=0;

A.push_back(0);

for(int i=0;i

{

if(s.empty()||A[i]>A[s.top()])

s.push(i++);

else

{

int tmp=s.top();

s.pop();

result=max(result,A[tmp]*(s.empty()?i:i-s.top()-1));

}

}

return result;

}

};

发表于 2018-01-21 13:24:18

回复(0)

1

class MaxInnerRec {

public:

int countArea(vector A, int n) { int Max = 0, l1, l2; for(int i=0;i=A[i]) l1++; else break; for(int j=i-1;j>=0;j--) if(A[j]>=A[i]) l2++; else break; int S = (l1+l2)*A[i]; Max = (Max>S)?Max:S; } return Max;

}

};

发表于 2017-10-26 11:04:11

回复(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值