Skip to content

Commit 04d4418

Browse files
authored
Merge pull request neetcode-gh#3264 from adityaiiitr/adityacpp
create 1091-shortestpath-in-binary-matrix.cpp
2 parents 779349c + 6092475 commit 04d4418

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

cpp/0787-cheapest-flights-within-k-stops.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/**
2+
This function uses the Bellman-Ford algorithm to find the cheapest price from source (src) to destination (dst)
3+
with at most k stops allowed. It iteratively relaxes the edges for k+1 iterations, updating the minimum
4+
cost to reach each vertex. The final result is the minimum cost to reach the destination, or -1 if the
5+
destination is not reachable within the given constraints.
6+
7+
Space Complexity: O(n) - space used for the prices array.
8+
Time Complexity: O(k * |flights|) - k iterations, processing all flights in each iteration.
9+
*/
10+
class Solution {
11+
public:
12+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
13+
vector<int> prices(n, INT_MAX);
14+
prices[src] = 0;
15+
16+
// Perform k+1 iterations of Bellman-Ford algorithm.
17+
for (int i = 0; i < k + 1; i++) {
18+
vector<int> tmpPrices(begin(prices), end(prices));
19+
20+
for (auto it : flights) {
21+
int s = it[0];
22+
int d = it[1];
23+
int p = it[2];
24+
25+
if (prices[s] == INT_MAX) continue;
26+
27+
if (prices[s] + p < tmpPrices[d]) {
28+
tmpPrices[d] = prices[s] + p;
29+
}
30+
}
31+
prices = tmpPrices;
32+
}
33+
return prices[dst] == INT_MAX ? -1 : prices[dst];
34+
}
35+
};
36+
37+
138
/*
239
Given cities connected by flights [from,to,price], also given src, dst, & k:
340
Return cheapest price from src to dst with at most k stops
@@ -75,4 +112,4 @@ class Solution {
75112
}
76113
return distances[dst];
77114
}
78-
};
115+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
int shortestPathBinaryMatrix(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
if(grid[0][0]==1 || grid[n-1][n-1]==1) return -1;
6+
7+
// {{r,c},length}
8+
queue<pair<pair<int,int>,int>> q;
9+
set<pair<int,int>> visited;
10+
11+
q.push({{0,0},0});
12+
visited.insert(make_pair(0,0));
13+
14+
int drow[] = {0,1,0,-1,1,-1,1,-1};
15+
int dcol[] = {1,0,-1,0,1,-1,-1,1};
16+
17+
while(!q.empty()){
18+
int row = q.front().first.first;
19+
int col = q.front().first.second;
20+
int len = q.front().second;
21+
22+
if(row == n-1 && col == n-1){
23+
return len+1;
24+
}
25+
26+
for(int i=0; i<8; i++){
27+
int nrow = row + drow[i];
28+
int ncol = col + dcol[i];
29+
30+
if(nrow>=0 && nrow<n && ncol>=0 && ncol<n && visited.find({nrow,ncol})==visited.end() && grid[nrow][ncol]==0){
31+
q.push({{nrow,ncol},len+1});
32+
visited.insert(make_pair(nrow,ncol));
33+
}
34+
}
35+
q.pop();
36+
}
37+
return -1;
38+
}
39+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy