From 660eb971ce6190547b58f9028b6ac9be846dc7fe Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 28 Nov 2024 23:49:22 +0530 Subject: [PATCH] Create 2290. Minimum Obstacle Removal to Reach Corner --- .... Minimum Obstacle Removal to Reach Corner | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2290. Minimum Obstacle Removal to Reach Corner diff --git a/2290. Minimum Obstacle Removal to Reach Corner b/2290. Minimum Obstacle Removal to Reach Corner new file mode 100644 index 0000000..698ddd2 --- /dev/null +++ b/2290. Minimum Obstacle Removal to Reach Corner @@ -0,0 +1,31 @@ +class Solution { +public: + int minimumObstacles(vector>& grid) { + priority_queue>, vector>>, greater>>> pq; + int n = grid.size(); + int m = grid[0].size(); + int dx[4] = {-1,0,1,0}; + int dy[4] = {0,1,0,-1}; + vector> check(n,vector(m,INT_MAX)); + pq.push({0,{0,0}}); + while(!pq.empty()){ + auto it = pq.top(); + pq.pop(); + int cw = it.first; + int cx = it.second.first; + int cy = it.second.second; + if(cx == n - 1 && cy == m - 1){ + return check[cx][cy]; + } + for(int i = 0; i < 4; i++){ + int nx = cx + dx[i]; + int ny = cy + dy[i]; + if(nx < n && nx >= 0 && ny < m && ny >= 0 && check[nx][ny] > grid[nx][ny] + cw){ + check[nx][ny] = grid[nx][ny] + cw; + pq.push({grid[nx][ny] + cw,{nx,ny}}); + } + } + } + return check[n-1][m-1]; + } +};