Skip to content

Commit 4c4a2cb

Browse files
committed
Time: 1097 ms (35.92%), Space: 162 MB (36.43%) - LeetHub
1 parent 7f4d1bd commit 4c4a2cb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
int minimumObstacles(vector<vector<int>>& grid) {
4+
5+
int n = grid.size(), m = grid[0].size();
6+
7+
queue<pair<int,int>> q;
8+
9+
vector<int> dx = {1,0,0,-1};
10+
vector<int> dy = {0,-1,+1,0};
11+
12+
vector<vector<int>> dp(n, vector<int>(m,INT_MAX));
13+
14+
q.push({0,0});
15+
dp[0][0] = 0;
16+
17+
while(!q.empty())
18+
{
19+
int x = q.front().first;
20+
int y = q.front().second;
21+
q.pop();
22+
23+
for(int i = 0; i<4; ++i)
24+
{
25+
int newx = x + dx[i];
26+
int newy = y + dy[i];
27+
28+
if(newx >= 0 and newy >= 0 and newx < n and newy < m and dp[x][y] + grid[newx][newy] < dp[newx][newy])
29+
{
30+
dp[newx][newy] = dp[x][y] + grid[newx][newy];
31+
q.push({newx,newy});
32+
}
33+
}
34+
}
35+
36+
return dp[n-1][m-1];
37+
}
38+
};

0 commit comments

Comments
 (0)