package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/pour-water/
 */
public class Problem756 {
    public int[] pourWater(int[] heights, int V, int K) {
        outer:
        for (int i = 0; i < V; i++) {
            // left
            for (int j = K; j >= 0; j--) {
                int left = (j - 1 < 0) ? Integer.MIN_VALUE : heights[j - 1];
                if (heights[j] < left) {
                    heights[j]++;
                    continue outer;
                }
            }
            // right
            for (int j = K; j < heights.length; j++) {
                int right = (j + 1 < heights.length) ? heights[j + 1] : Integer.MIN_VALUE;
                if (heights[j] < right) {
                    heights[j]++;
                    continue outer;
                }
            }
            heights[K]++;
        }
        return heights;
    }

    public static void main(String[] args) {
        Problem756 prob = new Problem756();
//        System.out.println(Arrays.toString(prob.pourWater(new int[] { 2, 1, 2, 2, 1, 2, 2 }, 1, 3))); // [2,2,2,2,1,2,2]
//        System.out.println(Arrays.toString(prob.pourWater(new int[] { 2, 1, 1, 2, 1, 2, 2 }, 4, 3))); // [2,2,2,3,2,2,2]
        System.out.println(Arrays.toString(prob.pourWater(new int[] { 2, 1, 1, 2, 1, 2, 2 }, 1, 3))); // [2,1,2,2,1,2,2]
//        System.out.println(Arrays.toString(prob.pourWater(new int[] { 1, 2, 3, 4 }, 2, 2))); // [2,3,3,4]
//        System.out.println(Arrays.toString(prob.pourWater(new int[] { 3, 1, 3 }, 5, 1))); // [4,4,4]
    }
}

@@ -1043,27 +1043,25 @@
 er(new int%5B%5D
- %7B 
+%7B
 2, 1, 2, 2, 
@@ -1059,33 +1059,32 @@
 1, 2, 2, 1, 2, 2
- 
 %7D, 1, 3))); // %5B
@@ -1159,35 +1159,33 @@
 rWater(new int%5B%5D
- %7B 
+%7B
 2, 1, 1, 2, 1, 2
@@ -1187,17 +1187,16 @@
  1, 2, 2
- 
 %7D, 4, 3)
@@ -1285,19 +1285,17 @@
 ew int%5B%5D
- %7B 
+%7B
 2, 1, 1,
@@ -1305,17 +1305,16 @@
  1, 2, 2
- 
 %7D, 1, 3)
@@ -1405,19 +1405,17 @@
 ew int%5B%5D
- %7B 
+%7B
 1, 2, 3,
@@ -1416,17 +1416,16 @@
  2, 3, 4
- 
 %7D, 2, 2)
@@ -1514,19 +1514,16 @@
 nt%5B%5D
- %7B 
+%7B
 3, 1, 3
- 
 %7D, 5
@@ -1541,16 +1541,134 @@
 %5B4,4,4%5D%0A
+        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B2, 1, 2, 3, 1, 2, 2%7D, 1, 3))); // %5B2,2,2,3,1,2,2%5D%0A
     %7D%0A%7D%0A

Update problem 756