int res=Integer.MIN_VALUE; for (inti=0; i < heights.length; i++) { int r=right[i]; int l=left[i]; int area=(r-l-1)*heights[i]; res=Math.max(res,area); } return res; } }
int[] maxLeft=newint[height.length]; int[] maxRight=newint[height.length]; for (inti=1; i < maxLeft.length-1; i++) { maxLeft[i]=Math.max(maxLeft[i-1],height[i-1]); } for (inti= maxRight.length - 2; i > 0; i--) { maxRight[i]=Math.max(maxRight[i+1],height[i+1]); }
int start=1; int end= height.length-2; int res=0; for(int i=start; i<=end; i++){ int left=maxLeft[i]; int right=maxRight[i]; if(left<=height[i] || right<=height[i]){ continue; } int h=Math.min(left,right)-height[i]; res+=h; } return res; }
publicintsumSubarrayMins(int[] arr) { int n=arr.length; int mod=(int)1e9+7; Deque<Integer> stack=newLinkedList<>(); int res=0; for (inti=0; i <= n; i++) { int curVal=i==n ? 0 : arr[i]; while(!stack.isEmpty() && arr[stack.peek()]>curVal){ int mid=stack.pop(); int l=stack.isEmpty() ? -1 : stack.peek(); long count=(mid-l)*(i-mid)%mod; long p=arr[mid]*count%mod; res+=p; res%=mod; } stack.push(i); } return res; }
publiclongsubArrayRanges(int[] nums) { long res=0; int n=nums.length; Deque<Integer> stack=newLinkedList<>(); for (inti=0; i <= n; i++) { int curVal= i==n ? Integer.MIN_VALUE : nums[i]; while(!stack.isEmpty() && nums[stack.peek()]>curVal){ int mid=stack.pop(); int l=stack.isEmpty() ? -1 : stack.peek(); res-=(long)(mid-l)*(i-mid)*nums[mid]; } stack.push(i); } stack.clear(); for (inti=0; i <= n; i++) { int curVal= i==n ? Integer.MAX_VALUE : nums[i]; while(!stack.isEmpty() && nums[stack.peek()]<curVal){ int mid=stack.pop(); int l=stack.isEmpty() ? -1 : stack.peek(); res+=(long)(mid-l)*(i-mid)*nums[mid]; } stack.push(i); } return res; }
1130. Minimum Cost Tree From Leaf Values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicintmctFromLeafValues(int[] arr) { Deque<Integer> stack=newLinkedList<>(); stack.push(Integer.MAX_VALUE); int res=0; for (int num : arr) { while(stack.peek()<=num){ //left mid right //stack.peek() stack.pop() num int mid=stack.pop(); res+=mid*Math.min(stack.peek(),num); } stack.push(num); } while(stack.size()>2){ res+=stack.pop()*stack.peek(); } return res; }
1475. Final Prices With a Special Discount in a Shop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicint[] finalPrices(int[] prices) { //next smaller or equal element int n=prices.length; int[] res=newint[n]; Deque<Integer> stack=newLinkedList<>(); for (inti= n-1; i >= 0; i--) { int cur=prices[i]; while(!stack.isEmpty() && stack.peek()>cur){ stack.pop(); } int dis=stack.isEmpty() ? 0 : stack.peek(); res[i]=cur-dis; stack.push(cur); } return res; }
1673. Find the Most Competitive Subsequence
If the element is greater than the dequeue’s kth element, we don’t need to store it. This also slightly improves performance while removing from the dequeue, as we didn’t add unnecessary elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicint[] mostCompetitive(int[] nums, int k) { Deque<Integer> stack=newLinkedList<>(); int n=nums.length; int[] res=newint[k]; for (inti=0; i < n; i++) { //make sure after deleting, there is >= k elements while(!stack.isEmpty() &&stack.size()+n-i>k && stack.peek()>nums[i]){ stack.pop(); } if(stack.size()<k){ stack.push(nums[i]); } } int idx=k-1; while(idx>=0){ res[idx--]=stack.pop(); } return res; }
publicdouble[] getCollisionTimes(int[][] cars) { int n=cars.length; double[] res=newdouble[n]; Arrays.fill(res,-1.0); Deque<Integer> stack=newLinkedList<>(); for (inti= n-1; i >= 0; i--) { int[] car=cars[i]; int pos=car[0]; int speed=car[1]; while(!stack.isEmpty()){ int j=stack.peek(); int[] car2=cars[j]; int pos2=car2[0]; int speed2=car2[1]; double time=(double)(pos2-pos)/(speed-speed2); if(speed>speed2 && (time<res[j] || res[j]<0)){ //1.faster //2.collide before car 2 vanishes, or never vanishes res[i]=time; break; }else{ //car2 would never be collided by [0,car1] stack.pop(); } } stack.push(i); } return res; }