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

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

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -439,32 +439,117 @@
      index = j;%0A
+                %7D else if (heights%5Bj%5D %3E heights%5Bindex%5D) %7B%0A                    break;%0A
                 
@@ -908,32 +908,117 @@
      index = j;%0A
+                %7D else if (heights%5Bj%5D %3E heights%5Bindex%5D) %7B%0A                    break;%0A
                 
@@ -1167,32 +1167,32 @@
 K%5D++;%0A        %7D%0A
+
         return h
@@ -1208,936 +1208,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem756 prob = new Problem756();%0A//        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B2, 1, 2, 2, 1, 2, 2%7D, 1, 3))); // %5B2,2,2,2,1,2,2%5D%0A//        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B2, 1, 1, 2, 1, 2, 2%7D, 4, 3))); // %5B2,2,2,3,2,2,2%5D%0A//        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B2, 1, 1, 2, 1, 2, 2%7D, 1, 3))); // %5B2,1,2,2,1,2,2%5D%0A//        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B1, 2, 3, 4%7D, 2, 2))); // %5B2,3,3,4%5D%0A//        System.out.println(Arrays.toString(prob.pourWater(new int%5B%5D%7B3, 1, 3%7D, 5, 1))); // %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        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

Solve problem 756