Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Largest Rectangle in Histogram leetcode

/*
	This is an implementation that demonstrates
	how to efficiently find the area of largest 
	rectangle that can be formed from a number
	of adjacent buildings, whose heights are given
	in an array of integers.

	Let n be the number of buildings.

	Time complexity: O(n) 
	Space complexity: O(n)
*/
import java.util.Stack;
import java.util.ArrayList;
import java.util.Arrays;

public class LargestRectangleArea {
	public static void main(String[] args) {
		Integer[] heights = { 1, 3, 3, 2, 4, 1, 5, 3, 2 };
		ArrayList<Integer> buildings = new ArrayList<Integer>(Arrays.asList(heights));
		System.out.println(largestRectangleUnderSkyline(buildings)); // 9
	}

	private static int largestRectangleUnderSkyline(ArrayList<Integer> buildings) {
		Stack<Integer> stack = new Stack<>();
		int[] maxArea = new int[1];
		int currentBuildingIdx;
		for (currentBuildingIdx = 0; currentBuildingIdx < buildings.size();) {
			if (stack.isEmpty() || buildings.get(stack.peek()) <= buildings.get(currentBuildingIdx)) {
				stack.push(currentBuildingIdx++);
			} else {
				updateMaxArea(stack, buildings, maxArea, currentBuildingIdx);
			}
		}

		while (!stack.isEmpty()) {
			updateMaxArea(stack, buildings, maxArea, currentBuildingIdx);
		}

		return maxArea[0];
	}

	private static void updateMaxArea(Stack<Integer> stack, ArrayList<Integer> buildings, int[] maxArea,
			int currentBuildingIdx) {
		int currentArea;
		int topBuildingIdx = stack.pop();
		if (stack.isEmpty()) {
			currentArea = buildings.get(topBuildingIdx) * currentBuildingIdx;
		} else {
			currentArea = buildings.get(topBuildingIdx) * (currentBuildingIdx - 1 - stack.peek());
		}
		if (currentArea > maxArea[0]) {
			maxArea[0] = currentArea;
		}
	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: spring boot access images in resources folder 
Java :: java object to int 
Java :: Caused by: java.lang.ClassNotFoundException 
Java :: enum java 
Java :: java sort a Map by keys reverse descending order 
Java :: deserialize date java 
Java :: java indexof all occurrences 
Java :: reverseString 
Java :: for cicle java 
Java :: java datatypes 
Java :: for and while loops java 
Java :: kubectl config kubernetes dashboard 
Java :: pre increment and post increments java 
Java :: java first character string number 
Java :: how junit test getter and setter 
Java :: hashtable java 
Java :: richest customer wealth 
Java :: asscending linkedlist remove duplicates valuesjava 
Java :: Program to remove duplicates in an ArrayList 
Java :: set text from strings.xml 
Java :: copying primitive array to arraylist in java 
Java :: java round up 
Java :: zweidimensionales array ausgeben java 
Java :: node in java 
Java :: java list last element 
Java :: print symbol in pyramid shape in java 
Java :: java use variables inside strings 
Java :: converting string to int in java 
Java :: loop array using stream java 
Java :: java implement interface 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =