Given a histogram {1, 3, 4, 6, 2, 1, 3}, calculate the largest rectangle area.
idea: for every index(post), we are certain that this rectangle starts from index, but when does it end? if the index is something that is smaller than previous, then the rectangle ends. e.g. the given sample, when index is 5, (value is 2), rectangle starts from 1(value 3), 2(value 4), 3(value 6) ends. We can use a stack to push larger values into it and pop them out if less value is encountered until what left in the stack is smaller than the current one.
public class Histogram {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] histogram = {1, 3, 4, 6, 2, 1, 3};
System.out.println("largest area: " + getLargestArea(histogram));
}
private static Info getLargestArea(int[] histogram) {
// TODO Auto-generated method stub
Stack<Info> stack = new Stack<Info>();
List<Info> list = new ArrayList<Info>();
for (int i = 0; i < histogram.length; i++) {
Info inf = new Info(i, histogram[i]);
//we can decide left value for a post
inf.left = i;
if (stack.isEmpty() || stack.peek().value <= histogram[i]) {
stack.push(inf);
} else {
// System.out.printf("i = %d\n", i);
//stack.peek().value > histogram[i], we can
while (!stack.isEmpty() && stack.peek().value > histogram[i]) {
Info isure = stack.pop();
isure.right = i;
// System.out.println("pop: " + isure);
list.add(isure);
}
stack.push(inf);
}
}
while (!stack.isEmpty()) {
Info isure = stack.pop();
isure.right = histogram.length-1;
list.add(isure);
}
Collections.sort(list);
return list.get(0);
}
private static class Info implements Comparable {
int left = -1;
int right = -1;
int area = -1;
int index; //histogram index
int value; //histogra value
Info(int index, int value) {
this.index = index;
this.value = value;
}
public int getArea() {
if (this.area == -1) {
this.area = this.value * (this.right - this.left);
}
return this.area;
}
public int compareTo(Object o) {
// TODO Auto-generated method stub
if (o == this) return 0;
if (o instanceof Info == false) return -1;
Info inf = (Info) o;
if (getArea() < inf.getArea()) {
return 1;
} else if (getArea() > inf.getArea()) {
return -1;
} else return 0;
}
public String toString() {
return "area: " + getArea() + ", value " + value + ", from " + left + " to " + right;
}
}
}
分享到:
相关推荐
在LeetCode这个著名的在线编程挑战平台上,有一道题目名为“Largest Rectangle in Histogram”(直译为“柱状图中的最大矩形”),这是一道与数据结构和算法密切相关的题目,旨在考察程序员对栈、动态规划以及几何...
sas The UNIVARIATE Procedure HISTOGRAM Statement.
Compute and plot (show the image and its histogram) the histogram of flower.pgm, swan.pgm and tools.pgm. Comment on what information can be discerned about the images from an examination of the ...
the code is basically handling the adjustment of the histogram of an image. the code can be used to enhance the image by adjusting the histogram of an image.
This algorithm presents the first part of our proposed technique named as “Histogram processed Face Recognition” [2] For training, grayscale images with 256 gray levels are used. Firstly, ...
.write a program in MATLAB to plot a histogram of image and compare your results with the output of histogram.
This algorithm presents the first part of our proposed technique named as “Histogram processed Face Recognition” [2] For training, grayscale images with 256 gray levels are used. Firstly, ...
根据提供的文件信息,我们可以总结出以下关于目标跟踪的知识点: 1. 片段化目标表示:文档中提到的目标跟踪算法使用了多个图像片段或块(patches)来表示目标,这些片段是任意选择的,不同于传统基于模型的方法,后...
histogram测试数据
《使用Histogram1D实现图像直方图与对比度调整》 在数字图像处理领域,直方图和对比度调整是两个至关重要的概念。本程序"Histogram1D.rar"旨在提供一个与ImageJ软件类似的功能,它能够显示图像的直方图,并对图像的...
calculate and plot histogram in opencv, implemented by c
《前端开源库-histogram:构建柱状图的艺术》 在前端开发中,数据可视化是至关重要的,它能够帮助用户快速理解和解析复杂的数据信息。而柱状图作为一种常见且直观的图表类型,被广泛应用于各种场景,如数据分析、...
python python_leetcode题解之084_Largest_Rectangle_in_Histogram
本项目“ADC Histogram_labview_”显然关注的是利用LabVIEW来处理ADC(模拟数字转换器)数据,并创建直方图进行数据分析。ADC是将连续的模拟信号转换为离散的数字信号的关键组件,常见于各种电子设备中,如数据采集...
在给定的“Gray Histogram”项目中,显然我们聚焦于视频帧的灰度值统计,利用了OpenCV库的2.2版本。OpenCV是一个广泛使用的计算机视觉库,提供了丰富的图像和视频处理功能。 首先,让我们来了解一下灰度图像。在...
javascript js_leetcode题解之84-largest-rectangle-in-histogram.js
Histogram Equalization and Local Histogram Equalization of Images
c c语言_leetcode题解之0084_largest_rectangle_in_histogram.zip
《MetaTrader 5中的Stochastic Histogram HTF:深入解析与应用》 MetaTrader 5(MT5)是一款广泛应用于外汇、期货和股票交易市场的交易平台,它提供了丰富的技术分析工具和自定义指标,使得交易者能够根据自己的...
area = heights[ 0 ]; for ( int i = 0 ; i < heights . length; i ++ ) { int minHeight = heights[i]; area = Math . max(area, heights[i]); for ( int j = i + 1 ; j < heights . length; j ++ ) { ...