-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode 1631.cpp
More file actions
43 lines (34 loc) · 1.27 KB
/
Leetcode 1631.cpp
File metadata and controls
43 lines (34 loc) · 1.27 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int n = heights.size();
int m = heights[0].size();
vector<vector<int>>dist(n,vector<int>(m,1e9));
dist[0][0]=0;
priority_queue< pair < pair<int,int> ,int>,
vector<pair < pair<int,int> ,int>>,
greater<pair < pair<int,int> ,int> >>pq;
pq.push({{0,0},0});
int delrow[]={-1,0,1,0};
int delcol[]={0,1,0,-1};
while(!pq.empty()){
int row = pq.top().first.first;
int col = pq.top().first.second;
int diff = pq.top().second;
pq.pop();
if(row == (n-1) && col ==(m-1))return diff;
for(int i=0;i<4;i++){
int nrow=row+delrow[i];
int ncol=col+delcol[i];
if(nrow>=0 && ncol>=0 && nrow<n && ncol<m){
int n_effort = max(abs(heights[row][col]-heights[nrow][ncol]),diff);
if(n_effort<dist[nrow][ncol]){
dist[nrow][ncol]=n_effort;
pq.push({{nrow,ncol},dist[nrow][ncol]});
}
}
}
}
return 0;
}
};