Leetcode 403 Solution
This article provides solution to leetcode question 403 (frog-jump)
Access this page by simply typing in "lcs 403" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/frog-jump
Solution
class Solution {
public:
bool canCross(vector<int>& stones) {
if (stones.size() == 1)
return true;
unordered_map<int, set<int>> a;
for (auto stone : stones)
a[stone] = set<int>();
for (int i = 0; i < stones.size(); i++)
{
if (i == 0)
{
if (a.find(1) == a.end())
return false;
a[1].insert(1);
a[1].insert(2);
}
else
{
int stone = stones[i];
if (a.find(stone) == a.end())
continue;
auto steps = a[stone];
for (auto step : steps)
{
if (a.find(stone + step) != a.end())
{
a[stone + step].insert(step - 1);
a[stone + step].insert(step);
a[stone + step].insert(step + 1);
}
}
}
if (a[stones.back()].size() != 0)
return true;
}
return false;
}
};