在HankerRank遇到一题计算柱状图连续矩形面积的问题.

举例 hist = [3, 2, 3]. 在这个柱状图里面最大可以容纳一个high = 2 length = 3的连续矩形, 其面积 = 2*3 = 6.



按照提示需要使用stack来是时间复杂度保持在O(n). 搜索提示和代码, 把自己的理解描述如下:

  1. 加入数组是升序的 hist = [1, 2, 3]



    因为数组是升序的, 所以每一个都会和之后的 high 形成更大的连续面积.

    也因为数组是升序的, 所以每一个都会和之前的 high 无法形成更大的连续面积. 3和2无法形成, 因为2和3 已经组合.

    所以升序的计算最好是在数组的最后面, i = 3时

    i = 3 计算 index = 2的面积. area = 3 * 1; h[index] * (i - (index - 1) - 1)

    i = 3 计算 index = 1的面积. area = 2 * 2; h[index] * (i - (index - 1) - 1)

    i = 3 计算 index = 0的面积. area = 1 * 3; h[index] * i

  2. 加入数组是降序的 hist = [3, 2, 1]



    和上面分析相反, 但是在i+1均可计算前一个连续矩形面积.

    i = 0 不计算

    i = 1 计算 index = 0 的面积. area = 3 * 1; h[index] * i

    i = 2 计算 index = 1 的面积. area = 2 * 2; h[index] * i

    i = 3 计算 index = 2 的面积. area = 1 * 3; h[index] * i


public static long GetLargestArea(int[] h)
{
long maxarea = -1;
long toparea = -1;
int i = 0;
int top;
var stack = new Stack<int>(); while(i < h.Length)
{
if(stack.Count == 0 || h[i] >= h[stack.Peek()])
{
// 把升序的数组索引入栈.
stack.Push(i++);
}
else
{
// 把降序的数组索引出栈.并计算其面积.
top = stack.Pop(); toparea = h[top] * (stack.Count == 0 ? i : (i - stack.Peek() - 1));
if(toparea > maxarea)
maxarea = toparea;
}
} while(stack.Count != 0)
{
top = stack.Pop();
toparea = h[top] * (stack.Count == 0 ? i : (i - stack.Peek() - 1)); if(toparea > maxarea)
maxarea = toparea;
} return maxarea;
}

参考:

Largest Rectangular Area in a Histogram

Hackerrank

Largest Rectangular Area in a Histogram 最大连续面积的更多相关文章

  1. Largest Rectangular Area in a Histogram

    题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...

  2. 【Leetcode_easy】812. Largest Triangle Area

    problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...

  3. Largest Allowed Area【模拟+二分】

    Largest Allowed Area 题目链接(点击) 题目描述 A company is looking for land to build its headquarters. It has a ...

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

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

  5. 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

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

  6. [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  7. [LeetCode] Largest Triangle Area 最大的三角区域

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  8. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  9. 【LeetCode】812. Largest Triangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 三重循环 组合函数 日期 题目地址:https:// ...

随机推荐

  1. Node.js、npm、vue-cli 的安装配置环境变量

    我安装node.js是为了学习vue,需要用到npm,所以就把node.js安装了,安装node.js会带有npm的安装. 在安装node.js之前,我们需要了解以下三个内容. npm: Nodejs ...

  2. webpack4.27.1中遇到的错误

    1:ERROR in Entry module not found: Error: Can't resolve './src' 我在使用webpack命令时报错,这时因为我的配置文件有问题webpac ...

  3. Spring AOP 切点(pointcut)表达式

    这遍文章将介绍Spring AOP切点表达式(下称表达式)语言,首先介绍两个面向切面编程中使用到的术语. 连接点(Joint Point):广义上来讲,方法.异常处理块.字段这些程序调用过程中可以抽像 ...

  4. python 图像处理,画一个正弦函数

    import numpy as np from PIL import Image import matplotlib.pyplot as plt import math size = 300 new_ ...

  5. IR2104s半桥驱动使用经验

    多次使用IR2104s,每次的调试都有种让人吐血的冲动.现在将使用过程遇到的错误给大家分享一下,方便大家找到思路. 一.自举电容部分(关键) 1.听说自举电路必须要安装场效应管,于是我在使用过程中,安 ...

  6. vue axios中文文档详解

    英文文档:https://github.com/axios/axios 参考:https://www.jb51.net/article/123485.htm

  7. Signalr实时通讯

    我们直接来干货~~~~~~觉得好推荐一下哈  研究不易 参考--https://www.jb51.net/article/133202.htm  这是基本教程 下面是重点: 如果你想允许跨域 具体代码 ...

  8. Kafka笔记4(消费者)

    消费者和消费群组: Kafka消费者从属于消费者群组,一个群组里的消费者订阅的是同一个主题,每个消费者接收主题的一部分分区消息 消费者的数量不要超过主题分区的数量,多余的消费者只会被闲置 一个主题可以 ...

  9. Centos7 关于防火墙的一些简单配置

    近期安装了linux系统Centos7,接触下来发现了与原来的Centos6.5有一些差别,这里主要记录下来我的一些关于Centos7防火墙的了解. 一.firewall简介 CentOS 7中防火墙 ...

  10. Redishelp

    /** * @author yanming.zhang * @date 2019/1/25 21:15 */ @Component public class RedisHelp { @Autowire ...