vladimirVenkov

Histogram

Jun 10th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class LargestRectangleinHistogram {
  6.  
  7.     static int findIndexofMin(int[] nums, int lInd, int rInd) {
  8.         int minIndex = lInd;
  9.         for (int i = lInd + 1; i < rInd; i++) {
  10.             if (nums[i - 1] > nums[i]) {
  11.                 minIndex = i;
  12.             }
  13.         }
  14.         return minIndex;
  15.     }
  16.     static int maxOfThree (int one, int two, int three) {
  17.         if (one >= two) {
  18.             if (two >= three) {
  19.                 return one;
  20.             }else if (three >= one) return three;
  21.             else return one;
  22.         }else if (two >= three) return two;
  23.         else return three;
  24.     }
  25.  
  26.     static int maxArea(int[] nums, int index, int sindex) {
  27.         if (sindex == index) {
  28.             return nums[index];
  29.         }
  30.         int minIndex = findIndexofMin(nums, index, sindex);
  31.         int one = 0;
  32.         int two ;
  33.         int three = 0;
  34.         if (minIndex > index)  one = maxArea(nums, index, minIndex - 1);
  35.  
  36.         two =nums[minIndex] * (sindex - index + 1);
  37.  
  38.         if (minIndex < sindex) three = maxArea(nums, minIndex + 1, sindex);
  39.  
  40.         return maxOfThree(one, two, three);
  41. //        return Math.max(one,Math.max(two,three));
  42.  
  43.     }
  44.  
  45.     public static void main(String[] args) throws IOException {
  46.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  47.         //int number = Integer.parseInt(br.readLine());
  48.         int[] nums = {2, 1, 5, 6, 2, 3};
  49.         System.out.println(maxArea(nums, 0, nums.length -1));
  50.     }
  51. }
Add Comment
Please, Sign In to add comment