在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢?

还是先把题目贴上来吧

题目写的很直观,就是找直方图的最大矩形面积,不知道是受之前的trie tree影响怎么的,感觉树这玩意还真有用,于是就思考呀,还真别说,真想出一种方式,好吧,其实是入了一个大坑,也无妨,记录下来,好歹也是思路历程.....

大概思路这样的:

每次寻找直方图的最小值,记录此时以该最小值,和以其为高度的矩形面积,再将直方图以该最小值为界限,将直方图分成若干份,按照同样思路对每个子直方图继续求解,这样如果把每个直方图作为节点的话,其实也形成了一棵树,不过这树没啥价值,因为本题并不关心得到最大矩形的路径,只要求面积即可,是时候献丑了,代码贴上:

#include <vector>
#include <list>
#include <iostream>
#include <stack>
using namespace std;
class LRTreeNode
{
private:
int getMin()
{
if (right-left == 0)
{
return 0;
}
int min = (*heights)[left];
for (int i=left;i<right;i++)
{
min = (*heights)[i] > min ? min : (*heights)[i];
}
return min;
}
void getMaxArea()
{
maxArea = bottom*(right-left);
}
public:
int left, right;
int bottom;
int min;
int maxArea;
static vector<int>* heights;
vector<LRTreeNode*> lrnv;
LRTreeNode(int bottom,int left,int right)
{
this->left = left;
this->right = right;
this->bottom=getMin()+bottom;
this->min = getMin();
getMaxArea();
}
vector<LRTreeNode*>* genChildren()
{
int left2=left, right2=left;
for (int i = left; i < right; i++)
{
(*heights)[i] -= min; if ((*heights)[i] == 0 )
{
if (right2-left2 != 0)
{
lrnv.push_back(new LRTreeNode(bottom,left2,right2));
}
left2 = i+1;
right2 = i+1;
}
else
{
right2++;
}
}
if (right2 - left2 != 0)
{
lrnv.push_back(new LRTreeNode(bottom, left2, right2));
}
return &lrnv;
}
};
vector<int>* LRTreeNode::heights = NULL;
class LRTree
{
private:
LRTreeNode root;
public:
LRTree(vector<int>& heights) :root(0,0,heights.size())
{ }
int getMaxArea()
{
int max = root.maxArea;
list<LRTreeNode*> st;
vector<LRTreeNode*>* t = root.genChildren();
LRTreeNode* stt;
for (int i = 0; i < t->size(); i++)
{
st.push_back((*t)[i]);
max = max >(*t)[i]->maxArea ? max : (*t)[i]->maxArea;
}
while (st.empty() == false)
{
stt = st.back();
t = stt->genChildren();
st.pop_back();
for (int i = 0; i < t->size(); i++)
{
st.push_back((*t)[i]);
max = max > (*t)[i]->maxArea ? max : (*t)[i]->maxArea;
}
delete stt;
}
return max;
}
}; class Solution {
public:
int largestRectangleArea(vector<int>& heights);
};
int Solution::largestRectangleArea(vector<int>& heights)
{
LRTreeNode::heights = &heights;
LRTree t(heights);
return t.getMaxArea();
} int main()
{
vector<int> t = {1,2,3,4,5};
Solution s;
cout << s.largestRectangleArea(t);
return 0;
}

代码里面有几个值得注意的问题:

1.按照之前所说的思路,每个节点都得存储一个子直方图,这样并非最好方法,试想如果直方图为n,依次增加,则空间复杂度为O(n^2),故采用了所有节点共用一个直方图,每个节点存储左右界限即可,也就是LRTreeNode的left,right;

2.在每次的子直方图中都减去了底部部分,所以最终的直方图数据会被变化。

该种方法虽然采用了分治的思想,但其本质其实是遍历了所有的可能的矩形,其实效果并不好,由于最小值的多次寻找增加了复杂度,但作为思路历程,还是一并记录。

关于LeetCode的Largest Rectangle in Histogram的低级解法的更多相关文章

  1. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

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

  2. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  3. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  4. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

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

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

  7. LeetCode之Largest Rectangle in Histogram浅析

    首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each ...

  8. [LeetCode OJ] Largest Rectangle in Histogram

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

  9. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

随机推荐

  1. TOGAF架构能力框架之架构能力建设和架构治理

    TOGAF架构能力框架之架构能力建设和架构治理 为了确保架构功能在企业中能够被成功地运用,企业需要通过建立适当的组织结构.流程.角色.责任和技能来实现其自身的企业架构能力,而这也正是TOGAF的架构能 ...

  2. [转]浅谈PCA的适用范围

    线性代数主要讲矩阵,矩阵就是线性变换,也就是把直线变成直线的几何变换,包括过原点的旋转.镜射.伸缩.推移及其组合.特征向量是对一个线性变换很特殊的向量:只有他们在此变换下可保持方向不变,而对应的特征值 ...

  3. C# 根据时间创建文件夹

    string file = ((fileNameIndex)index).ToString(); if (!Directory.Exists(HttpContext.Current.Server.Ma ...

  4. iOS 监听声音按键

    有时在项目中需要监听用户是否按下了物理声音键,然后来做某些操作,如:你自定义了一个照相功能,希望用户按下声音按键时也能进行拍照,苹果自带的照相机就有这种功能. 监听物理声音键是否按下的方法有很多中,我 ...

  5. springMVC3学习(一)--框架搭建

    由于项目需要,学习下springMVC,在此简单记录一下. 如有十万个为什么,暂且忽略,待以后研究. 本人是基于3.1.1版本开发,如遇jar包版本冲突等其他问题,概不负责. 下载地址:上传此zip资 ...

  6. linux学习之linux的hostname修改详解《转》

    linux的hostname是一个kernel变量,可以通过hostname命令来查看本机的hostname.也可以直接cat /proc/sys/kernel/hostname查看. #hostna ...

  7. go: GOPATH entry is relative; must be absolute path: "".

    安装:vscode-go出现以下提示: go: GOPATH entry is relative; must be absolute path: "".Run 'go help g ...

  8. 从c#基础到java基础的学习的感悟

    从进入培训公司到现在已经有三周多了,我想我和绝大多数人一样,能考虑进入培训学校,肯定是心理做好了准备的,那就是只有一个目的学好这门技术,从之前的开班典礼来看,从每个同学的自我介绍,我们这的大部分人来这 ...

  9. zabbix 布署实践【7 H3C网络设备监控模版制作思路】

    我们知道,zabbix安装后自带Template OS Linux 模版已满足了绝大部分Linux服务器的基础环境监控,只是我们在其模版上稍微修改,可配合将SWAP监控取消,另存为一个叫OS Linu ...

  10. unity3dWeb版文本转语音

    文本转语音 <script type="text/javascript"> function VioceSpeack(str) { var zhText = str; ...