Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class LargestRectangleinHistogram {
- static int findIndexofMin(int[] nums, int lInd, int rInd) {
- int minIndex = lInd;
- for (int i = lInd + 1; i < rInd; i++) {
- if (nums[i - 1] > nums[i]) {
- minIndex = i;
- }
- }
- return minIndex;
- }
- static int maxOfThree (int one, int two, int three) {
- if (one >= two) {
- if (two >= three) {
- return one;
- }else if (three >= one) return three;
- else return one;
- }else if (two >= three) return two;
- else return three;
- }
- static int maxArea(int[] nums, int index, int sindex) {
- if (sindex == index) {
- return nums[index];
- }
- int minIndex = findIndexofMin(nums, index, sindex);
- int one = 0;
- int two ;
- int three = 0;
- if (minIndex > index) one = maxArea(nums, index, minIndex - 1);
- two =nums[minIndex] * (sindex - index + 1);
- if (minIndex < sindex) three = maxArea(nums, minIndex + 1, sindex);
- return maxOfThree(one, two, three);
- // return Math.max(one,Math.max(two,three));
- }
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //int number = Integer.parseInt(br.readLine());
- int[] nums = {2, 1, 5, 6, 2, 3};
- System.out.println(maxArea(nums, 0, nums.length -1));
- }
- }
Add Comment
Please, Sign In to add comment