Skip to content

Commit 49b6b6a

Browse files
authored
Create 2290. Minimum Obstacle Removal to Reach Corner (#645)
2 parents ce7aec9 + 660eb97 commit 49b6b6a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int minimumObstacles(vector<vector<int>>& grid) {
4+
priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> pq;
5+
int n = grid.size();
6+
int m = grid[0].size();
7+
int dx[4] = {-1,0,1,0};
8+
int dy[4] = {0,1,0,-1};
9+
vector<vector<int>> check(n,vector<int>(m,INT_MAX));
10+
pq.push({0,{0,0}});
11+
while(!pq.empty()){
12+
auto it = pq.top();
13+
pq.pop();
14+
int cw = it.first;
15+
int cx = it.second.first;
16+
int cy = it.second.second;
17+
if(cx == n - 1 && cy == m - 1){
18+
return check[cx][cy];
19+
}
20+
for(int i = 0; i < 4; i++){
21+
int nx = cx + dx[i];
22+
int ny = cy + dy[i];
23+
if(nx < n && nx >= 0 && ny < m && ny >= 0 && check[nx][ny] > grid[nx][ny] + cw){
24+
check[nx][ny] = grid[nx][ny] + cw;
25+
pq.push({grid[nx][ny] + cw,{nx,ny}});
26+
}
27+
}
28+
}
29+
return check[n-1][m-1];
30+
}
31+
};

0 commit comments

Comments
 (0)