Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int trap(int[] height) {
- if (height.length < 2) return 0;
- int totalWater = 0;
- int max1 = height[0];
- int max2 = 0;
- int sum1 = 0;
- int sum2 = 0;
- int n1 = 0;
- int n2 = 0;
- for (int i = 1; i < height.length; i++) {
- int h = height[i];
- if (h > max1) {
- max2 = max1;
- System.out.println("i=" + i + " max1=" + max1 + " max2=" + max2 + " n1=" + n1 + " n2="+ n2 + " sum1=" + sum1 + " sum2=" + sum2 + " delta " + ((n1+n2)*max2 - sum1 - sum2));
- totalWater += ((n1 + n2)*max2 - sum1 - sum2;
- max1 = h;
- max2 = 0;
- sum1 = 0;
- sum2 = 0;
- n1 = 0;
- n2 = 0;
- }
- else {
- n2++;
- sum2 += h;
- if (h >= max2){
- max2 = h;
- sum1 += sum2;
- sum2 = 0;
- n1 += n2;
- }
- }
- }
- System.out.println(" max1=" + max1 + " max2=" + max2 + " n1=" + n1 + " sum1=" + sum1 + " sum2=" + sum2 + " delta " + (n1*max2 - sum1));
- totalWater += n1*max2 - sum1;
- return totalWater;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement