package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/where-will-the-ball-fall/
 */
public class Problem1706 {
    public int[] findBall(int[][] grid) {
        // TODO
        return null;
    }

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

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -160,704 +160,1065 @@
     
-// TODO%0A        return null
+int maxRow = grid.length;%0A        int maxCol = grid.length %3E 0 ? grid%5B0%5D.length : 0
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1706 prob = new Problem1706();%0A        System.out.println(Arrays.toString(prob.findBall(new int%5B%5D%5B%5D%7B%0A            %7B1,1,1,-1,-1%7D,%0A            %7B1,1,1,-1,-1%7D,%0A            %7B-1,-1,-1,1,1%7D,%0A            %7B1,1,1,1,-1%7D,%0A            %7B-1,-1,-1,-1,-1%7D%0A        %7D))); // %5B1,-1,-1,-1,-1%5D%0A        System.out.println(Arrays.toString(prob.findBall(new int%5B%5D%5B%5D%7B%0A            %7B-1%7D%0A        %7D))); // %5B-1%5D%0A        System.out.println(Arrays.toString(prob.findBall(new int%5B%5D%5B%5D%7B%0A            %7B1,1,1,1,1,1%7D,%0A            %7B-1,-1,-1,-1,-1,-1%7D,%0A            %7B1,1,1,1,1,1%7D,%0A            %7B-1,-1,-1,-1,-1,-1%7D%0A        %7D))); // %5B0,1,2,3,4,-1%5D
+int%5B%5D answer = new int%5BmaxCol%5D;%0A        for (int ball = 0; ball %3C maxCol; ball++) %7B%0A            int col = ball;%0A            for (int row = 0; row %3C maxRow; row++) %7B%0A                if (grid%5Brow%5D%5Bcol%5D == 1) %7B%0A                    if (col + 1 %3C maxCol && grid%5Brow%5D%5Bcol + 1%5D == -1) %7B%0A                        answer%5Bball%5D = -1; // stuck%0A                        break;%0A                    %7D%0A                    col++;%0A                %7D else %7B // grid%5Brow%5D%5Bcol%5D == -1%0A                    if (col - 1 %3E= 0 && grid%5Brow%5D%5Bcol - 1%5D == 1) %7B%0A                        answer%5Bball%5D = -1; // stuck%0A                        break;%0A                    %7D%0A                    col--;%0A                %7D%0A                if (col %3C 0 %7C%7C col == maxCol) %7B%0A                    answer%5Bball%5D = -1; // stuck%0A                    break;%0A                %7D%0A                if (row == maxRow - 1) %7B%0A                    answer%5Bball%5D = col;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1706