|
| 1 | +package binary_search; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +/** |
| 8 | + * Created by gouthamvidyapradhan on 07/04/2019 |
| 9 | + * On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). |
| 10 | + * |
| 11 | + * Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another |
| 12 | + * 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can |
| 13 | + * swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim. |
| 14 | + * |
| 15 | + * You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, |
| 16 | + * N-1)? |
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * |
| 20 | + * Input: [[0,2],[1,3]] |
| 21 | + * Output: 3 |
| 22 | + * Explanation: |
| 23 | + * At time 0, you are in grid location (0, 0). |
| 24 | + * You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0. |
| 25 | + * |
| 26 | + * You cannot reach point (1, 1) until time 3. |
| 27 | + * When the depth of water is 3, we can swim anywhere inside the grid. |
| 28 | + * Example 2: |
| 29 | + * |
| 30 | + * Input: [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]] |
| 31 | + * Output: 16 |
| 32 | + * Explanation: |
| 33 | + * 0 1 2 3 4 |
| 34 | + * 24 23 22 21 5 |
| 35 | + * 12 13 14 15 16 |
| 36 | + * 11 17 18 19 20 |
| 37 | + * 10 9 8 7 6 |
| 38 | + * |
| 39 | + * The final route is marked in bold. |
| 40 | + * We need to wait until time 16 so that (0, 0) and (4, 4) are connected. |
| 41 | + * Note: |
| 42 | + * |
| 43 | + * 2 <= N <= 50. |
| 44 | + * grid[i][j] is a permutation of [0, ..., N*N - 1]. |
| 45 | + * |
| 46 | + * Solution: O(N ^ 2 x log N ^ 2) |
| 47 | + * Binary search for the possible answers in the range [0 to N * N-1] and dfs through the grid to check if the |
| 48 | + * destination is reachable |
| 49 | + */ |
| 50 | +public class SwimInRisingWater { |
| 51 | + |
| 52 | + private final int[] R = {0, 0, 1, -1}; |
| 53 | + private final int[] C = {1, -1, 0, 0}; |
| 54 | + |
| 55 | + class Pair{ |
| 56 | + int r, c; |
| 57 | + Pair(int r, int c){ |
| 58 | + this.r = r; |
| 59 | + this.c = c; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean equals(Object o) { |
| 64 | + if (this == o) return true; |
| 65 | + if (!(o instanceof Pair)) return false; |
| 66 | + Pair pair = (Pair) o; |
| 67 | + return r == pair.r && |
| 68 | + c == pair.c; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int hashCode() { |
| 73 | + return Objects.hash(r, c); |
| 74 | + } |
| 75 | + } |
| 76 | + /** |
| 77 | + * Main method |
| 78 | + * @param args |
| 79 | + */ |
| 80 | + public static void main(String[] args) { |
| 81 | + int[][] grid = {{0,1,2,3,4},{24,23,22,21,5},{12,13,14,15,16},{11,17,18,19,20},{10,9,8,7,6}}; |
| 82 | + System.out.println(new SwimInRisingWater().swimInWater(grid)); |
| 83 | + } |
| 84 | + |
| 85 | + public int swimInWater(int[][] grid) { |
| 86 | + int l = 0, h = (grid.length * grid.length); |
| 87 | + int ans = 0; |
| 88 | + while(l <= h){ |
| 89 | + int m = l + (h - l) / 2; |
| 90 | + Set<Pair> done = new HashSet<>(); |
| 91 | + if(dfs(grid, 0, 0, done, m)){ |
| 92 | + ans = m; |
| 93 | + h = m - 1; |
| 94 | + } else{ |
| 95 | + l = m + 1; |
| 96 | + } |
| 97 | + } |
| 98 | + return ans; |
| 99 | + } |
| 100 | + |
| 101 | + private boolean dfs(int[][] grid, int r, int c, Set<Pair> done, int V){ |
| 102 | + if(r == grid.length - 1 && c == grid[0].length - 1) return true; |
| 103 | + done.add(new Pair(r, c)); |
| 104 | + for(int i = 0; i < 4; i ++){ |
| 105 | + int newR = r + R[i]; |
| 106 | + int newC = c + C[i]; |
| 107 | + if(newR >= 0 && newR < grid.length && newC >= 0 && newC < grid[0].length){ |
| 108 | + int childH = Math.max(V, grid[newR][newC]); |
| 109 | + int curH = Math.max(V, grid[r][c]); |
| 110 | + if(curH == childH){ |
| 111 | + Pair child = new Pair(newR, newC); |
| 112 | + if(!done.contains(child)){ |
| 113 | + if(dfs(grid, newR, newC, done, V)) return true; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
0 commit comments