Advertisement
ogv

Untitled

ogv
Sep 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. class Solution {
  2. public int trap(int[] height) {
  3. if (height.length < 2) return 0;
  4.  
  5. int totalWater = 0;
  6.  
  7. int max1 = height[0];
  8. int max2 = 0;
  9. int sum1 = 0;
  10. int sum2 = 0;
  11. int n1 = 0;
  12. int n2 = 0;
  13. for (int i = 1; i < height.length; i++) {
  14. int h = height[i];
  15. if (h > max1) {
  16. max2 = max1;
  17. System.out.println("i=" + i + " max1=" + max1 + " max2=" + max2 + " n1=" + n1 + " n2="+ n2 + " sum1=" + sum1 + " sum2=" + sum2 + " delta " + ((n1+n2)*max2 - sum1 - sum2));
  18. totalWater += ((n1 + n2)*max2 - sum1 - sum2;
  19. max1 = h;
  20. max2 = 0;
  21. sum1 = 0;
  22. sum2 = 0;
  23. n1 = 0;
  24. n2 = 0;
  25. }
  26. else {
  27. n2++;
  28. sum2 += h;
  29. if (h >= max2){
  30. max2 = h;
  31. sum1 += sum2;
  32. sum2 = 0;
  33. n1 += n2;
  34. }
  35. }
  36. }
  37.  
  38. System.out.println(" max1=" + max1 + " max2=" + max2 + " n1=" + n1 + " sum1=" + sum1 + " sum2=" + sum2 + " delta " + (n1*max2 - sum1));
  39.  
  40. totalWater += n1*max2 - sum1;
  41.  
  42. return totalWater;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement