package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/map-of-highest-peak/
 */
public class Problem1765 {
    public int[][] highestPeak(int[][] isWater) {
        // TODO
        return null;
    }

    private static void print(int[][] array) {
        for (int[] a : array) {
            System.out.println(Arrays.toString(a));
        }
    }

    public static void main(String[] args) {
        Problem1765 prob = new Problem1765();
        print(prob.highestPeak(new int[][]{
            {0, 1},
            {0, 0}
        })); // [[1,0],[2,1]]
        print(prob.highestPeak(new int[][]{
            {0, 0, 1},
            {1, 0, 0},
            {0, 0, 0}
        })); // [[1,1,0],[0,1,1],[1,2,2]]
    }
}

@@ -29,22 +29,50 @@
 va.util.
-Arrays
+LinkedList;%0Aimport java.util.Queue
 ;%0A%0A/**%0A 
@@ -217,87 +217,241 @@
     
-// TODO%0A        return null
+int maxRow = isWater.length;%0A        int maxCol = maxRow %3E 0 ? isWater%5B0%5D.length : 0
 ;%0A    
-%7D%0A%0A
     
-private static void print(int%5B%5D%5B%5D array
+Queue%3Cint%5B%5D%3E queue = new LinkedList%3C%3E();%0A        boolean%5B%5D%5B%5D visited = new boolean%5BmaxRow%5D%5BmaxCol%5D;%0A        for (int i = 0; i %3C maxRow; i++
 ) %7B%0A
@@ -458,24 +458,28 @@
         
+    
 for (int
 %5B%5D a : a
@@ -474,450 +474,1366 @@
 (int
-%5B%5D a : array) %7B%0A            System.out.println(Arrays.toString(a));%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1765 prob = new Problem1765();%0A        print(prob.highestPeak(new int%5B%5D%5B%5D%7B%0A            %7B0, 1%7D,%0A            %7B0, 0%7D%0A        %7D)); // %5B%5B1,0%5D,%5B2,1%5D%5D%0A        print(prob.highestPeak(new int%5B%5D%5B%5D%7B%0A            %7B0, 0, 1%7D,%0A            %7B1, 0, 0%7D,%0A            %7B0, 0, 0%7D%0A        %7D)); // %5B%5B1,1,0%5D,%5B0,1,1%5D,%5B1,2,2%5D%5D
+ j = 0; j %3C maxCol; j++) %7B%0A                if (isWater%5Bi%5D%5Bj%5D == 1) %7B%0A                    isWater%5Bi%5D%5Bj%5D = 0;%0A                    queue.add(new int%5B%5D%7Bi, j%7D);%0A                    visited%5Bi%5D%5Bj%5D = true;%0A                %7D%0A            %7D%0A        %7D%0A        while (!queue.isEmpty()) %7B%0A            int%5B%5D current = queue.remove();%0A            for (int%5B%5D adjacent : new int%5B%5D%5B%5D%7B%7B0, 1%7D, %7B0, -1%7D, %7B1, 0%7D, %7B-1, 0%7D%7D) %7B%0A                int row = current%5B0%5D + adjacent%5B0%5D;%0A                int col = current%5B1%5D + adjacent%5B1%5D;%0A                if (row %3C 0 %7C%7C row == maxRow %7C%7C col %3C 0 %7C%7C col == maxCol%0A                    %7C%7C visited%5Brow%5D%5Bcol%5D) %7B%0A                    continue;%0A                %7D%0A                int min = Integer.MAX_VALUE;%0A                for (int%5B%5D adj : new int%5B%5D%5B%5D%7B%7B0, 1%7D, %7B0, -1%7D, %7B1, 0%7D, %7B-1, 0%7D%7D) %7B%0A                    int adjRow = row + adj%5B0%5D;%0A                    int adjCol = col + adj%5B1%5D;%0A                    if (adjRow %3C 0 %7C%7C adjRow == maxRow %7C%7C adjCol %3C 0 %7C%7C adjCol == maxCol%0A                        %7C%7C !visited%5BadjRow%5D%5BadjCol%5D) %7B%0A                        continue;%0A                    %7D%0A                    min = Math.min(min, isWater%5BadjRow%5D%5BadjCol%5D);%0A                %7D%0A                isWater%5Brow%5D%5Bcol%5D = min + 1;%0A                visited%5Brow%5D%5Bcol%5D = true;%0A                queue.add(new int%5B%5D%7Brow, col%7D);%0A            %7D%0A        %7D%0A        return isWater;
 %0A   

Solve problem 1765