Created
November 9, 2020 18:14
-
-
Save modos/2d05a2161ce5f1de1999a08eb7c55b49 to your computer and use it in GitHub Desktop.
مساحت محصور
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Online Java Compiler. | |
Code, Compile, Run and Debug java program online. | |
Write your code in this editor and press "Run" button to execute it. | |
*******************************************************************************/ | |
import java.util.Stack; | |
import java.util.Scanner; | |
public class Main | |
{ | |
static int getMaxArea(int hist[], int n) | |
{ | |
Stack<Integer> s = new Stack<>(); | |
int max_area = 0; | |
int tp; | |
int area_with_top; | |
int i = 0; | |
while (i < n) | |
{ | |
if (s.empty() || hist[s.peek()] <= hist[i]) | |
s.push(i++); | |
else | |
{ | |
tp = s.peek(); | |
s.pop(); | |
area_with_top = hist[tp] * (s.empty() ? i : i - s.peek() - 1); | |
if (max_area < area_with_top) | |
max_area = area_with_top; | |
} | |
} | |
while (s.empty() == false) | |
{ | |
tp = s.peek(); | |
s.pop(); | |
area_with_top = hist[tp] * (s.empty() ? i : i - s.peek() - 1); | |
if (max_area < area_with_top) | |
max_area = area_with_top; | |
} | |
return max_area; | |
} | |
public static void main(String[] args) | |
{ | |
Scanner input = new Scanner(System.in); | |
int n = input.nextInt(); | |
int[] arr = new int[n]; | |
for(int i = 0; i < n; i++){ | |
arr[i] = input.nextInt(); | |
} | |
System.out.println(getMaxArea(arr, arr.length)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment