@@ -46,12 +46,159 @@ We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
4646
4747## Thought Process {#thought-process}
4848
49- 1 . asd
49+ 1 . Binary Search and DFS
50+ 1 . Search between 0 and N \* N - 1 using binary search
51+ 2 . We stop when at a particular time, t, that the bottom is reached
52+ 3 . We create a DFS function reachBottom\( grid, t, N, visit, i, j\) , where t is the candidate and visit is boolean array to avoid repeated check
53+ 4 . Time complexity O\( log\( N\) \* N^2\)
54+ 5 . Space complexity O\( N^2\)
55+ 2 . Union Find
56+ 1 . Union find structure will be particularly useful for finding if two points are connected
57+ 2 . We start putting each grid and its flattened index in the map
58+ 3 . Use while loop to check 0 and N \* N - 1 is connected
59+ 4 . If not, we increase the water depth and get the respective flattened index out of map and connect its neighbors if the depth is greater than theirs
60+ 5 . If yes, we can simply return the depth
61+ 6 . Time complexity O\( N^2\) ???
62+ 7 . Space complexity O\( N^2\)
63+ 3 . Dijkstra's Search
64+ 1 . Create a node to store val and respective location i
65+ 2 . Use heap to store the nodes based on its val
66+ 3 . Every time, we poll the min node out and compared node's val with our current answer, res, where the max of two is our new answer
67+ 4 . If we have reach the right bottom corner, our search is done
68+ 5 . Else, we need to add current node's unvisited neighbors to the heap
69+ 6 . Time complexity O\( N^2log\( N\)\)
70+ 7 . Space complexity O\( N^2\)
5071
5172## Solution
5273
5374``` java
75+ class Solution {
76+ public int swimInWater (int [][] grid ) {
77+ int N = grid. length;
78+ int lo = 0 , hi = N * N - 1 ;
79+ while (lo < hi) {
80+ int mi = lo + (hi - lo) / 2 ;
81+ boolean [][] visit = new boolean [N ][N ];
82+ if (reachBottom(grid, mi, N , visit, 0 , 0 )) hi = mi;
83+ else lo = mi + 1 ;
84+ }
85+ return lo;
86+ }
87+
88+ private boolean reachBottom (int [][] grid , int t , int N , boolean [][] visit , int i , int j ) {
89+ if (i < 0 || i >= N || j < 0 || j >= N || visit[i][j] || grid[i][j] > t) return false ;
90+ visit[i][j] = true ;
91+ if (i == N - 1 && j == N - 1 ) return true ;
92+ else return reachBottom(grid, t, N , visit, i + 1 , j) || reachBottom(grid, t, N , visit, i - 1 , j) || reachBottom(grid, t, N , visit, i, j - 1 ) || reachBottom(grid, t, N , visit, i, j + 1 );
93+ }
94+ }
95+ ```
5496
97+ ``` java
98+ class Solution {
99+ private int [][] dirs = {{- 1 , 0 }, {1 , 0 }, {0 , - 1 }, {0 , 1 }};
100+
101+ private class UF {
102+ int [] ids;
103+ int [] size;
104+ public UF (int n ) {
105+ ids = new int [n];
106+ size = new int [n];
107+ for (int i = 0 ; i < n; i++ ) {
108+ ids[i] = i;
109+ size[i] = 1 ;
110+ }
111+ }
112+
113+ public boolean isConnected (int i , int j ) {
114+ return find(i) == find(j);
115+ }
116+
117+ private int find (int i ) {
118+ int root = i;
119+ while (ids[root] != root) root = ids[root];
120+ while (i != root) {
121+ int next = ids[i];
122+ ids[i] = root;
123+ i = next;
124+ }
125+ return ids[i];
126+ }
127+
128+ public void union (int i , int j ) {
129+ int rootI = find(i), rootJ = find(j);
130+ if (rootI == rootJ) return ;
131+ if (size[rootI] < size[rootJ]) {
132+ ids[rootI] = rootJ;
133+ size[rootJ] += size[rootI];
134+ } else {
135+ ids[rootJ] = rootI;
136+ size[rootI] += size[rootJ];
137+ }
138+ }
139+ }
140+
141+ public int swimInWater (int [][] grid ) {
142+ int N = grid. length;
143+ Map<Integer , Integer > map = new HashMap<> ();
144+ for (int i = 0 ; i < N ; i++ ) {
145+ for (int j = 0 ; j < N ; j++ ) {
146+ map. put(grid[i][j], i * N + j);
147+ }
148+ }
149+ UF uf = new UF (N * N );
150+ int res = 0 ;
151+ while (! uf. isConnected(0 , N * N - 1 )) {
152+ res++ ;
153+ int k = map. get(res);
154+ int i = k / N , j = k % N ;
155+ for (int [] dir : dirs) {
156+ int x = i + dir[0 ], y = j + dir[1 ];
157+ if (x >= 0 && x < N && y >= 0 && y < N && grid[i][j] > grid[x][y]) {
158+ uf. union(k, x * N + y);
159+ }
160+ }
161+ }
162+ return res;
163+ }
164+ }
165+ ```
166+
167+ ``` java
168+ class Solution {
169+ private class Node {
170+ int v, i, j;
171+ public Node (int v , int i , int j ) {
172+ this . v = v;
173+ this . i = i;
174+ this . j = j;
175+ }
176+ }
177+
178+ private int [][] dirs = {{- 1 , 0 }, {1 , 0 }, {0 , - 1 }, {0 , 1 }};
179+
180+ public int swimInWater (int [][] grid ) {
181+ int N = grid. length;
182+ PriorityQueue<Node > pq = new PriorityQueue<> ((a, b) - > a. v - b. v);
183+ pq. offer(new Node (grid[0 ][0 ], 0 , 0 ));
184+ int res = grid[0 ][0 ];
185+ boolean [][] visited = new boolean [N ][N ];
186+ while (! pq. isEmpty()) {
187+ Node cur = pq. poll();
188+ res = Math . max(cur. v, res);
189+ if (cur. i == N - 1 && cur. j == N - 1 ) break ;
190+ for (int [] dir : dirs) {
191+ int x = cur. i + dir[0 ];
192+ int y = cur. j + dir[1 ];
193+ if (x >= 0 && x < N && y >= 0 && y < N && ! visited[x][y]) {
194+ pq. offer(new Node (grid[x][y], x, y));
195+ visited[x][y] = true ;
196+ }
197+ }
198+ }
199+ return res;
200+ }
201+ }
55202```
56203
57204## Additional {#additional}
0 commit comments