zoukankan      html  css  js  c++  java
  • 计算直方图中最大矩形面积

    题目是计算直方图中的最大矩形面积,下面是我的做法,我在网上也看到有人说可以通过栈的方式来解决,因为时间问题,并没有马上尝试,下回有时间在尝试下吧!!

    还有这题有变式:计算矩阵中最大的矩形面积,其中矩阵中元素只能为1和0,代码下次补发吧!!

    代码如下:

    #include<iostream>
    using namespace std;

    int maxSquare(const int pos,const int n,const int height[])
    {

    if(n==1)
    return height[0];

    int square=0;
    for(int i=pos;i<n;i++)
    {
    int count=1,temp_square=0;
    int j=i-1;
    while(j>=0)
    {
    if(height[j--]>=height[i])
    count++;
    else
    break;
    }
    j=i+1;
    while(j<n)
    {
    if(height[j++]>=height[i])
    count++;
    else
    break;
    }
    temp_square=count*height[i];

    if(temp_square>square)
    square=temp_square;
    }
    return square;
    }

    int main()
    {

    int square,n,temp;
    int height[100000];
    cin>>n;
    for(int i=0;i<n;i++)
    {
    cin>>temp;
    height[i]=temp;
    }
    square=maxSquare(0,n,height);
    cout<<square<<endl;
    return 0;
    }

  • 相关阅读:
    dubbo 学习
    JSTL 实现 为Select赋多个值
    Spring MVC 单元测试Demo
    IDEA git commit push revert
    高并发处理
    Redis Expire TTL命令
    Redis 原子操作INCR
    Redis 安装
    慢日志查询
    angularJs 处理多选框(checkbox)
  • 原文地址:https://www.cnblogs.com/romantic-Right/p/4873150.html
Copyright © 2011-2022 走看看