On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).
Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.
You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?
Example 1:
Input: [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
Example 2:
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]]
Output: 16
Explanation: 0 1 2 3 4
24 23 22 21 512 13 14 15 1611 17 18 19 20
10 9 8 7 6
The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
Note:
2 <= N <= 50.
grid[i][j] is a permutation of [0, …, N*N – 1].
Solution 1: Dijkstra’s Algorithm
Time complexity: O(n^2*logn) Space complexity: O(n^2)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Author: Huahua, running time: 8 ms
classSolution{
public:
intswimInWater(vector<vector<int>>&grid){
constintn=grid.size();
priority_queue<pair<int,int>>q;// {-time, y * N + x}
q.push({-grid[0][0],0*n+0});
vector<int>seen(n*n);
vector<int>dirs{-1,0,1,0,-1};
seen[0*n+0]=1;
while(!q.empty()){
auto node=q.top();q.pop();
intt=-node.first;
intx=node.second%n;
inty=node.second/n;
if(x==n-1&&y==n-1)returnt;
for(inti=0;i<4;++i){
inttx=x+dirs[i];
intty=y+dirs[i+1];
if(tx<0||ty<0||tx>=n||ty>=n)continue;
if(seen[ty*n+tx])continue;
seen[ty*n+tx]=1;
q.push({-max(t,grid[ty][tx]),ty*n+tx});
}
}
return-1;
}
};
Solution 2: Binary Search + BFS
Time complexity: O(2logn * n^2) Space complexity: O(n^2)
Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list “parts”.
The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.
The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.
Return a List of ListNode’s representing the linked list parts that are formed.
Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]
Example 1:
1
2
3
4
5
6
7
8
Input:
root=[1,2,3],k=5
Output:[[1],[2],[3],[],[]]
Explanation:
The input andeachelement of the output are ListNodes,notarrays.
Forexample,the input root has root.val=1,root.next.val=2,\root.next.next.val=3,androot.next.next.next=null.
The first element output[0]has output[0].val=1,output[0].next=null.
The last element output[4]isnull,but it'sstringrepresentation asaListNode is[].
Example 2:
1
2
3
4
5
Input:
root=[1,2,3,4,5,6,7,8,9,10],k=3
Output:[[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most1,andearlier parts arealarger size than the later parts.
Note:
The length of root will be in the range [0, 1000].
Each value of a node in the input will be an integer in the range [0, 999].