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
            int index = K;
            boolean found = false;
            for (int j = K; j > 0; j--) {
                int left = heights[j - 1];
                if (heights[j] > left) {
                    found = true;
                    index = left;
                }
            }
            if (found) {
                heights[index]++;
                continue outer;
            }

            // right
            index = K;
            found = false;
            for (int j = K; j < heights.length - 1; j++) {
                int right = heights[j + 1];
                if (heights[j] > right) {
                    found = true;
                    index = right;
                }
            }
            if (found) {
                heights[index]++;
                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]
//        System.out.println(Arrays.toString(prob.pourWater(new int[]{2, 1, 2, 3, 1, 2, 2}, 1, 3))); // [2,2,2,3,1,2,2]
    }
}

@@ -340,13 +340,18 @@
  = K
+ - 1
 ; j %3E
+=
  0; 
@@ -378,71 +378,38 @@
    i
-nt left = heights%5Bj - 1%5D;%0A                if (heights%5Bj%5D %3E left
+f (heights%5Bj%5D %3C heights%5Bindex%5D
 ) %7B%0A
@@ -470,20 +470,17 @@
 index = 
-left
+j
 ;%0A      
@@ -708,16 +708,20 @@
 nt j = K
+ + 1
 ; j %3C he
@@ -732,20 +732,16 @@
 s.length
- - 1
 ; j++) %7B
@@ -762,73 +762,38 @@
    i
-nt right = heights%5Bj + 1%5D;%0A                if (heights%5Bj%5D %3E right
+f (heights%5Bj%5D %3C heights%5Bindex%5D
 ) %7B%0A
@@ -854,21 +854,17 @@
 index = 
-right
+j
 ;%0A      
@@ -1387,24 +1387,26 @@
 ,2,3,2,2,2%5D%0A
+//
         Syst
@@ -1836,16 +1836,164 @@
 ,1,2,2%5D%0A
+        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1%7D, 2, 5))); // %5B1,2,3,4,3,3,2,2,3,4,3,2,1%5D%0A
     %7D%0A%7D%0A

Update problem 756