We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here's my Java 8 solution, passing all test cases, for Max Score of 50.
While solving this problem, I could think of three approaches: two using Explicit Stacks, while one using Implicit Stack (via Recursion). All three of them pass ALL Test-Cases, for a Max Score of 50. All of them have been provided in the code, with some explanatory comments.
classResult{// SIMPLEST & BEST APPROACH// Largest Rectangle using Linear Iteration, with Stack// Passes ALL HackerRank Test-Cases. For Max Score of 50// Time-Complexity : WC: O(n) | AC: O(n)// Space-Complexity: WC: O(n) | AC: O(log n) | i.e. Explicit Stack DepthpublicstaticlongsolveSimpleIterativeWithStack(List<Integer>h){intn=h.size();Stack<Integer>stack=newStack<>();longmaxArea=0;for(inti=0;i<=n;i++){longcurHeight=((i==n)?0:h.get(i));while(!stack.isEmpty()&&curHeight<h.get(stack.peek())){intheight=h.get(stack.pop());intwidth=(stack.isEmpty()?i:(i-stack.peek()-1));maxArea=Math.max(maxArea,(long)height*width);}stack.push(i);}returnmaxArea;}// Largest Rectangle using Divide & Conquer (Recursive) Approach// No Stacks used in this Solution.// Passes ALL HackerRank Test-Cases. For Max Score of 50// Time-Complexity : WC: O(n^2) | AC: O(n * log n)// Space-Complexity: WC: O(n) | AC: O(log n) | i.e. Recursion DepthprivatestaticlongsolveDivNCnqRecursive(List<Integer>h,intstart,intend){if(start>end){return0;}// Find index of minimum height in current rangeintminIndex=start;for(inti=(start+1);i<=end;i++){if(h.get(i)<h.get(minIndex)){minIndex=i;}}// Area using the minimum height across entire rangelongareaWithMin=(long)(h.get(minIndex)*(end-start+1));// Recurse on left and right sublistslongleftArea=solveDivNCnqRecursive(h,start,minIndex-1);longrightArea=solveDivNCnqRecursive(h,minIndex+1,end);returnMath.max(areaWithMin,Math.max(leftArea,rightArea));}// Largest Rectangle using Divide & Conquer (Non-Recursive / Iterative) Approach// Stack is used to simulate Recursion// Passes ALL HackerRank Test-Cases. For Max Score of 50// Time-Complexity : WC: O(n^2) | AC: O(n * log n)// Space-Complexity: WC: O(n) | AC: O(log n) | i.e. Explicit Stack DepthpublicstaticlongsolveDivNCnqIterative(List<Integer>h){longmaxArea=0;Stack<int[]>stack=newStack<>();// each element: [start, end]stack.push(newint[]{0,h.size()-1});while(!stack.isEmpty()){int[]range=stack.pop();intstart=range[0],end=range[1];if(start>end)continue;// Find min index in current rangeintminIndex=start;for(inti=start+1;i<=end;i++){if(h.get(i)<h.get(minIndex)){minIndex=i;}}// Compute area with this min heightlongareaWithMin=(long)h.get(minIndex)*(end-start+1);maxArea=Math.max(maxArea,areaWithMin);// Push left and right rangesstack.push(newint[]{start,minIndex-1});stack.push(newint[]{minIndex+1,end});}returnmaxArea;}/* * Complete the 'largestRectangle' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts INTEGER_ARRAY h as parameter. */publicstaticlonglargestRectangle(List<Integer>h){// Write your code herereturnsolveSimpleIterativeWithStack(h);// return solveDivNCnqRecursive(h, 0, h.size() - 1);// return solveDivNCnqIterative(h);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Rectangle
You are viewing a single comment's thread. Return to all comments →
Here's my Java 8 solution, passing all test cases, for Max Score of 50.
While solving this problem, I could think of three approaches: two using Explicit Stacks, while one using Implicit Stack (via Recursion). All three of them pass ALL Test-Cases, for a Max Score of 50. All of them have been provided in the code, with some explanatory comments.