Skip to content

Commit 7e2effd

Browse files
authored
Update minimum-cost-to-make-at-least-one-valid-path-in-a-grid.cpp
1 parent 4b7a8ad commit 7e2effd

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

C++/minimum-cost-to-make-at-least-one-valid-path-in-a-grid.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,24 @@ class Solution2 {
6060
{3, 1, 0}, {4, -1, 0}};
6161
const pair<int, int> b = {0, 0}, t = {grid.size() - 1, grid[0].size() - 1};
6262
deque<pair<pair<int, int>, int>> dq = {{b, 0}};
63-
unordered_map<int, int> lookup = {{b.first * grid[0].size() + b.second, 0}};
63+
unordered_set<int> lookup;
6464
while (!dq.empty()) {
6565
const auto [b, d] = dq.front(); dq.pop_front();
6666
if (b == t) {
6767
return d;
6868
}
69-
if (lookup[b.first * grid[0].size() + b.second] < d) {
69+
if (lookup.count(b.first * grid[0].size() + b.second)) {
7070
continue;
7171
}
72+
lookup.emplace(b.first * grid[0].size() + b.second);
7273
for (const auto& [nd, dr, dc] : directions) {
7374
const auto& nb = make_pair(b.first + dr, b.second + dc);
7475
const auto& cost = nd != grid[b.first][b.second] ? 1 : 0;
7576
if (!(0 <= nb.first && nb.first < grid.size() &&
7677
0 <= nb.second && nb.second < grid[0].size() &&
77-
(!lookup.count(nb.first * grid[0].size() + nb.second) ||
78-
lookup[nb.first * grid[0].size() + nb.second] > d + cost))) {
78+
!lookup.count(nb.first * grid[0].size() + nb.second))) {
7979
continue;
8080
}
81-
lookup[nb.first * grid[0].size() + nb.second] = d + cost;
8281
if (!cost) {
8382
dq.emplace_front(nb, d);
8483
} else {

0 commit comments

Comments
 (0)