id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(const std::vector<int>& stones) {\n std::vector<std::vector<char>> memo(stones.size()+1, std::vector<char>(stones.size()+1, -1));\n return go(stones, 0, 0, memo);\n }\n\nprivate:\n bool go(const auto& stones, int idx, long speed, auto& memo) {\n if(idx == stones.size()-1)\n return true;\n if(memo[idx][speed] == -1){\n std::array<bool, 3> jump_options;\n for(int j = -1, i = 0; i<3; ++i, ++j) {\n auto next_it = std::lower_bound(stones.cbegin()+idx+1, stones.cend(), stones[idx]+speed+j);\n if(next_it == stones.cend() or stones[idx]+speed+j != *next_it)\n jump_options[i] = false;\n else\n jump_options[i] = go(stones, std::distance(stones.begin(), next_it), speed + j, memo);\n }\n memo[idx][speed] = std::ranges::any_of(jump_options, std::identity{});\n }\n return memo[idx][speed];\n }\n};\n\n", "memory": "72789" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int target;\n vector<map<int,bool>> mp;\n bool rec(set<int> &s, int j, int cv){\n if(j == 0) return false;\n if(cv > target) return false;\n if(cv == target) return true;\n if(s.find(cv) == s.end()) return false;\n if(mp[j].find(cv) != mp[j].end()){\n return mp[j][cv];\n }\n return mp[j][cv] = rec(s, j-1, cv+j) | rec(s, j, cv+j) | rec(s, j+1, cv+j);\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n target = stones[n-1];\n mp.resize(10000);\n set<int> s(stones.begin(), stones.end());\n return rec(s,1,0);\n }\n};", "memory": "92194" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n map<int, set<int>> dp;\n dp[stones[0] + 1] = {1}; // can get to dp[] from #'s in set\n for (int i = 1; i < n; ++i){\n // if (dp.find(stones[i]) == dp.end()) continue;\n for (auto k : dp[stones[i]]){\n // if (dp.find(stones[i] + k) != dp.end()){\n dp[stones[i] + k].insert(k);\n // } else dp[stones[i] + k] = {k};\n // if (dp.find(stones[i] + k - 1) != dp.end()){\n dp[stones[i] + k - 1].insert(k - 1);\n // } else dp[stones[i] + k - 1] = {k - 1};\n // if (dp.find(stones[i] + k + 1) != dp.end()){\n dp[stones[i] + k + 1].insert(k + 1);\n // } else dp[stones[i] + k + 1] = {k + 1};\n }\n }\n\n for (auto s : dp){\n for (auto i : s.second) cout << s.first << \" \" << i << \" \";\n cout << endl;\n }\n cout << stones[n - 1] << endl;\n return (dp[stones[n - 1]].size() != 0);\n }\n};", "memory": "92194" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n map<vector<int>,int>dp;\n int find(int node,vector<int>&v,int last)\n {\n if(node+1==v.size()) return 1;\n if(dp.find({node,last})!=dp.end()) return dp[{node,last}];\n int p=0;\n int n=v.size();\n int st=node+1;\n int en=v.size()-1;\n int ans=st;\n while(st<=en)\n {\n int mid=(st+en)/2;\n if(v[mid]-v[node]<=last)\n {\n ans=mid;\n st=mid+1;\n }\n else en=mid-1;\n }\n for(int i=ans;i<min(n,ans+3);i++)\n {\n if(abs(v[i]-v[node]-last)<=1)\n {\n p=max(p,find(i,v,v[i]-v[node]));\n }\n }\n return dp[{node,last}]=p;\n }\npublic:\n bool canCross(vector<int>& v) {\n if(v[1]-v[0]==1) return find(1,v,1);\n return false;\n }\n};", "memory": "97045" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n map<vector<int>,int>dp;\n int find(int node,vector<int>&v,int last)\n {\n if(node+1==v.size()) return 1;\n if(dp.find({node,last})!=dp.end()) return dp[{node,last}];\n int p=0;\n int n=v.size();\n int st=node+1;\n int en=v.size()-1;\n int ans=st;\n while(st<=en)\n {\n int mid=(st+en)/2;\n if(v[mid]-v[node]<=last)\n {\n ans=mid;\n st=mid+1;\n }\n else en=mid-1;\n }\n for(int i=ans;i<min(n,ans+3);i++)\n {\n if(abs(v[i]-v[node]-last)<=1)\n {\n p=max(p,find(i,v,v[i]-v[node]));\n }\n }\n return dp[{node,last}]=p;\n }\npublic:\n bool canCross(vector<int>& v) {\n if(v[1]-v[0]==1) return find(1,v,1);\n return false;\n }\n};", "memory": "97045" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\nbool ok=false;\n int dp[2001][10001];\n bool f(int i,int k,vector<int>& a){\n\n if(i>=a.size()-1){\n dp[i][k]=true;ok=true;return true;\n }\n if(dp[i][k]!=-1)return dp[i][k];\n bool x1=false,x2=false,x3=false;\n auto it=lower_bound(a.begin(),a.end(),a[i]+k+1)-a.begin();\n if(it<a.size() && a[it]==a[i]+k+1)x1=f(it,k+1,a);\n auto it1=lower_bound(a.begin(),a.end(),a[i]+k)-a.begin();\n if(it1<a.size() && a[it1]==a[i]+k)x2=f(it1,k,a);\n if(k!=1)\n { \n auto itr=lower_bound(a.begin(),a.end(),a[i]+k-1)-a.begin();\n if(itr<a.size() && a[itr]==a[i]+k-1)x3=f(itr,k-1,a);\n }\n return dp[i][k]=x1||x2||x3;\n }\n bool canCross(vector<int>& stones) {\n if(stones[0]+1!=stones[1])return false;\n memset(dp,-1,sizeof(dp));\n f(stones[1],1,stones);\n \n return ok;\n }\n};", "memory": "101896" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "using ll = long long;\nclass Solution {\npublic:\n int dp[2005][10000];\n unordered_map<int,int> mpp;\n bool rec(int lvl, int k, vector<int> &nums){\n // pruning\n if(k <= 0)\n return false;\n\n // base case\n if(lvl >= nums.size()-1){\n return true;\n } \n\n // cache check\n if(dp[lvl][k] != -1)\n return dp[lvl][k];\n\n // check\n bool res = false;\n if(mpp.find(nums[lvl]+k) != mpp.end()){\n res = rec(mpp[nums[lvl]+k], k, nums) || rec(mpp[nums[lvl]+k], k-1, nums) || rec(mpp[nums[lvl]+k], k+1, nums); \n }\n\n // result\n return dp[lvl][k] = res;\n\n }\n bool canCross(vector<int>& stones) {\n ios_base::sync_with_stdio(0); \n cout.tie(0); cin.tie(0);\n memset(dp,-1,sizeof(dp));\n for(int i = 0; i<stones.size(); i++){\n mpp[stones[i]] = i; \n }\n return rec(0,1,stones);\n }\n};", "memory": "101896" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool generate(const vector<int> &stones, int i, int k)\n {\n if (i == (stones.size() - 1))\n {\n return true;\n }\n bool flag = false;\n for (int j = i + 1; j != stones.size(); ++j)\n {\n if ((stones[j] - stones[i]) == (k + 1))\n {\n if (hash[j][k + 1] == INT_MIN)\n {\n hash[j][k + 1] = generate(stones, j, k + 1);\n }\n flag |= hash[j][k + 1];\n }\n else if ((stones[j] - stones[i]) == k)\n {\n if (hash[j][k] == INT_MIN)\n {\n hash[j][k] = generate(stones, j, k);\n }\n flag |= hash[j][k];\n }\n else if (k > 1 && (stones[j] - stones[i]) == (k - 1))\n {\n if (hash[j][k - 1] == INT_MIN)\n {\n hash[j][k - 1] = generate(stones, j, k - 1);\n }\n flag |= hash[j][k - 1];\n }\n if (flag) return true;\n }\n return flag;\n \n }\n bool canCross(vector<int>& stones) {\n if ((stones[1] - stones[0]) != 1) return false;\n hash.resize(stones.size());\n for (int i = 0; i != hash.size(); ++i) hash[i].resize(i + 1, INT_MIN);\n return generate(stones, 1, 1);\n }\nprivate:\n vector<vector<int>> hash;\n};", "memory": "106748" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool generate(const vector<int> &stones, int i, int k)\n {\n if (i == (stones.size() - 1))\n {\n return true;\n }\n bool flag = false;\n for (int j = i + 1; j != stones.size(); ++j)\n {\n if ((stones[j] - stones[i]) > (k + 1)) break;\n if ((stones[j] - stones[i]) == (k + 1))\n {\n if (hash[j][k + 1] == INT_MIN)\n {\n hash[j][k + 1] = generate(stones, j, k + 1);\n }\n flag |= hash[j][k + 1];\n if (flag) return true;\n }\n else if ((stones[j] - stones[i]) == k)\n {\n if (hash[j][k] == INT_MIN)\n {\n hash[j][k] = generate(stones, j, k);\n }\n flag |= hash[j][k];\n if (flag) return true;\n }\n else if (k > 1 && (stones[j] - stones[i]) == (k - 1))\n {\n if (hash[j][k - 1] == INT_MIN)\n {\n hash[j][k - 1] = generate(stones, j, k - 1);\n }\n flag |= hash[j][k - 1];\n if (flag) return true;\n }\n if (flag) return true;\n }\n return flag;\n \n }\n bool canCross(vector<int>& stones) {\n if ((stones[1] - stones[0]) != 1) return false;\n hash.resize(stones.size());\n for (int i = 0; i != hash.size(); ++i) hash[i].resize(i + 1, INT_MIN);\n return generate(stones, 1, 1);\n }\nprivate:\n vector<vector<int>> hash;\n};", "memory": "106748" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n unordered_map<int, short> si;\n vector<int> s;\n int n;\n vector<vector<short>> dp;\n\n short jumpable(int from, int jump) {\n if(s[from] + jump == s[n-1])\n return 1;\n if(jump == 0 || (s[from] + jump > s[n-1]))\n return 0;\n if(dp[from][jump] != -1)\n return dp[from][jump];\n \n if(si.find(s[from] + jump) == si.end())\n dp[from][jump] = 0;\n else {\n int newFrom = si[s[from] + jump];\n dp[from][jump] = (jumpable(newFrom, jump + 1) || jumpable(newFrom, jump) || jumpable(newFrom, jump - 1));\n }\n\n return dp[from][jump];\n }\n\n bool canCross(vector<int>& stones) {\n s = stones;\n\n if(s[1] != 1)\n return false;\n \n n = s.size();\n \n for(int i = 0; i < n; i++)\n si[s[i]] = i;\n\n dp = vector<vector<short>>(n + 1, vector<short>(n + 1, -1));\n return (jumpable(0, 1) == 1 ? true : false);\n }\n};", "memory": "111599" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool rec(int index,int k,vector<int>&stones,map<int,int>&mp,map<vector<int>,bool>&dp){\n if(index==stones.size()-1) return true;\n if(dp.find({index,k})!=dp.end()) return dp[{index,k}];\n bool ans=false;\n for(int i=k-1;i<=k+1;i++){\n if(i>0){\n int newval=stones[index]+i;\n if(mp.find(newval)!=mp.end()){\n ans|=rec(mp[newval],i,stones,mp,dp);\n }\n }\n }\n\n return dp[{index,k}]=ans;\n }\n bool canCross(vector<int>& stones) {\n map<int,int>mp;\n int n=stones.size();\n for(int i=0;i<n;i++){\n mp[stones[i]]=i;\n }\n map<vector<int>,bool>dp;\n // rec(0,0,stones,mp,dp);\n return rec(0,0,stones,mp,dp);\n }\n};", "memory": "116450" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int stlast = stones.back();\n map<int,set<int>> mp;\n int n = stones.size();\n for(int i=0;i<n;i++){\n set<int> s1;\n mp[stones[i]] = s1;\n }\n mp[0].insert(1);\n for(auto j:mp){\n int pos = j.first;\n set<int> s1 = j.second;\n for(auto nj:s1){\n int np = pos + nj;\n if(np==stlast){\n return true;\n }\n if(mp.count(np)){\n mp[np].insert(nj);\n mp[np].insert(nj+1);\n if((nj-1)>0){\n mp[np].insert(nj-1);\n }\n }\n } \n }\n return false;\n }\n};", "memory": "116450" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(const std::vector<int>& stones) {\n std::vector<std::vector<std::optional<bool>>> memo(stones.size()+1, std::vector<std::optional<bool>>(stones.size()+1));\n return go(stones, stones.cbegin(), 0, memo);\n }\n\nprivate:\n bool go(const auto& stones, std::vector<int>::const_iterator it, long last_jump, auto& memo) {\n if(it == stones.cend()-1)\n return true;\n if(not memo[std::distance(stones.begin(),it)][last_jump]){\n std::array<bool, 3> jump_options;\n for(int j = -1, i = 0; i<3; ++i, ++j) {\n auto next_it = std::lower_bound(it, stones.cend(), *it+last_jump+j);\n if(next_it == stones.cend() or next_it == it or *it+last_jump+j != *next_it)\n jump_options[i] = false;\n else\n jump_options[i] = go(stones, next_it, last_jump + j, memo);\n }\n memo[std::distance(stones.begin(), it)][last_jump].emplace(std::ranges::any_of(jump_options, std::identity{}));\n }\n return *memo[std::distance(stones.begin(), it)][last_jump];\n }\n};\n\n", "memory": "121301" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(const std::vector<int>& stones) {\n std::vector<std::vector<std::optional<bool>>> memo(stones.size()+1, std::vector<std::optional<bool>>(stones.size()+1));\n return go(stones, stones.cbegin(), 0, memo);\n }\n\nprivate:\n bool go(const auto& stones, std::vector<int>::const_iterator it, long speed, auto& memo) {\n if(it == stones.cend()-1)\n return true;\n if(not memo[std::distance(stones.begin(),it)][speed]){\n std::array<bool, 3> jump_options;\\\n for(int j = -1, i = 0; i<3; ++i, ++j) {\n auto next_it = std::lower_bound(std::next(it), stones.cend(), *it+speed+j);\n if(next_it == stones.cend() or *it+speed+j != *next_it)\n jump_options[i] = false;\n else\n jump_options[i] = go(stones, next_it, speed + j, memo);\n }\n memo[std::distance(stones.begin(), it)][speed].emplace(std::ranges::any_of(jump_options, std::identity{}));\n }\n return *memo[std::distance(stones.begin(), it)][speed];\n }\n};\n\n", "memory": "126153" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, bool>> ikMapping;\n unordered_map<int, unordered_map<int, bool>> ikVis;\n bool rec(vector<int>& s, int i, int k)\n {\n int n = s[i]+k;\n if(ikVis[i][k]) return ikMapping[i][k];\n //cout<<i<<\" \"<<k<<\" \"<<n<<endl;\n ikVis[i][k]=true;\n auto it = std::lower_bound(s.begin(), s.end(), n);\n if (it == s.end() || *it != n) {\n return ikMapping[i][k] = false;\n } \n int index = std::distance(s.begin(), it);\n if(index == s.size()-1) return ikMapping[i][k] = true;\n return ikMapping[i][k] = rec(s, index, k-1) || rec(s, index, k) || rec(s, index, k+1);\n }\n\n bool canCross(vector<int>& stones) {\n return rec(stones, 0, 1);\n }\n};", "memory": "126153" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n #define ll long long\n\n bool solve(vector<int>&stones, int index, int k, unordered_map<int,int>&mp, vector<vector<int>>&dp){\n if(index == stones.size()-1) return true;\n if(index >= stones.size()) return false;\n\n if(dp[index][k] != -1) return dp[index][k];\n\n if(mp.find(stones[index]+k+1) != mp.end()){\n if(solve(stones, mp[stones[index]+k+1], k+1, mp, dp)) return dp[index][k] = true;\n }\n if(mp.find(stones[index]+k) != mp.end()){\n if(solve(stones, mp[stones[index]+k], k, mp, dp)) return dp[index][k] = true;\n }\n if(k!=1 && mp.find(stones[index]+k-1) != mp.end()){\n if(solve(stones, mp[stones[index]+k-1], k-1, mp, dp)) return dp[index][k] = true;\n }\n\n return dp[index][k] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(stones[1] != 1) return false;\n if(stones.back() > 2001000) return false;\n\n vector<vector<int>> dp(n, vector<int>(n+1, -1));\n\n unordered_map<int, int> mp;\n for(int i = 0; i < n; i++){\n mp[stones[i]] = i;\n }\n return solve(stones, 1, 1, mp, dp);\n }\n};", "memory": "131004" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n #define ll long long\n\n bool solve(vector<int>&stones, int index, int k, unordered_map<int,int>&mp, vector<vector<int>>&dp){\n if(index == stones.size()-1) return true;\n if(index >= stones.size()) return false;\n\n if(dp[index][k] != -1) return dp[index][k];\n\n if(k!=1 && mp.find(stones[index]+k-1) != mp.end()){\n if(solve(stones, mp[stones[index]+k-1], k-1, mp, dp)) return dp[index][k] = true;\n }\n if(mp.find(stones[index]+k) != mp.end()){\n if(solve(stones, mp[stones[index]+k], k, mp, dp)) return dp[index][k] = true;\n }\n if(mp.find(stones[index]+k+1) != mp.end()){\n if(solve(stones, mp[stones[index]+k+1], k+1, mp, dp)) return dp[index][k] = true;\n }\n\n return dp[index][k] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(stones[1] != 1) return false;\n if(stones.back() > 2001000) return false;\n\n vector<vector<int>> dp(n, vector<int>(n+1, -1));\n\n unordered_map<int, int> mp;\n for(int i = 0; i < n; i++){\n mp[stones[i]] = i;\n }\n return solve(stones, 1, 1, mp, dp);\n }\n};", "memory": "135855" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n // vector<unordered_set<long long>> dp(n);\n // dp[0].insert(0);\n // for(int i = 1; i < n; ++i){\n // for(int j = 0; j < i; ++j){\n // long long diff = stones[i]-stones[j];\n // if(dp[j].count(diff) || dp[j].count(diff-1) || dp[j].count(diff+1)){\n // dp[i].insert(diff);\n // }\n // }\n // }\n // // for(int steps = 1; steps <= 2000; ++steps){\n // // if(helper(stones, n-1, steps)){\n // // return true;\n // // }\n // // }\n // // for(int j = 0; j <= 2000; ++j){\n // // if(dp[n-1][j]){\n // // return true;\n // // }\n // // }\n // return !dp[n-1].empty();\n unordered_map<int,unordered_map<long long,bool>> cache;\n for(int i = 0; i < n - 1; i++){\n int steps = stones[n-1]-stones[i];\n if(helper(stones, n-1, steps,cache)){\n return true;\n }\n }\n // for(int steps = 1; steps <= 2000; ++steps){\n // if(helper(stones, n-1, steps,cache)){\n // return true;\n // }\n // }\n return false;\n }\n bool helper(vector<int>& stones, int cur, int steps, unordered_map<int,unordered_map<long long,bool>>& cache){\n if(cur == 0 ){\n return steps == 0;\n }\n if(steps < 1){\n return false;\n }\n if(cache.count(cur) && cache[cur].count(steps)){\n return cache[cur][steps];\n }\n for(int i = cur-1; i >= 0; --i){\n long long dif = stones[cur]-stones[i];\n if(dif != steps ){\n continue;\n }else{\n if(helper(stones,i, dif,cache) || helper(stones,i, dif-1,cache) || helper(stones,i, dif+1,cache)){\n cache[cur][steps] = true;\n return true;\n }else{\n break;\n }\n }\n \n \n \n }\n cache[cur][steps] = false;\n return false;\n }\n};", "memory": "140706" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool solve(int idx, int prevJump, vector<vector<int>>& dp,\n vector<int>& stones) {\n int n = stones.size();\n if (idx == n - 1)\n return true;\n if (idx >= n)\n return false;\n if (stones[idx + 1] - stones[idx] > prevJump + 1)\n return false;\n\n if (dp[idx][prevJump] != -1) {\n return dp[idx][prevJump];\n }\n bool ans = false;\n for (int i = idx + 1; i < n; i++) {\n int diff = stones[i] - stones[idx];\n if (diff == prevJump - 1 || diff == prevJump ||\n diff == prevJump + 1) {\n ans = ans||solve(i, diff, dp, stones);\n }\n }\n\n return dp[idx][prevJump] = ans;\n }\n bool canCross(vector<int>& stones) {\n\n if (stones[1] - stones[0] > 1)\n return false;\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(1e3, -1));\n return solve(1, 1, dp, stones);\n }\n};", "memory": "145558" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n\n bool memo(vector<int>& stones, int index, int lastStep, vector<vector<int>>& dp) {\n if(index == stones.size() - 1) \n return true;\n\n if(dp[index][lastStep] != -1) return dp[index][lastStep];\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) {\n dp[index][lastStep] = memo(stones, i, stones[i] - stones[index], dp);\n if(dp[index][lastStep]) return true;\n } \n }\n\n return dp[index][lastStep] = false;\n }\n\n bool solve(vector<int>& stones, int index, int lastStep) {\n if(index == stones.size() - 1) \n return true;\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) \n if(solve(stones, i, stones[i] - stones[index])) return true;\n }\n\n return false;\n }\n\n // bool solve(vector<int>& stones, int index, int lastStep) {\n\n // if(index >= stones.size()) return false;\n // if(index == stones.size() - 1) return true;\n\n // bool sameStep = (lastStep + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep) : false;\n // bool oneStepDec = (lastStep - 1 + stones[index] == stones[index + 1]) ? \n // solve(stones, index + 1, lastStep - 1) : false;\n // bool oneStepInc = (lastStep + 1 + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep + 1) : false;\n\n // return sameStep || oneStepDec || oneStepInc;\n\n // }\n\npublic:\n bool canCross(vector<int>& stones) {\n\n if(stones[0] != 0 || stones[1] != 1) return false;\n\n vector<vector<int>> dp(stones.size(), vector<int>(1000, -1));\n // return solve(stones, 1, 1);\n return memo(stones, 1, 1, dp);\n\n }\n};", "memory": "150409" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n\n bool memo(vector<int>& stones, int index, int lastStep, vector<vector<int>>& dp) {\n if(index == stones.size() - 1) \n return true;\n\n if(dp[index][lastStep] != -1) return dp[index][lastStep];\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) {\n dp[index][lastStep] = memo(stones, i, stones[i] - stones[index], dp);\n if(dp[index][lastStep]) return true;\n } \n }\n\n return dp[index][lastStep] = false;\n }\n\n bool solve(vector<int>& stones, int index, int lastStep) {\n if(index == stones.size() - 1) \n return true;\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) \n if(solve(stones, i, stones[i] - stones[index])) return true;\n }\n\n return false;\n }\n\n // bool solve(vector<int>& stones, int index, int lastStep) {\n\n // if(index >= stones.size()) return false;\n // if(index == stones.size() - 1) return true;\n\n // bool sameStep = (lastStep + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep) : false;\n // bool oneStepDec = (lastStep - 1 + stones[index] == stones[index + 1]) ? \n // solve(stones, index + 1, lastStep - 1) : false;\n // bool oneStepInc = (lastStep + 1 + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep + 1) : false;\n\n // return sameStep || oneStepDec || oneStepInc;\n\n // }\n\npublic:\n bool canCross(vector<int>& stones) {\n\n if(stones[0] != 0 || stones[1] != 1) return false;\n\n vector<vector<int>> dp(stones.size(), vector<int>(1000, -1));\n // return solve(stones, 1, 1);\n return memo(stones, 1, 1, dp);\n\n }\n};", "memory": "150409" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\nprivate:\n bool helper(int ind,int k,vector<int>&stones,vector<vector<int>> &dp){\n if(ind==(stones.size()-1))\n return true;\n \n if(dp[ind][k]!=-1)\n return dp[ind][k];\n bool canReach = false;\n \n for(int i=ind+1;i<stones.size();i++){\n bool op1 = false,op2 = false,op3 = false;\n // jump k-1;\n if(stones[i]== stones[ind]+ k-1)\n op1 = helper(i,k-1,stones,dp);\n if(stones[i]== stones[ind]+ k)\n op2 = helper(i,k,stones,dp);\n if(stones[i]== stones[ind]+ k+1)\n op3 = helper(i,k+1,stones,dp);\n bool ans = (op1|| op2 || op3);\n canReach = canReach || ans;\n \n }\n return dp[ind][k] = canReach;\n }\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n+1,vector<int> (1e3,-1));\n return helper(0,0,stones,dp);\n }\n};", "memory": "155260" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool func(int i,vector<int> &stones,int k,vector<vector<int>> &dp)\n {\n if(i>=stones.size() || k==0)\n {\n return false;\n }\n if(i==stones.size()-1)\n {\n return true;\n }\n if(dp[i][k]!=-1)\n {\n return dp[i][k];\n }\n int a=lower_bound(stones.begin(),stones.end(),stones[i]+k-1)-stones.begin();\n bool flag=0;\n for(int j=a;j<stones.size();j++)\n {\n if(stones[j]<=stones[i]+k+1)\n {\n flag=func(j,stones,stones[j]-stones[i],dp);\n if(flag)\n {\n return dp[i][k]=true;\n }\n }\n else\n {\n break;\n }\n }\n return dp[i][k]=false;\n }\n\n bool canCross(vector<int>& stones) {\n vector<vector<int>> dp(stones.size(),vector<int> (1000,-1));\n if(stones[1]-stones[0]>1)\n {\n return false;\n }\n return func(1,stones,1,dp);\n }\n};", "memory": "160111" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool func(int i,vector<int> &stones,int k,vector<vector<int>> &dp)\n {\n if(i>=stones.size() || k==0)\n {\n return false;\n }\n if(i==stones.size()-1)\n {\n return true;\n }\n if(dp[i][k]!=-1)\n {\n return dp[i][k];\n }\n int a=lower_bound(stones.begin(),stones.end(),stones[i]+k-1)-stones.begin();\n bool flag=0;\n for(int j=a;j<stones.size();j++)\n {\n if(stones[j]<=stones[i]+k+1)\n {\n flag=func(j,stones,stones[j]-stones[i],dp);\n if(flag)\n {\n return dp[i][k]=true;\n }\n }\n else\n {\n break;\n }\n }\n return dp[i][k]=false;\n }\n\n bool canCross(vector<int>& stones) {\n vector<vector<int>> dp(stones.size(),vector<int> (1000,-1));\n if(stones[1]-stones[0]>1)\n {\n return false;\n }\n return func(1,stones,1,dp);\n }\n};", "memory": "160111" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool solve(vector<int> &stones, int index, int lastJump, int n, vector<vector<int>> &dp) {\n if (index == n - 1) // If we reach the last stone, return true\n return true;\n\n // If this state has already been computed, return the stored result\n if(dp[index][lastJump] != -1)\n return dp[index][lastJump];\n\n // For k-1, k, and k+1\n bool case1 = false, case2 = false, case3 = false;\n \n for (int i = index + 1; i < n; i++) {\n int gap = stones[i] - stones[index]; // Calculate the distance between stones\n\n // Check for k-1\n if (gap == lastJump - 1 && lastJump - 1 > 0) { \n case1 = solve(stones, i, gap, n, dp);\n }\n // Check for k\n else if (gap == lastJump) { \n case2 = solve(stones, i, gap, n, dp);\n }\n // Check for k+1\n else if (gap == lastJump + 1) { \n case3 = solve(stones, i, gap, n, dp);\n }\n\n // If any of the cases succeed, no need to continue further\n if (case1 || case2 || case3) \n return dp[index][lastJump] = true;\n }\n\n // If none of the cases succeed, return false\n return dp[index][lastJump] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int lastJump = 1; // The first jump must be 1 unit\n int n = stones.size();\n\n // If the second stone is not 1 unit away, it's impossible to cross\n if (stones[1] != 1) \n return false;\n\n // Initialize dp table with -1 (indicating the state has not been computed yet)\n vector<vector<int>> dp(n, vector<int>(n + 1, -1));\n\n return solve(stones, 1, lastJump, n, dp); // Start from the first stone with the first jump of 1\n }\n};\n", "memory": "164963" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int check(int num, int i, vector<int>&stones){\n for(int j = i; j < stones.size(); j++){\n if(num == stones[j]) return j;\n }\n return -1;\n }\n bool solve(int i, int k, vector<int>&stones, vector<vector<int>>&dp){\n int n = stones.size();\n if(i == n-1) return true;\n if(dp[i][k] != -1) return dp[i][k];\n bool ans = false;\n if(k-1 > 0){\n int x = check(stones[i] + k-1, i+1, stones);\n if(x != -1){\n ans |= solve(x, k-1, stones, dp);\n }\n }\n int x = check(stones[i]+k, i+1, stones);\n if(x != -1) ans |= solve(x, k, stones, dp);\n int q = check(stones[i] + k + 1, i+1,stones);\n if(q != -1) ans |= solve(q, k+1, stones, dp);\n return dp[i][k] = ans;\n }\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(stones[1] > stones[0] + 1) return false;\n vector<vector<int>>dp(n, vector<int>(n+2, -1));\n return solve(1, 1, stones, dp);\n }\n};", "memory": "169814" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& stones,int lasti,int curri,vector<vector<int>>&dp){\n if(curri==stones.size()-1)\n return true;\n if(dp[lasti][curri]) return false;\n int lastjump=stones[curri]-stones[lasti];\n int nexti=curri+1;\n while(nexti<stones.size() && stones[nexti]<=stones[curri]+lastjump+1){\n int nextjump=stones[nexti]-stones[curri];\n int jump=nextjump-lastjump;\n if(jump>=-1 && jump<=1){\n if(solve(stones,curri,nexti,dp)){\n return true;\n }\n }\n nexti++;\n }\n dp[lasti][curri]=1;\n return false;\n }\n bool canCross(vector<int>& stones) {\n int n=stones.size();\n if(stones[1]>1) return false;\n vector<vector<int>>dp(n,vector<int>(n,0));\n return solve(stones,0,1,dp);\n }\n};", "memory": "169814" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool func(vector<int>& stones, int index, int jump, int &n, vector<vector<int>> &dp) {\n if(index == n - 1){\n return true;\n }\n if(dp[index][jump] != -1){\n return dp[index][jump];\n }\n bool flag = false;\n for(int i = index + 1; i < n; i++){\n if(stones[i] - stones[index] > jump + 1){\n break;\n }\n for(int j = -1; j < 2; j++){\n if(stones[i] - stones[index] == jump + j){\n flag |= func(stones, i, jump + j, n, dp);\n }\n }\n }\n return dp[index][jump] = flag;\n }\n\n bool canCross(vector<int>& stones) {\n if(stones[1] - stones[0] > 1){\n return false;\n }\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int> (n + 1, -1));\n return func(stones, 1, 1, n, dp);\n }\n};\n", "memory": "174665" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool helper(const vector<int>& stones, const int pos, const int lastjump, std::unordered_map<int, vector<short>>& cache) const {\n if(lastjump == 0) {\n return false;\n }\n else if(cache.count(pos) != 0 && cache.find(pos)->second[lastjump] != -1) {\n return cache.find(pos)->second[lastjump];\n }\n auto& insert = cache.emplace(std::make_pair(pos, std::vector<short>(stones.size(), -1))).first->second;\n const auto found = std::lower_bound(stones.cbegin(), stones.cend(), pos);\n if(found == stones.cend() || *found != pos) {\n insert[lastjump] = 0;\n return false;\n }\n else if(found == std::prev(stones.cend())) {\n insert[lastjump] = 1;\n return true;\n }\n const auto res = helper(stones, pos+lastjump+1, lastjump+1, cache) ||\n helper(stones, pos+lastjump, lastjump, cache) ||\n helper(stones, pos+lastjump-1, lastjump-1, cache);\n insert[lastjump] = res ? 1 : 0;\n return res;\n }\n\n bool canCross(const vector<int>& stones) const {\n if(stones[1] != 1) {\n return false;\n }\n else if(stones.size() == 1) {\n return true;\n }\n const auto maxJump = static_cast<int>(stones.size());\n std::unordered_map<int, std::vector<short>> cache;\n auto lastjump = 1;\n auto pos = 1;\n return helper(stones, pos, lastjump, cache);\n }\n};", "memory": "198921" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\n\n bool solve(int k, vector<int> &stones, int idx, vector<vector<int>> &dp)\n {\n if(idx>stones.size())\n {\n return false;\n }\n if(idx==stones.size()-1)\n {\n return true;\n }\n if(dp[k][idx]!=-1) return dp[k][idx];\n bool ans = false;\n for(int i=idx+1;i<stones.size();i++)\n {\n int diff = stones[i]-stones[idx];\n if(diff==k-1 && k-1>0)\n {\n ans = ans || solve(k-1,stones,i,dp);\n }\n if(diff==k)\n {\n ans = ans || solve(k,stones,i,dp);\n }\n if(diff==k+1)\n {\n ans = ans || solve(k+1,stones,i,dp);\n }\n }\n return dp[k][idx] = ans;\n \n }\npublic:\n bool canCross(vector<int>& stones) {\n if(stones[1]!=1) return false;\n vector<vector<int>> dp(stones.size()+3,vector<int>(stones.size()+1,-1));\n return solve(1,stones,1,dp);\n }\n};", "memory": "203773" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool helper(vector<int>& stones, int index, int lastJump, vector<vector<int>>& dp) {\n if (index == stones.size() - 1) return true;\n if (dp[index][lastJump] != -1) return dp[index][lastJump];\n\n for (int jump = lastJump - 1; jump <= lastJump + 1; ++jump) {\n if (jump > 0) {\n int nextIndex = lower_bound(stones.begin(), stones.end(), stones[index] + jump) - stones.begin();\n \n if (nextIndex < stones.size() && stones[nextIndex] == stones[index] + jump) {\n if (helper(stones, nextIndex, jump, dp)) {\n return dp[index][lastJump] = true;\n }\n }\n }\n \n }\n\n return dp[index][lastJump] = false;\n }\n\n bool canCross(vector<int>& stones) {\n if (stones[1] != 1) return false;\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n, -1));\n return helper(stones, 1, 1, dp);\n }\n};\n", "memory": "208624" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& stones,int n,int i,int p,vector<vector<int>> &dp){\n if(i==n-1)\n return true;\n if(i>=n)\n return false;\n if(dp[i][p]!=-1)\n return dp[i][p];\n int k=stones[i]-stones[p];\n bool ans=false;\n for(int g=k-1;g<=k+1;g++){\n if(g==0)\n continue;\n int jump=stones[i]+g;\n int eleInd=lower_bound(stones.begin(),stones.end(),jump)-stones.begin();\n if(eleInd<n && stones[eleInd]==jump){\n ans=ans|solve(stones,n,eleInd,i,dp);\n }\n }\n return dp[i][p]=ans;\n }\n bool canCross(vector<int>& stones) {\n int n=stones.size();\n if(stones[1]-stones[0]!=1)\n return false;\n vector<vector<int>> dp(n,vector<int> (n,-1));\n return solve(stones,n,1,0,dp);\n }\n};", "memory": "213475" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool func(int i, int jumps, vector<int>& stones, vector<vector<int>>& dp){\n if(i==stones.size()-1)\n return 1;\n if(i>=stones.size())\n return 0;\n if(dp[i][jumps] != -1)\n return dp[i][jumps];\n bool ans=0;\n for(int t=-1;t<=1;t++)\n {\n if(jumps+t)\n {\n int g=lower_bound(stones.begin(), stones.end(), stones[i]+jumps+t)-stones.begin();\n if(g==stones.size() or stones[g]!=stones[i]+jumps+t)\n continue;\n ans = func(g, jumps+t, stones, dp) or ans;\n }\n }\n return dp[i][jumps] = ans;\n }\n bool canCross(vector<int>& stones) \n {\n if(stones[1]-stones[0]>1)\n return 0;\n vector<vector<int>> dp(stones.size()+1, vector<int>(stones.size()+1, -1));\n return func(0, 0, stones, dp);\n }\n};", "memory": "213475" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n // int solve(vector<int>& s, unordered_map<int,int>&m, int i, int j, vector<vector<int>>&dp){\n // if(i>=s.size()-1) return 1;\n // if(dp[i][j]!=-1) return dp[i][j];\n // int ans = 0;\n // if(m.count(s[i]+j-1) && j!=1) ans = ans | solve(s,m,m[s[i]+j-1],j-1,dp);\n // if(m.count(s[i]+j)) ans = ans | solve(s,m,m[s[i]+j],j,dp);\n // if(m.count(s[i]+j+1)) ans = ans | solve(s,m,m[s[i]+j+1],j+1,dp);\n // return dp[i][j]=ans;\n // }\n\n bool canCross(vector<int>& s) {\n int n = s.size();\n if(n==1) return 1;\n if(s[1]-s[0]>1) return 0;\n vector<vector<int>>dp(s.size()+1, vector<int>(s.size()+1,0));\n for(int i=1; i<=n; i++) dp[n-1][i]=1;\n unordered_map<int,int>m;\n for(int i=0; i<s.size(); i++){\n m[s[i]]=i;\n }\n for(int i=n-2; i>=1; i--){\n for(int j=n-1; j>=1; j--){\n int ans = 0;\n if(m.count(s[i]+j-1) && j!=1) ans = ans | dp[m[s[i]+j-1]][j-1];\n if(m.count(s[i]+j)) ans = ans | dp[m[s[i]+j]][j];\n if(m.count(s[i]+j+1)) ans = ans | dp[m[s[i]+j+1]][j+1];\n dp[i][j]=ans;\n }\n }\n return dp[1][1];\n }\n};", "memory": "218326" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n bool canCross(vector<int>& s) {\n int n = s.size();\n if(n==1) return 1;\n if(s[1]-s[0]>1) return 0;\n vector<vector<int>>dp(s.size()+1, vector<int>(s.size()+1,0));\n for(int i=1; i<=n; i++) dp[n-1][i]=1;\n unordered_map<int,int>m;\n for(int i=0; i<s.size(); i++){\n m[s[i]]=i;\n }\n for(int i=n-2; i>=1; i--){\n for(int j=n-1; j>=1; j--){\n int ans = 0;\n if(m.count(s[i]+j-1) && j!=1) ans = ans | dp[m[s[i]+j-1]][j-1];\n if(m.count(s[i]+j)) ans = ans | dp[m[s[i]+j]][j];\n if(m.count(s[i]+j+1)) ans = ans | dp[m[s[i]+j+1]][j+1];\n dp[i][j]=ans;\n }\n }\n return dp[1][1];\n }\n};", "memory": "218326" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n unordered_map<int,int> M;\n bool hhh(vector<int> &stones,int jump,int i,vector<vector<int>> &dp){\n if(i==stones.size()-1)\n return true;\n bool a=false,b=false,c=false;\n if(jump==0)\n return 0;\n if(dp[i][jump]!=-1)\n return dp[i][jump];\n if(M[stones[i]+jump-1]!=0){\n a=hhh(stones,jump-1,M[stones[i]+jump-1],dp);\n }\n if(M[stones[i]+jump]){\n b=hhh(stones,jump,M[stones[i]+jump],dp);}\n if(M[stones[i]+jump+1]){\n // cout<<stones[i]+jump+1<<\" \"<<stones[i]<<\" \"<<M[stones[i]+jump+1]<<endl;\n c=hhh(stones,jump+1,M[stones[i]+1+jump],dp);}\n return dp[i][jump]=a||b||c;\n }\n bool canCross(vector<int>& stones) {\n for(int i=1;i<stones.size();i++){\n M[stones[i]]=i;\n }\n if(stones[0]+1<stones[1])\n return false;\n vector<vector<int>> dp(stones.size(),vector<int>(stones.size(),-1));\n return hhh(stones,1,1,dp);\n }\n};", "memory": "223178" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\nint solve(vector<int>&v,int n,int i,int last,unordered_map<int,int>&mp,vector<vector<int>>&dp){\n if(i==n-1)\n return true;\n if(i>n-1)\n return false;\n\n if(dp[i][last]!=-1)\n return dp[i][last];\n\n int a=0,b=0,c=0;\n\n if(mp[v[i]+last]){\n a=a||solve(v,n,mp[v[i]+last],last,mp,dp);\n }\n if(mp[v[i]+last-1] && last!=1){\n b=b||solve(v,n,mp[v[i]+last-1],last-1,mp,dp);\n }\n if(mp[v[i]+last+1]){\n c=c||solve(v,n,mp[v[i]+last+1],last+1,mp,dp);\n }\n\n return dp[i][last] = a||b||c;\n\n}\n\n bool canCross(vector<int>& stones) {\n int n=stones.size();\n if(stones[1]-stones[0]>1)\n return 0;\n int last=1;\n vector<vector<int>>dp(n+1,vector<int>(n+1,-1));\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[stones[i]]=i;\n }\n return solve(stones,n,1,last,mp,dp);\n }\n};", "memory": "223178" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "bool canreach(vector<int>& stones, long long int k, long long int i, long long int n, map<long long int, long long int>& mpp, vector<vector<int>> &dp){\n if(i == n-1) return true;\n\n if(dp[i][k] != -1) return dp[i][k];\n\n bool first = false;\n if(k-1 > 0 && mpp[stones[i] + k - 1] > 0){\n first = canreach(stones, k-1, mpp[stones[i]+k-1], n, mpp, dp);\n }\n bool second = false;\n if(mpp[stones[i]+k] > 0){\n second = canreach(stones, k, mpp[stones[i]+k], n, mpp, dp);\n }\n bool third = false;\n if(mpp[stones[i]+k+1] > 0){\n third = canreach(stones, k+1, mpp[stones[i]+k+1], n, mpp, dp);\n }\n\n\n return dp[i][k] = first || second || third;\n}\nclass Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(stones[1] != 1) return false;\n map<long long int, long long int> mpp;\n for(int i = 0; i<n; i++){\n mpp[stones[i]] = i;\n }\n vector<vector<int>> dp(n+1, vector<int>(n, -1));\n \n bool flag = canreach(stones, 1, 1, n, mpp, dp);\n\n return flag;\n }\n};", "memory": "228029" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n + 2, 0));\n dp[0][1] = 1;\n for(int i = 1; i < n; i++) {\n for(int j = 0; j < i; j++) {\n int dist = stones[i] - stones[j];\n if(dist <= n + 1 && dp[j][dist] == 1) {\n dp[i][dist - 1] = 1;\n dp[i][dist] = 1;\n dp[i][dist + 1] = 1;\n }\n }\n }\n for(auto& a : dp.back()) if(a == 1) return true;\n return false;\n }\n};", "memory": "228029" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n dp[0][1] = 1;\n for(int i = 1; i < n; i++) {\n for(int j = 0; j < i; j++) {\n int dist = stones[i] - stones[j];\n if(dist < n && dp[j][dist] == 1) {\n dp[i][dist - 1] = 1;\n dp[i][dist] = 1;\n if(dist + 1 < n)\n dp[i][dist + 1] = 1;\n }\n }\n }\n for(auto& a : dp.back()) if(a == 1) return true;\n return false;\n }\n};", "memory": "232880" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(n + 1, 0));\n dp[0][1] = 1;\n for(int i = 1; i < n; i++) {\n for(int j = 0; j < i; j++) {\n int dist = stones[i] - stones[j];\n if(dist <= n && dp[j][dist] == 1) {\n dp[i][dist - 1] = 1;\n dp[i][dist] = 1;\n dp[i][dist + 1] = 1;\n }\n }\n }\n for(auto& a : dp.back()) if(a == 1) return true;\n return false;\n }\n};", "memory": "232880" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n bool solve(vector<int>&v, int idx, int step, int n){\n if(idx>n || step<0)\n return 0;\n if(idx==n-1){\n dp[idx][step]=1;\n return 1;\n }\n if(dp[idx][step]!=-1)\n return dp[idx][step];\n int j=0;\n for(int i=idx+1;i<n;i++){\n if(v[i]==(v[idx]+step)){\n j |= solve(v,i,step,n); \n }\n if(v[i]==(v[idx]+step+1)){\n j |= solve(v,i,step+1,n);\n }\n if(v[i]==(v[idx]+step-1)){\n j |= solve(v,i,step-1,n);\n }\n }\n dp[idx][step]=j;\n return dp[idx][step];\n }\n bool canCross(vector<int>& v) {\n int i,j,n=v.size(),k;\n dp = vector<vector<int>>(n+2,vector<int>(n+2,-1));\n j = solve(v,0,0,n);\n return j;\n }\n};", "memory": "237731" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool solve(int i,vector<int>&stones, vector<vector<int>>&dp,int k){\n int n = stones.size();\n if(i == n-1)return 1;\n if(dp[i][k]!=-1){\n return dp[i][k];\n }\n\n bool ans = 0;\n for(int jumps : {k-1,k,k+1}){\n if(jumps==0)continue;\n\n int next = lower_bound(stones.begin()+(i+1), stones.end(),stones[i]+jumps)\n -stones.begin();\n if(next == n || (stones[next]!=stones[i]+jumps)){continue;}\n ans = ans || solve(next,stones,dp,jumps);\n }\nreturn dp[i][k]=ans;}\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>>dp(n+1,vector<int>(n+1,-1));\n return solve(0,stones,dp,0);\n \n }\n};", "memory": "237731" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCrossII(int sum, int prev, vector<int>& stones, map<int, int>& m,\n map<vector<int>, bool>& dp) {\n if (sum == stones[stones.size() - 1]) {\n return true;\n }\n if (m.count(sum) == 0) {\n return false;\n }\n if (prev - 1 != 0) {\n if (dp.count({sum + prev - 1, prev - 1}) == 0) {\n dp[{sum + prev - 1, prev - 1}] =\n canCrossII(sum + prev - 1, prev - 1, stones, m, dp);\n }\n }\n if (dp.count({sum + prev, prev}) == 0) {\n dp[{sum + prev, prev}] =\n canCrossII(sum + prev, prev, stones, m, dp);\n }\n if (dp.count({sum + prev + 1, prev + 1}) == 0) {\n dp[{sum + prev + 1, prev + 1}] =\n canCrossII(sum + prev + 1, prev + 1, stones, m, dp);\n }\n if (prev - 1 != 0) {\n dp[{sum, prev}] = dp[{sum + prev, prev}] ||\n dp[{sum + prev - 1, prev - 1}] ||\n dp[{sum + prev + 1, prev + 1}];\n } else {\n dp[{sum, prev}] =\n dp[{sum + prev, prev}] || dp[{sum + prev + 1, prev + 1}];\n }\n return dp[{sum, prev}];\n }\n bool canCross(vector<int>& stones) {\n map<int, int> m;\n map<vector<int>, bool> dp;\n for (unsigned int i = 0; i < stones.size(); i++) {\n m[stones[i]]++;\n }\n return canCrossII(1, 1, stones, m, dp);\n }\n};", "memory": "242583" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCrossII(int sum, int prev, vector<int>& stones, map<int, int>& m,\n map<vector<int>, bool>& dp) {\n if (sum == stones[stones.size() - 1]) {\n return true;\n }\n if (m.count(sum) == 0) {\n return false;\n }\n if (prev - 1 != 0) {\n if (dp.count({sum + prev - 1, prev - 1}) == 0) {\n dp[{sum + prev - 1, prev - 1}] =\n canCrossII(sum + prev - 1, prev - 1, stones, m, dp);\n }\n }\n if (dp.count({sum + prev, prev}) == 0) {\n dp[{sum + prev, prev}] =\n canCrossII(sum + prev, prev, stones, m, dp);\n }\n if (dp.count({sum + prev + 1, prev + 1}) == 0) {\n dp[{sum + prev + 1, prev + 1}] =\n canCrossII(sum + prev + 1, prev + 1, stones, m, dp);\n }\n if (prev - 1 != 0) {\n dp[{sum, prev}] = dp[{sum + prev, prev}] ||\n dp[{sum + prev - 1, prev - 1}] ||\n dp[{sum + prev + 1, prev + 1}];\n } else {\n dp[{sum, prev}] =\n dp[{sum + prev, prev}] || dp[{sum + prev + 1, prev + 1}];\n }\n return dp[{sum, prev}];\n }\n bool canCross(vector<int>& stones) {\n map<int, int> m;\n map<vector<int>, bool> dp;\n for (unsigned int i = 0; i < stones.size(); i++) {\n m[stones[i]]++;\n }\n return canCrossII(1, 1, stones, m, dp);\n }\n};", "memory": "242583" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> jump(n, vector<int>(n + 1, 0));\n for (int i = 0; i < n; i ++) {\n for (int j = 0; j < i; j ++) {\n if (stones[i] - stones[j] >= n) continue;\n jump[j][stones[i] - stones[j]] = i;\n }\n }\n\n vector<vector<bool>> vis(n, vector<bool>(n + 1, false));\n vis[0][0] = true;\n for (int i = 0; i < n; i ++) {\n for (int j = 0; j <= i; j ++) {\n if (vis[i][j]) {\n for (int k = -1; k <= 1; k ++) {\n if (j + k <= 0) continue;\n if (jump[i][j + k]) {\n vis[jump[i][j + k]][j + k] = true;\n }\n }\n }\n }\n }\n bool res = false;\n for (int i = 1; i <= n; i ++) {\n res = res || vis[n - 1][i];\n }\n return res;\n }\n};", "memory": "247434" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>> jump(n, vector<int>(n + 1, 0));\n for (int i = 0; i < n; i ++) {\n for (int j = 0; j < i; j ++) {\n if (stones[i] - stones[j] >= n) continue;\n jump[j][stones[i] - stones[j]] = i;\n }\n }\n\n vector<vector<bool>> vis(n, vector<bool>(n + 1, false));\n vis[0][0] = true;\n for (int i = 0; i < n; i ++) {\n for (int j = 0; j <= i; j ++) {\n if (vis[i][j]) {\n for (int k = -1; k <= 1; k ++) {\n if (j + k <= 0) continue;\n if (jump[i][j + k]) {\n vis[jump[i][j + k]][j + k] = true;\n }\n }\n }\n }\n }\n bool res = false;\n for (int i = 1; i <= n; i ++) {\n res = res || vis[n - 1][i];\n }\n return res;\n }\n};", "memory": "247434" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool recurse(int i, int k, vector<int>& stones, vector<vector<int>>& dp){\n if(i == stones.size()-1){\n return true;\n }\n\n if(dp[i][k] != -1){\n return (bool) dp[i][k];\n }\n\n bool ret = false;\n vector<int> jumpSizes;\n if(k == 0){\n jumpSizes = {1};\n } else if(k == 1){\n jumpSizes = {1, 2};\n } else {\n jumpSizes = {k-1, k, k+1};\n }\n\n for(int jumpSize: jumpSizes){\n auto it = lower_bound(stones.begin(), stones.end(), stones[i] + jumpSize);\n int idx = it - stones.begin(); \n if(it != stones.end() && stones[idx]-stones[i] == jumpSize){\n ret |= recurse(idx, jumpSize, stones, dp);\n }\n }\n dp[i][k] = (ret) ? 1 : 0;\n return ret;\n }\n\n bool canCross(vector<int>& stones) {\n vector<vector<int>> dp(stones.size(), vector<int>(stones.size()+1, -1));\n return (bool) recurse(0, 0, stones, dp);\n }\n};", "memory": "252285" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n#define ll long long\n bool solve(vector<int>& stones, int index, int k, unordered_map<int, int>& mp, vector<vector<int>>& dp){\n if(index == stones.size()-1) return true;\n if(index >= stones.size()) return false;\n\n if(dp[index][k] != -1) return dp[index][k];\n\n bool a = false, b = false, c = false;\n if(k!=1 && mp.find(stones[index]+k-1) != mp.end()){\n a = solve(stones, mp[stones[index]+k-1], k-1, mp, dp);\n if(a) return dp[index][k] = true;\n }\n if(mp.find(stones[index]+k) != mp.end()){\n b = solve(stones, mp[stones[index]+k], k, mp, dp);\n if(b) return dp[index][k] = true;\n }\n if(mp.find(stones[index]+k+1) != mp.end()){\n c = solve(stones, mp[stones[index]+k+1], k+1, mp, dp);\n if(c) return dp[index][k] = true;\n }\n\n return dp[index][k] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if(stones[1] != 1) return false;\n if(stones.back() > 2001000) return false;\n\n vector<vector<int>> dp(n, vector<int>(2001, -1));\n\n unordered_map<int, int> mp;\n for(int i = 0; i < n; i++){\n mp[stones[i]] = i;\n }\n return solve(stones, 1, 1, mp, dp);\n }\n};", "memory": "252285" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n long long min(long long a, long long b){\n if(a<b) return a;\n return b;\n }\n bool will_cross(vector<int> &stones, int i, int k, vector<vector<int>> &dp){\n int dest=stones.back();\n int curr_place=stones[i];\n if(curr_place==dest) return true;\n else if(dp[i][k]!=-1) return dp[i][k];\n \n bool ans=false;\n\n for(int j=i+1;j<stones.size();j++){\n if(stones[j]-curr_place==k-1){\n ans=(ans||will_cross(stones,j,k-1,dp));\n }\n else if(stones[j]-curr_place==k){\n ans=(ans||will_cross(stones,j,k,dp));\n }\n else if(stones[j]-curr_place==k+1){\n ans=(ans||will_cross(stones,j,k+1,dp));\n }\n\n if(stones[j]-curr_place>=k+1) break;\n }\n\n dp[i][k]=ans;\n return dp[i][k];\n }\n bool canCross(vector<int>& stones) {\n \n if(stones.size()==1) return true;\n else if(stones[1]>1) return false;\n int n=stones.size();\n long long max_size=stones[n-1];\n max_size+=1;\n vector<vector<int>> dp(n,vector<int> (min(2001,max_size),-1));\n return will_cross(stones,1,1,dp);\n }\n};", "memory": "257136" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool find(int i, int lastSteps, vector<int>& stones, int n,\n vector<vector<int>>& dp) {\n if (i == n - 1)\n return true;\n\n if (dp[i][lastSteps] != -1)\n return dp[i][lastSteps];\n\n for (int j = i + 1; j < n; j++) {\n if (stones[j] > stones[i] + lastSteps + 1)\n break;\n\n if (stones[i] + lastSteps - 1 == stones[j])\n if (find(j, lastSteps - 1, stones, n, dp))\n return dp[i][lastSteps] = true;\n if (stones[i] + lastSteps == stones[j])\n if (find(j, lastSteps, stones, n, dp))\n return dp[i][lastSteps] = true;\n if (stones[i] + lastSteps + 1 == stones[j])\n if (find(j, lastSteps + 1, stones, n, dp))\n return dp[i][lastSteps] = true;\n }\n\n return dp[i][lastSteps] = false;\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n if (stones[1] - stones[0] != 1)\n return false;\n\n vector<vector<int>> dp(n, vector<int>(3000, -1));\n return find(1, 1, stones, n, dp);\n }\n};", "memory": "261988" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<vector<int>> dp;\nbool chk(int ind,int jl,vector<int> &stones){\n int n=stones.size();\n if(ind>=n-1)return true;\n if(dp[ind][jl]!=-1)return dp[ind][jl];\n int maxi=jl+1;\n int mini=jl-1;\n bool ans=false;\n //int dist=stones[ind+1]-stones[ind];\n for(int i=ind+1;i<n;i++){\n int rd=stones[i]-stones[ind];\n if(rd>=mini && rd<=maxi){\n ans=ans|chk(i,rd,stones);\n }\n \n }\n \n return dp[ind][jl]= ans;\n}\n bool canCross(vector<int>& stones) {\n if(stones[1]-stones[0]>1)return false;\n dp.assign(stones.size()+1,vector<int>(3000,-1));\n return chk(1,1,stones);\n }\n};", "memory": "266839" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\nunordered_map<int,int> mp;\nbool func(int i, int last, vector<int> &stones,vector<vector<int>>&dp){\n int n =stones.size();\n if(i==n-1) return true;\n if(dp[i][last]!=-1) return dp[i][last];\n bool ans=false;\n for(int next= last-1;next<=last+1;next++){\n int val = stones[i]+ next;\n \n if(mp[val] && mp[val]!=i) ans= ans | func(mp[val],next,stones,dp);\n };\n return dp[i][last]= ans;\n}\n bool canCross(vector<int>& stones) {\n if(stones[1]!=1) return false;\n int n = stones.size();\n \nfor(int i=0;i<n;i++){\n mp[stones[i]]=i;\n};\nvector<vector<int>> dp(n+1,vector<int>(3000,-1));\n return func(1,1,stones,dp);\n };\n};", "memory": "271690" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int target;\n vector<map<int,bool>> mp;\n bool rec(set<int> &s, int j, int cv){\n if(j == 0) return false;\n if(cv > target) return false;\n if(cv == target) return true;\n if(s.find(cv) == s.end()) return false;\n if(mp[j].find(cv) != mp[j].end()){\n return mp[j][cv];\n }\n return mp[j][cv] = rec(s, j-1, cv+j) | rec(s, j, cv+j) | rec(s, j+1, cv+j);\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n target = stones[n-1];\n mp.resize(100000);\n set<int> s(stones.begin(), stones.end());\n return rec(s,1,0);\n }\n};", "memory": "276541" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool canJump(int idx, int jump, vector<int> &stones, map<int, int> &mp, vector<vector<int>> &dp) {\n if (idx == stones.size() - 1) return 1;\n if (dp[idx][jump] != -1) return dp[idx][jump];\n for(int i=-1; i<=1; i++) {\n int newStone = stones[idx] + jump + i;\n if (jump+i > 0 and mp.contains(newStone) and canJump(mp[newStone], jump+i, stones, mp, dp)) {\n return dp[idx][jump] = 1;\n }\n }\n return dp[idx][jump] = 0;\n }\npublic:\n bool canCross(vector<int>& stones) {\n map<int, int> mp;\n vector<vector<int>> dp(stones.size(), vector<int>(3000, -1));\n for(int i=0; i<stones.size(); i++) {\n mp[stones[i]] = i;\n }\n return canJump(0, 0, stones, mp, dp);\n }\n};", "memory": "281393" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n int n;\n \npublic:\n bool solve(vector<int>& stones,int i,int k, vector<vector<int>>&dp){\n \n if(i>=n){\n return 0;\n }\n bool check=0;\n int j=i;\n int prev;\n if(i==0) prev=0;\n else prev=stones[i-1];\n //cout<<prev+k<<\" \";\n for(;j<n;j++){\n if(stones[j]==prev+k){\n check=1;break;\n }\n }\n if(dp[i][k]!=-1) return dp[i][k];\n if(!check) return 0;\n if(i==n-1||prev+k==stones[n-1]) return 1;\n if(i==0){\n return dp[i][k]= solve(stones,i+1,k+1,dp);\n }\n else{\n return dp[i][k]= solve(stones,j+1,k-1,dp)||solve(stones,j+1,k,dp)||solve(stones,j+1,k+1,dp);\n }\n \n }\n bool canCross(vector<int>& stones) {\n n=stones.size();\n vector<vector<int>>dp(n+1,vector<int>(3001,-1));\n return solve(stones,0,stones[0],dp);\n }\n};", "memory": "286244" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool help(int i,int k,vector<int>&s,vector<vector<int>>&dp){\n if(i==s.size()-1){\n return true;\n }\n bool a=false;\n if(dp[i][k]!=-1)return dp[i][k];\n for(int j=i+1;j<s.size();j++){\n if(s[j]>(s[i]+k+1))break;\n else if(s[j]==(s[i]+k-1)){\n a |= help(j,k-1,s,dp);\n }\n else if(s[j]==(s[i]+k)){\n a |= help(j,k,s,dp);\n }\n else if(s[j]==(s[i]+k+1)){\n a |= help(j,k+1,s,dp);\n }\n }\n return dp[i][k] = a;\n }\n bool canCross(vector<int>& s) {\n vector<vector<int>>dp(s.size()+2,vector<int>(4000,-1));\n return help(0,0,s,dp);\n }\n};\n\n// class Solution {\n// public:\n// vector<vector<int>> dp;\n\n// bool solve(int i, int last_jump, vector<int>& stones){\n// if(i>=stones.size()-1) return 1;\n// if(dp[i][last_jump] != -1) return dp[i][last_jump];\n\n// bool ans = false;\n// for(int j=i+1; j<stones.size(); j++){\n// if(stones[j]-stones[i] > last_jump+1) break;\n// for(int x=-1; x<2; x++){\n// if(stones[j]-stones[i] == last_jump+x){\n// ans = ans || solve(j,last_jump+x,stones);\n// }\n// }\n// }\n\n// return dp[i][last_jump] = ans;\n// }\n\n// bool canCross(vector<int>& stones) {\n// dp = vector<vector<int>> (stones.size()+2,vector<int>(4000,-1));\n// if(stones[1] - stones[0] > 1) return 0;\n// return solve(0,1,stones);\n// }\n// };", "memory": "291095" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool help(int i,int k,vector<int>&s,vector<vector<int>>&dp){\n if(i==s.size()-1){\n return true;\n }\n bool a=false;\n if(dp[i][k]!=-1)return dp[i][k];\n for(int j=i+1;j<s.size();j++){\n if(s[j]>(s[i]+k+1))break;\n else if(s[j]==(s[i]+k-1)){\n a |= help(j,k-1,s,dp);\n }\n else if(s[j]==(s[i]+k)){\n a |= help(j,k,s,dp);\n }\n else if(s[j]==(s[i]+k+1)){\n a |= help(j,k+1,s,dp);\n }\n }\n return dp[i][k]= a;\n }\n bool canCross(vector<int>& s) {\n vector<vector<int>>dp(s.size()+2,vector<int>(4000,-1));\n return help(0,0,s,dp);\n }\n};", "memory": "295946" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool help(int i,int k,vector<int>&s,vector<vector<int>>&dp){\n if(i==s.size()-1){\n return true;\n }\n bool a=false;\n if(dp[i][k]!=-1)return dp[i][k];\n for(int j=i+1;j<s.size();j++){\n if(s[j]>(s[i]+k+1))break;\n else if(s[j]==(s[i]+k-1)){\n a |= help(j,k-1,s,dp);\n }\n else if(s[j]==(s[i]+k)){\n a |= help(j,k,s,dp);\n }\n else if(s[j]==(s[i]+k+1)){\n a |= help(j,k+1,s,dp);\n }\n }\n return dp[i][k]= a;\n }\n bool canCross(vector<int>& s) {\n vector<vector<int>>dp(s.size()+2,vector<int>(4000,-1));\n return help(0,0,s,dp);\n }\n};", "memory": "300798" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\nunordered_map<int,int>mp;\nbool su(int stone,int jump,int target,vector<vector<int>>&dp){\n \n if(stone==target){\n return true;\n }\n int f=mp[stone];\n if(dp[f][jump]!=-1){\n return dp[f][jump];\n }\n if(mp[stone]==0){\n return dp[f][jump]=false;\n }\n if(jump<=0){\n return dp[f][jump]=false;\n }\n return dp[f][jump]=su(stone+jump,jump+1,target,dp)||su(stone+jump,jump,target,dp)||su(stone+jump,jump-1,target,dp);\n}\n bool canCross(vector<int>& stones) {\n int z=0;\n int j=0;\n for(auto it:stones){\n mp[it]=j+1;\n z=it;\n j++;\n }\n vector<vector<int>>dp(stones.size()+5,vector<int>(3000,-1));\n return su(0,1,z,dp);\n }\n};", "memory": "320203" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n int n;\n bool f(int ind, int jump, vector<int>& stone, vector<vector<int>>& dp){\n if(ind == n - 1) return true;\n if(dp[ind][jump] != -1) return dp[ind][jump];\n\n bool a = false;\n for(int j = ind + 1; j < n; j++){\n if(stone[j] > stone[ind] + jump + 1) break;\n else if(stone[j] == stone[ind] + jump - 1) a |= f(j, jump - 1, stone, dp);\n else if(stone[j] == stone[ind] + jump) a |= f(j, jump, stone, dp);\n else if(stone[j] == stone[ind] + jump + 1) a |= f(j, jump + 1, stone, dp);\n }\n\n return dp[ind][jump] = a;\n }\npublic:\n bool canCross(vector<int>& stones) {\n n = stones.size();\n if(stones[1] != 1) return false;\n\n vector<vector<int>> dp(n + 2, vector<int>(4000, -1));\n return f(0, 0, stones, dp);\n }\n};", "memory": "325054" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n vector<vector<int>> dp(2000,vector<int> (4001,0));\n for(int i=0;i<stones.size();i++){\n if(i==0){\n dp[i][0]=1;\n }\n else{\n for(int j=0;j<=i-1;j++){\n int jump=stones[i]-stones[j];\n if(stones[i]-stones[j]<=3999){\n \n if(dp[j][jump-1]||dp[j][jump]||dp[j][jump+1]){\n dp[i][jump]=1;\n if(i==stones.size()-1){\n return 1;\n }\n }\n }\n }\n }\n \n \n } \n // cout<<dp[0][0]<<\" \"<<dp[1][1]<<\" \"<<dp[2][2]<<\" \"<<dp[3][2]<<\" \"<<dp[5][3]<<\" \"<<dp[6][4]<<\" \"<<dp[7][5];\n return 0;\n \n }\n};", "memory": "329905" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n int n;\n bool f(int ind, int jump, vector<int>& stone, vector<vector<int>>& dp){\n if(ind == n - 1) return true;\n if(dp[ind][jump] != -1) return dp[ind][jump];\n\n bool a = false;\n for(int j = ind + 1; j < n; j++){\n if(stone[j] > stone[ind] + jump + 1) break;\n else if(stone[j] == stone[ind] + jump - 1) a |= f(j, jump - 1, stone, dp);\n else if(stone[j] == stone[ind] + jump) a |= f(j, jump, stone, dp);\n else if(stone[j] == stone[ind] + jump + 1) a |= f(j, jump + 1, stone, dp);\n }\n\n return dp[ind][jump] = a;\n }\npublic:\n bool canCross(vector<int>& stones) {\n n = stones.size();\n if(stones[1] != 1) return false;\n\n vector<vector<int>> dp(n + 2, vector<int>(4000, -1));\n return f(0, 0, stones, dp);\n }\n};", "memory": "334756" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n int n;\n bool f(int ind, int jump, vector<int>& stone, vector<vector<int>>& dp){\n if(ind == n - 1) return true;\n if(dp[ind][jump] != -1) return dp[ind][jump];\n\n bool a = false;\n for(int j = ind + 1; j < n; j++){\n if(stone[j] > stone[ind] + jump + 1) break;\n else if(stone[j] == stone[ind] + jump - 1) a |= f(j, jump - 1, stone, dp);\n else if(stone[j] == stone[ind] + jump) a |= f(j, jump, stone, dp);\n else if(stone[j] == stone[ind] + jump + 1) a |= f(j, jump + 1, stone, dp);\n }\n\n return dp[ind][jump] = a;\n }\npublic:\n bool canCross(vector<int>& stones) {\n n = stones.size();\n if(stones[1] != 1) return false;\n\n vector<vector<int>> dp(n + 2, vector<int>(4000, -1));\n return f(0, 0, stones, dp);\n }\n};", "memory": "334756" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector < vector <int>> dp;\n bool recfun( vector<int>& a , int k , int i ){\n if( dp[ i ][k] !=-1) return dp[i][k];\n if( i == a.size()-1) return dp [i ][ k ] = true;\n int curr = a[i];\n bool flg = false;\n int n = a.size() ;\n for( int j = i+1 ; j <n;j++){\n int num = a[ j ];\n if( num - curr > k + 1 ){ break;}\n if( num - curr == k - 1) flg = flg | recfun( a,k - 1,j );\n else if( num - curr == k ) flg = flg | recfun( a, k , j);\n else if( num - curr == k+1 ) flg = flg | recfun( a, k+1 , j);\n }\n\n return dp [ i ][ k ] = flg;\n }\n bool canCross( vector<int>& st ) {\n dp.resize(st.size() + 1 , vector <int> ( 2300, -1) );\n if( st[1] != 1) return false;\n return recfun( st , 1 , 1 );\n \n \n } \n};", "memory": "339608" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n \n vector<vector<int>>dp;\n bool fun(int ind,int jump,vector<int>&stones,int n){\n if(ind>=n)\n return false;\n if(ind==n-1)\n return true;\n if(dp[ind][jump]!=-1)\n return dp[ind][jump]==1;\n bool ans=false;\n if(ind==0){\n if(stones[ind+1] - stones[ind] == 1)\n ans = ans | fun(ind+1,1,stones,n);\n return ans;\n }\n for(int i=ind+1;i<n;i++){\n if((stones[i]-stones[ind])>jump+1){\n break;\n }\n if((stones[i]-stones[ind])==jump){\n ans=ans|fun(i,jump,stones,n);\n }\n if((stones[i]-stones[ind])==jump+1){\n ans=ans|fun(i,jump+1,stones,n);\n }\n if((stones[i]-stones[ind])==jump-1){\n ans=ans|fun(i,jump-1,stones,n);\n }\n\n }\n dp[ind][jump]=ans?1:0;\n return ans;\n }\npublic:\n bool canCross(vector<int>& stones) {\n int n=stones.size();\n dp.resize(3000,vector<int>(3000,-1));\n return fun(0,1,stones,n);\n }\n};", "memory": "344459" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n unordered_map<int,int> isPresent, ind;\n\n bool f(vector<int> &stones, vector<vector<int>> &dp, int i, int lastJump){\n if(i == stones.size() - 1){\n return true;\n }\n\n if(dp[i][lastJump] != -1){\n return dp[i][lastJump];\n }\n\n bool way1 = false, way2 = false, way3 = false;\n int next;\n if(lastJump - 1 > 0){\n next = stones[i] + lastJump - 1;\n if(isPresent[next] && next != stones[i]){\n way1 = f(stones, dp, ind[next], lastJump - 1);\n }\n }\n next = stones[i] + lastJump;\n if(isPresent[next] && next != stones[i]){\n way2 = f(stones, dp, ind[next], lastJump);\n }\n next = stones[i] + lastJump + 1;\n if(isPresent[next] && next != stones[i]){\n way3 = f(stones, dp, ind[next], lastJump+1);\n }\n\n return dp[i][lastJump] = (way1 | way2 | way3);\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n for(int i=0; i<stones.size(); i++){\n isPresent[stones[i]] = 1;\n ind[stones[i]] = i;\n }\n\n vector<vector<int>> dp(n+1, vector<int>(4000, -1));\n\n return f(stones, dp, 0, 0);\n }\n};", "memory": "349310" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int target;\n vector<map<int,bool>> mp;\n bool rec(set<int> &s, int j, int cv){\n if(j == 0) return false;\n if(cv > target) return false;\n if(cv == target) return true;\n if(s.find(cv) == s.end()) return false;\n if(mp[j].find(cv) != mp[j].end()){\n return mp[j][cv];\n }\n return mp[j][cv] = rec(s, j-1, cv+j) | rec(s, j, cv+j) | rec(s, j+1, cv+j);\n }\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n target = stones[n-1];\n mp.resize(1000000);\n set<int> s(stones.begin(), stones.end());\n return rec(s,1,0);\n }\n};", "memory": "354161" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n unordered_map<int,int>mp;\n\n bool solve(vector<int>&nums,int i,int k,vector<vector<int>>&dp){\n if(i>nums.size())return false;\n if(i==nums.size()-1)return true;\n if(mp[nums[i]+k]<=i)return false;\n if(dp[i][k]!=-1)return dp[i][k];\n bool kam=false;\n if(k>0)kam=solve(nums,mp[nums[i]+k],k-1,dp);\n bool bar=solve(nums,mp[nums[i]+k],k,dp);\n bool jada=solve(nums,mp[nums[i]+k],k+1,dp);\n return dp[i][k]=kam||bar||jada;\n }\n\n bool canCross(vector<int>& stones) {\n for(int i=0;i<stones.size();i++)mp[stones[i]]=i;\n vector<vector<int>>dp(2006,vector<int>(4000,-1));\n return solve(stones,0,1,dp);\n }\n};", "memory": "354161" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool helper(int ind, int jumps, vector<int>& stones, vector<vector<int>>& dp){\n if(ind == stones.size()-1) return true;\n\n if(dp[ind][jumps] != -1) return dp[ind][jumps];\n\n bool ans = false;\n for(int i = ind+1; i<stones.size(); i++){\n if((stones[i] - stones[ind]) > jumps+1) break;\n\n for(int t = -1; t<=1; t++){\n\n if((stones[i] - stones[ind]) == jumps+t){\n ans = helper(i, jumps+t, stones, dp);\n if(ans) return dp[ind][jumps] = ans;\n }\n }\n }\n return dp[ind][jumps] = ans;\n }\n bool canCross(vector<int>& stones) {\n if((stones[1] - stones[0]) > 1) return false;\n\n if(stones.size() == 2) return (stones[1] - stones[0]) == 1;\n vector<vector<int>> dp(stones.size(), \n vector<int> (2001, -1));\n\n return helper(1, 1, stones, dp);\n\n }\n};", "memory": "359013" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canCross(vector<int>& stones) {\n if(stones.size()==1)return true;\n if(stones[1]!=1)return false;\n vector<vector<int>> dp(stones.size(), vector<int>(2001, -1));\n return solve(1, 1, stones, dp);\n }\nprivate:\n bool solve(int i, int k, vector<int>& stones, vector<vector<int>>& dp) {\n if(i==stones.size()-1)return true;\n if(dp[i][k]!=-1)return dp[i][k];\n bool ans = false;\n for(int j = i+1; j<stones.size(); j++) {\n if(stones[j]-stones[i]>=k-1 && stones[j]-stones[i]<=k+1) {\n ans = ans|solve(j, stones[j]-stones[i], stones, dp);\n }\n }\n return dp[i][k] = ans;\n }\n};", "memory": "363864" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int idx, vector<int>& stones, int jump, int &n, vector<vector<int>> &dp){\n if(idx==n-1) return true;\n if(dp[idx][jump]!=-1) return dp[idx][jump];\n int i = idx;\n bool a = false;\n while(i<n && (stones[idx]+jump+1)>=stones[i]){\n if(stones[idx]+jump+1==stones[i]){\n a = a || solve(i, stones, jump+1, n, dp);\n }\n i++;\n }\n i = idx;\n while(i<n && (stones[idx]+jump)>=stones[i]){\n if(stones[idx]+jump==stones[i]){\n a = a || solve(i, stones, jump, n, dp);\n }\n i++;\n }\n i = idx+1;\n while(i<n && (stones[idx]+jump-1)>=stones[i]){\n if(stones[idx]+jump-1==stones[i]){\n a = a || solve(i, stones, jump-1, n, dp);\n }\n i++;\n }\n return dp[idx][jump] = a;\n }\n bool canCross(vector<int>& stones) {\n if(stones[1]>1) return false;\n int n = stones.size();\n vector<vector<int>> dp(n, vector<int>(2002, -1));\n return solve(1, stones, 1, n, dp);\n }\n};", "memory": "368715" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool canFrogCross(int index,int k,vector <int> &stones,vector <vector <int>>&dp) {\n int n = stones.size();\n // base case\n if(index == n-1)\n return true;\n if(dp[index][k] != -1)\n return dp[index][k];\n // main case\n bool ans = false;\n for(int i = -1;i <= 1;i++) {\n int K = k + i;\n for(int ind = index + 1;ind < n;ind++) {\n if(stones[index] + K < stones[ind]) break;\n if(stones[index] + K == stones[ind]) {\n if(canFrogCross(ind,K,stones,dp) == true) {\n return dp[index][k] = true;\n }\n }\n }\n }\n return dp[index][k] = ans;\n }\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n int index = 1;\n int k = 1;\n if(stones[1] != stones[0] + 1)\n return false;\n vector <vector <int>> dp(n,vector <int>(2001,-1)); \n return canFrogCross(index,k,stones,dp);\n }\n};", "memory": "373566" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool checkCross(int i,int jump,vector<int>& stones,vector<vector<int>>& dp) {\n if(i==stones.size()-1) return true;\n if(i>=stones.size()) return false;\n\n if(dp[i][jump]!=-1) return dp[i][jump]==1?true:false;\n\n bool ans = false;\n\n bool ans1=false,ans2=false,ans3=false;\n for(int j=i+1;j<stones.size();j++) {\n if(stones[j]-stones[i]==jump && jump!=0)\n ans1=checkCross(j,jump,stones,dp);\n if(stones[j]-stones[i]==jump+1)\n ans2=checkCross(j,jump+1,stones,dp);\n if(stones[j]-stones[i]==jump-1)\n ans3=checkCross(j,jump-1,stones,dp);\n \n ans= ans1 || ans2 || ans3 ;\n }\n\n if(ans){\n dp[i][jump]=1;\n } else {\n dp[i][jump]=0;\n }\n\n return ans;\n \n }\n bool canCross(vector<int>& stones) {\n int n=stones.size();\n if(stones[1]-stones[0]!=1) return false;\n vector<vector<int>> dp(n+1,vector<int> (2001,-1));\n return checkCross(1,1,stones,dp);\n }\n};", "memory": "373566" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n bool helper (int pos, int k, map<int,int>mp,int target)\n {\n if(pos==target)\n {\n return 1;\n }\n if(pos > target || k <0 || mp.find(pos)==mp.end())\n {\n return 0;\n }\n bool h1,h2;\n if(k!=0)\n {\n h1=helper(pos+k,k,mp,target);\n h2=helper(pos+k-1,k-1,mp,target);\n }\n \n bool h3=helper(pos+k+1,k+1,mp,target);\n\n return h1 || h2|| h3;\n\n\n }\n\n\n\n bool helper1(int index, int k, unordered_map<int,int>&mp,int &target,vector<int>&stones,vector<vector<int>>&dp)\n {\n if(stones[index]==target)\n {\n return 1;\n }\n if(stones[index]>target || k< 0)\n {\n return 0;\n }\n if(dp[index][k]!=-1)\n {\n return dp[index][k];\n }\n\n int h1=0,h2=0,h3=0;\n if(k!=0)\n {\n int nh1=stones[index]+k;\n int nh2=stones[index]+k-1;\n if(mp.find(nh1)!=mp.end())\n {\n h1=helper1(mp[nh1],k,mp,target,stones,dp);\n }\n if(mp.find(nh2)!=mp.end())\n {\n h2=helper1(mp[nh2],k-1,mp,target,stones,dp);\n }\n }\n\n int nh3=stones[index]+k+1;\n if(mp.find(nh3)!=mp.end())\n {\n h3=helper1(mp[nh3],k+1,mp,target,stones,dp);\n }\n\n return dp[index][k]=h1||h2||h3;\n }\n\n\n bool canCross(vector<int>& stones) {\n if(stones[1]!=1)\n {\n return 0;\n }\n unordered_map<int,int>mp;\n int n=stones.size();\n vector<vector<int>>dp(n,vector<int>(2000,-1));\n for(int i=0;i<stones.size();i++)\n {\n mp[stones[i]]=i;\n }\n return helper1(1,1,mp,stones[stones.size()-1],stones,dp);\n }\n};", "memory": "378418" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n/*\n int n;\n unordered_map<int, int> mp;\n int t[2001][2001];\n \n bool solve(vector<int>& stones, int curr_stone_index, int prevJump) {\n if(curr_stone_index == n-1)\n return true;\n \n bool result = false;\n \n if(t[curr_stone_index][prevJump] != -1)\n return t[curr_stone_index][prevJump];\n \n for(int nextJump = prevJump-1; nextJump <= prevJump+1; nextJump++) {\n \n if(nextJump > 0) {\n int next_stone = stones[curr_stone_index] + nextJump;\n \n if(mp.find(next_stone) != mp.end()) {\n result = result || solve(stones, mp[next_stone], nextJump);\n }\n }\n \n }\n \n return t[curr_stone_index][prevJump] = result;\n \n }\n \n bool canCross(vector<int>& stones) {\n \n if(stones[1] != 1)\n return false;\n \n n = stones.size();\n for(int i = 0; i<n; i++) {\n mp[stones[i]] = i;\n }\n \n memset(t, -1, sizeof(t));\n \n return solve(stones, 0, 0);\n }\n */\n \n bool f(vector<int>& s, int i, unordered_map<int, int>& map, int prev,vector<vector<int>>&dp) {\n if (i == s.size() - 1)\n return true;\n\n if(dp[i][prev]!=-1)return dp[i][prev];\n\n bool result = false;\n\n for (int next = prev - 1; next <= prev + 1; next++) {\n if (next >0) {\n int to_find = s[i] + next;\n if (map.find(to_find) != map.end()) {\n result = result || f(s, map[to_find], map, next,dp);\n }\n }\n }\n return dp[i][prev]=result;\n }\n bool canCross(vector<int>& s) {\n unordered_map<int, int> map;\n if (s[1] != 1)\n return false;\n\n for (int i = 0; i < s.size(); i++) {\n map[s[i]] = i;\n \n }\n vector<vector<int>>dp(s.size(),vector<int>(2001,-1));\n return f(s, 0, map, 0,dp);\n }\n\n};\n\n", "memory": "378418" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(int index, int k, vector<int>&stones, vector<vector<int>>&dp){\n if(index == stones.size() - 1)return true;\n if(dp[index][k]!=-1){return dp[index][k];}\n for(int i=k+1;i>=k-1;i--){\n for(int j=i;j>=1;j--){\n if(index + j < stones.size() && stones[index] + i == stones[index + j]){\n if(solve(index + j, i, stones, dp)==true){\n return dp[index][k] = true;\n }\n }\n }\n }\n return dp[index][k] = false;\n }\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n vector<vector<int>>dp(n,vector<int>(2005,-1));\n if(stones[1]>1) return false;\n return solve(1,1,stones,dp);\n }\n};", "memory": "383269" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int bs(int l, int h, vector<int>&nums, int key){\n \n while(l<=h){\n \n int mid=(h-l)/2+l;\n if(nums[mid]==key){\n // cout<<\"yay\";\n // cout<<mid;\n return mid;\n }\n else if(nums[mid]>key){\n h=mid-1;\n }\n else{\n l=mid+1;\n }\n }\n return -1;\n }\n int solve(int key, int k, vector<int>&stones, vector<vector<int>>&vj, int n){\n int a=bs(0, n-1, stones, key);\n // return 1;\n // cout<<a<<endl;\n // return 1;\n if(a==-1)return 0;\n if(a==stones.size()-1){cout<<\"YAY\";return 1;}\n \n if(vj[a][k]!=-1)return vj[a][k];\n int ans=0;\n if(k>0)ans=ans||solve(key+k, k, stones, vj, n);\n if(k-1>0)ans=ans||solve(key+k-1, k-1, stones, vj, n);\n if(k+1>0)ans=ans||solve(key+k+1, k+1, stones, vj, n);\n return vj[a][k]=ans;\n }\n bool canCross(vector<int>& stones) {\n vector<vector<int>>vj(stones.size(), vector<int>(2002, -1));\n int a= solve(stones[0], 0, stones, vj, stones.size());\n // for(int i=0; i<stones.size(); i++)cout<<vj[i]<<\" \";\n\n return (bool)a;\n }\n};", "memory": "383269" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "#include<bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n#define dd double\n#define sz size()\n#define rtn return\n#define rnt rtn\n\n#define vi vector<int>\n#define vvi vector<vi>\n#define vvvi vector<vvi>\n#define vl vector<ll>\n#define vvl vector<vl>\n#define vd vector<dd>\n#define vvd vector<vd>\n#define vc vector<char>\n#define vvc vector<vc>\n\n#define pii pair<int,int>\n#define pl pair<ll,ll>\n#define vpii vector<pii>\n#define vpl vector<pl>\n\n#define loop(i,a,b) for(int i=a;i<b;i++) //for loop from a(included)->b(excluded)\n#define all(x) x.begin(),x.end()\n#define sortAll(x) sort(all(x)); //sort in ascending\n#define tr(it,a) for(auto it=a.begin();it!=a.end();it++) // to iterate in map/set/vector/... named a.\n\n#define fi first\n#define se second\n#define top top()\n#define pop pop()\n#define pop_back pop_back()\n#define empty empty()\n#define pb push_back\n#define eb emplace_back\n\n//========================================================= DISJOINT SET =================================================================\nclass DisjointSet {\n\npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n int findUPar(int node) {\n if (node == parent[node])\n return node;\n return parent[node] = findUPar(parent[node]);\n }\n\n void unionByRank(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (rank[ulp_u] < rank[ulp_v]) {\n parent[ulp_u] = ulp_v;\n }\n else if (rank[ulp_v] < rank[ulp_u]) {\n parent[ulp_v] = ulp_u;\n }\n else {\n parent[ulp_v] = ulp_u;\n rank[ulp_u]++;\n }\n }\n\n void unionBySize(int u, int v) {\n int ulp_u = findUPar(u);\n int ulp_v = findUPar(v);\n if (ulp_u == ulp_v) return;\n if (size[ulp_u] < size[ulp_v]) {\n parent[ulp_u] = ulp_v;\n size[ulp_v] += size[ulp_u];\n }\n else {\n parent[ulp_v] = ulp_u;\n size[ulp_u] += size[ulp_v];\n }\n }\n};\n//========================================================================================================================================\n\n//using node=ListNode;\n//using node=TreeNode;\n\nconst int mod=1e9+7;\n\nint dr[]={-1,0,1,0}; //up,left,down,right\nint dc[]={0,-1,0,1};\nbool in(int i,int j,int n,int m){\n rtn (i>=0 && i<n && j>=0 && j<m);\n}\n\n\nclass Solution {\npublic:\n bool f(int idx,int cur,vi& stones,vvi& dp){\n int n=stones.sz;\n if(idx==n-1) rtn true;\n if(dp[idx][cur]!=-1){\n rtn (dp[idx][cur]==1)?true:false;\n }\n\n loop(i,cur-1,cur+2){\n if(i>0){\n int ele=stones[idx]+i;\n int a=lower_bound(stones.begin()+idx,stones.end(),ele)- stones.begin();\n if(a==n)continue;\n else if(stones[a]!=ele) continue;\n else{\n bool z=f(a,i,stones,dp);\n if(z){\n dp[idx][cur]=1;\n rnt true;\n }else continue;\n }\n }\n }\n\n dp[idx][cur]=0;\n rtn false;\n }\n bool canCross(vector<int>& stones) {\n vvi dp(stones.sz,vi(2001,-1));\n\n rtn f(0,0,stones,dp);\n }\n};", "memory": "388120" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool solve(int idx,int curr_jump,vector<int>& stones,vector<vector<int>>&dp)\n {\n\n if(idx==stones.size()-1)\n {\n return true;\n }\n\n\n if(dp[idx][curr_jump]!=-1)\n {\n if(dp[idx][curr_jump]==1)\n {\n return true;\n }\n else\n {\n return false;\n }\n }\n \n for(int i=curr_jump-1; i<=curr_jump+1; i++)\n {\n if(i>0)\n {\n int ele = stones[idx]+i;\n int a = lower_bound(stones.begin()+idx,stones.end(),ele)-stones.begin();\n\n if(a==stones.size())\n {\n continue;\n }\n else if(stones[a]!=ele)\n {\n continue;\n }\n else\n {\n bool z = solve(a,i,stones,dp);\n if(z==true)\n {\n dp[idx][curr_jump]=1;\n return true;\n }\n }\n }\n }\n\n dp[idx][curr_jump]=0;\n return false;\n }\n\n bool canCross(vector<int>& stones) {\n \n\n int idx = 0;\n int curr_jump = 0;\n vector<vector<int>>dp(stones.size()+1,vector<int>(2001,-1));\n bool ans = solve(idx,curr_jump,stones,dp);\n\n return ans;\n }\n};", "memory": "388120" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n\n bool memo(vector<int>& stones, int index, int lastStep, vector<vector<int>>& dp) {\n if(index == stones.size() - 1) \n return true;\n\n if(dp[index][lastStep] != -1) return dp[index][lastStep];\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) {\n dp[index][lastStep] = memo(stones, i, stones[i] - stones[index], dp);\n if(dp[index][lastStep]) return true;\n } \n }\n\n return dp[index][lastStep] = false;\n }\n\n bool solve(vector<int>& stones, int index, int lastStep) {\n if(index == stones.size() - 1) \n return true;\n\n for(int i = index + 1; i < stones.size(); i++) {\n if(stones[i] - stones[index] == lastStep || stones[i] - stones[index] == lastStep - 1 || \n stones[i] - stones[index] == lastStep + 1) \n if(solve(stones, i, stones[i] - stones[index])) return true;\n }\n\n return false;\n }\n\n // bool solve(vector<int>& stones, int index, int lastStep) {\n\n // if(index >= stones.size()) return false;\n // if(index == stones.size() - 1) return true;\n\n // bool sameStep = (lastStep + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep) : false;\n // bool oneStepDec = (lastStep - 1 + stones[index] == stones[index + 1]) ? \n // solve(stones, index + 1, lastStep - 1) : false;\n // bool oneStepInc = (lastStep + 1 + stones[index] == stones[index + 1]) ?\n // solve(stones, index + 1, lastStep + 1) : false;\n\n // return sameStep || oneStepDec || oneStepInc;\n\n // }\n\npublic:\n bool canCross(vector<int>& stones) {\n\n if(stones[0] != 0 || stones[1] != 1) return false;\n\n vector<vector<int>> dp(stones.size(), vector<int>(5000, -1));\n // return solve(stones, 1, 1);\n return memo(stones, 1, 1, dp);\n\n }\n};", "memory": "392971" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solveRec(int ind, int curr_jump, vector<int> &arr, map<int,int> &mp){\n if(curr_jump<=0) return false;\n\n if(ind==arr.size()-1) return true;\n\n if(!mp[arr[ind] + curr_jump]) return 0;\n\n bool op1 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump, arr, mp);\n bool op2 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump+1, arr, mp);\n bool op3 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump-1, arr, mp);\n\n return op1 || op2 || op3;\n }\n\n bool solveMem(int ind, int curr_jump, vector<int> &arr, map<int,int> &mp, \n vector<vector<int>> &dp){\n if(curr_jump<=0) return false;\n if(ind==arr.size()-1) return 1;\n\n if(!mp[arr[ind] + curr_jump]) return false;\n\n if(dp[ind][curr_jump]!=-1) return dp[ind][curr_jump];\n\n bool op1 = solveMem(mp[arr[ind] + curr_jump], curr_jump, arr, mp, dp);\n bool op2 = solveMem(mp[arr[ind] + curr_jump], curr_jump + 1, arr, mp, dp);\n bool op3 = solveMem(mp[arr[ind] + curr_jump], curr_jump - 1, arr, mp, dp);\n\n\n return dp[ind][curr_jump] = op1 || op2 || op3;\n }\n\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n map<int,int> mp;\n for(int i=0; i<n; i++){\n mp[stones[i]] = i;\n }\n\n // return solveRec(0, 1, stones, mp);\n \n vector<vector<int>> dp(n+1, vector<int>(2001, -1));\n return solveMem(0, 1, stones, mp, dp);\n\n // return solveTab(stone, mp);\n }\n};", "memory": "397823" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solveRec(int ind, int curr_jump, vector<int> &arr, map<int,int> &mp){\n if(curr_jump<=0) return false;\n\n if(ind==arr.size()-1) return true;\n\n if(!mp[arr[ind] + curr_jump]) return 0;\n\n bool op1 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump, arr, mp);\n bool op2 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump+1, arr, mp);\n bool op3 = solveRec(mp[arr[ind]+curr_jump]-1, curr_jump-1, arr, mp);\n\n return op1 || op2 || op3;\n }\n\n bool solveMem(int ind, int curr_jump, vector<int> &arr, map<int,int> &mp, \n vector<vector<int>> &dp){\n if(curr_jump<=0) return false;\n if(ind==arr.size()-1) return 1;\n\n if(!mp[arr[ind] + curr_jump]) return false;\n\n if(dp[ind][curr_jump]!=-1) return dp[ind][curr_jump];\n\n bool op1 = solveMem(mp[arr[ind] + curr_jump]-1, curr_jump, arr, mp, dp);\n bool op2 = solveMem(mp[arr[ind] + curr_jump]-1, curr_jump + 1, arr, mp, dp);\n bool op3 = solveMem(mp[arr[ind] + curr_jump]-1, curr_jump - 1, arr, mp, dp);\n\n\n return dp[ind][curr_jump] = op1 || op2 || op3;\n }\n\n\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n map<int,int> mp;\n for(int i=0; i<n; i++){\n mp[stones[i]] = i+1;\n }\n\n // return solveRec(0, 1, stones, mp);\n \n vector<vector<int>> dp(n+1, vector<int>(2001, -1));\n return solveMem(0, 1, stones, mp, dp);\n }\n};", "memory": "397823" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool f(vector<int> &v,int idx,int k,int n,map<int,int> &mp,vector<vector<int>> &dp){\n if(idx==n-1) return true;\n if(dp[idx][k] != -1) return dp[idx][k];\n bool res1=false,res2=false,res3=false;\n if(mp.count(v[idx] + k-1) && k!=1) res1 = f(v,mp[v[idx] + k - 1],k-1,n,mp,dp);\n if(mp.count(v[idx] + k)) res2 = f(v,mp[v[idx]+k],k,n,mp,dp);\n if(mp.count(v[idx] + k + 1)) res3 = f(v,mp[v[idx]+k+1],k+1,n,mp,dp);\n return dp[idx][k] = res1 || res2 || res3;\n }\n bool canCross(vector<int>& stones) {\n int n = stones.size();\n map<int,int> mp;\n for(int i=0;i<n;i++) {\n mp[stones[i]] = i;\n }\n if(n==1) return true;\n if(stones[1] - stones[0] != 1) return false;\n vector<vector<int>> dp(n,vector<int>(5000,-1));\n return f(stones,1,1,n,mp,dp);\n }\n};", "memory": "402674" }
403
<p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p> <p>Given a list of <code>stones</code>&nbsp;positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p> <p>If the frog&#39;s last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,3,5,6,8,12,17] <strong>Output:</strong> true <strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stones = [0,1,2,3,4,8,9,11] <strong>Output:</strong> false <strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= stones.length &lt;= 2000</code></li> <li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>stones[0] == 0</code></li> <li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li> </ul>
3
{ "code": "class Solution {\n unordered_map<int,int> mp;\n int solve(int i, int k, vector<int> &stones, vector<vector<int>> &dp){\n if(i==stones.size()-1)return 1;\n if(dp[i][k]!=-1)return dp[i][k];\n\n int ans=false;\n if(mp.count(stones[i]+k))ans=ans||solve(mp[stones[i]+k],k,stones,dp);\n if(k!=1 && mp.count(stones[i]+k-1))ans=ans||solve(mp[stones[i]+k-1],k-1,stones,dp);\n if(mp.count(stones[i]+k+1))ans=ans||solve(mp[stones[i]+k+1],k+1,stones,dp);\n\n return dp[i][k]=ans;\n }\npublic:\n bool canCross(vector<int>& stones) {\n mp.clear();\n int n=stones.size();\n if(stones[1]!=1)return 0;\n for(int i=0; i<n; i++){\n mp[stones[i]]=i;\n }\n vector<vector<int>> dp(n,vector<int>(2*n,-1));\n return solve(1,1,stones,dp);\n }\n};", "memory": "402674" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, bool isLeft, int &count) {\n if(root == NULL) {\n return;\n }\n inorder(root->left, true, count);\n if (isLeft && root->left == NULL && root->right == NULL) {\n count += root->val;\n }\n inorder(root->right, false, count);\n }\n int sumOfLeftLeaves(TreeNode* root) {\n int count = 0;\n inorder(root, false, count);\n return count;\n }\n};", "memory": "14700" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n return dfs(root, false);\n }\n int dfs(TreeNode* node, bool isLeft) {\n if (!node) return 0;\n if (!node->left && !node->right) return isLeft ? node->val : 0;\n int leftSum = dfs(node->left, true); \n int rightSum = dfs(node->right, false);\n return leftSum + rightSum;\n }\n};", "memory": "14700" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n int helper_func(TreeNode* root, bool left){\n if (root){\n if (root->left || root->right){\n int l = helper_func(root->left, true);\n int r = helper_func(root->right, false);\n return l + r;\n } else{\n return left*root->val;\n }\n }\n return 0;\n }\n \n \n int sumOfLeftLeaves(TreeNode* root) {\n return helper_func(root, false);\n \n \n }\n};", "memory": "14800" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode *root, int &sum){\n if(root == NULL){\n return ;\n }\n if(root -> left != NULL && root -> left -> left == NULL && root -> left -> right == NULL){\n sum += root -> left -> val;\n }\n solve(root -> left, sum);\n solve(root -> right, sum);\n }\n int sumOfLeftLeaves(TreeNode* root) {\n int sum = 0;\n solve(root, sum);\n return sum;\n }\n};", "memory": "14800" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n return inorder(root,false);\n \n }\n int inorder(TreeNode* root,bool isLeft){\n if(root==NULL)\n return 0;\n if(isLeft==true && root->left==NULL && root->right==NULL){\n return root->val;\n }\n return inorder( root->left,true)+inorder( root->right,false);\n }\n};", "memory": "14900" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n if (root == NULL)\n return 0;\n int sum = 0;\n if (root -> left != NULL && root -> left -> left == NULL && root -> left -> right == NULL) {\n sum += root -> left -> val;\n }\n return (sum + sumOfLeftLeaves(root -> left) + sumOfLeftLeaves(root -> right));\n }\n};", "memory": "14900" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n if(root == NULL) return 0;\n int sum = 0;\n if(root->left && isleaf(root->left)){\n sum += root->left->val;\n }\n sum += sumOfLeftLeaves(root->left);\n sum += sumOfLeftLeaves(root->right);\n\n return sum;\n }\n bool isleaf(TreeNode * root){\n if(root!= NULL && root->left == NULL && root->right == NULL){\n return true;\n }\n return false;\n }\n};", "memory": "15000" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n if (root == nullptr)\n return 0;\n\n int ans = 0;\n\n if (root->left) {\n if (root->left->left == nullptr && root->left->right == nullptr)\n ans += root->left->val;\n else\n ans += sumOfLeftLeaves(root->left);\n }\n ans += sumOfLeftLeaves(root->right);\n\n return ans; \n }\n};", "memory": "15000" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n if (root == NULL)\n return 0;\n int sum = 0;\n if (root -> left != NULL && root -> left -> left == NULL && root -> left -> right == NULL) {\n sum += root -> left -> val;\n }\n return (sum + sumOfLeftLeaves(root -> left) + sumOfLeftLeaves(root -> right));\n }\n};", "memory": "15100" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, int& res)\n {\n if (root == NULL)\n return ;\n \n inorder(root->left, res);\n\n if (root->left != NULL && root->left->left == NULL && root->left->right == NULL)\n {\n res += root->left->val;\n }\n \n inorder(root->right, res);\n }\n int sumOfLeftLeaves(TreeNode* root) {\n int res = 0;\n inorder(root, res);\n\n return res;\n }\n};", "memory": "15100" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* root, bool flag, int& sum) {\n if( root == NULL) return ;\n if (root->left == NULL && root->right == NULL && flag)\n sum += root->val;\n solve(root->left, true, sum);\n solve(root->right, false, sum);\n }\n\n int sumOfLeftLeaves(TreeNode* root) {\n int sum = 0;\n solve(root, false, sum);\n\n return sum;\n }\n};", "memory": "15200" }
404
<p>Given the <code>root</code> of a binary tree, return <em>the sum of all left leaves.</em></p> <p>A <strong>leaf</strong> is a node with no children. A <strong>left leaf</strong> is a leaf that is the left child of another node.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg" style="width: 277px; height: 302px;" /> <pre> <strong>Input:</strong> root = [3,9,20,null,null,15,7] <strong>Output:</strong> 24 <strong>Explanation:</strong> There are two left leaves in the binary tree, with values 9 and 15 respectively. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>-1000 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int sumOfLeftLeaves(TreeNode* root) {\n if (!root->left && !root->right) return 0;\n stack<TreeNode*> s;\n int sum = 0;\n bool flag = 0; // 1表示在此之前是走左邊\n while (root || !s.empty()) {\n if (root) {\n s.push(root);\n root = root->left;\n if (root && !root->left && !root->right) sum+= root->val;\n }\n else {\n root = s.top();\n s.pop();\n root = root->right;\n }\n }\n return sum;\n\n }\n};", "memory": "15200" }
406
<p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p> <p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] <strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] <strong>Explanation:</strong> Person 0 has height 5 with no other people taller or the same height in front. Person 1 has height 7 with no other people taller or the same height in front. Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1. Person 3 has height 6 with one person taller or the same height in front, which is person 1. Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3. Person 5 has height 7 with one person taller or the same height in front, which is person 1. Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] <strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= people.length &lt;= 2000</code></li> <li><code>0 &lt;= h<sub>i</sub> &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k<sub>i</sub> &lt; people.length</code></li> <li>It is guaranteed that the queue can be reconstructed.</li> </ul>
0
{ "code": "#include <algorithm>\n#include <list>\n#include <vector>\nusing namespace std;\n\nclass Solution {\n public:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [&](const auto& p1, const auto& p2) {\n return h_val(p1) * 2000 + k_val(p1) < h_val(p2) * 2000 + k_val(p2);\n });\n\n for (auto it{people.begin()}; it != people.end();) {\n auto p_count{k_val(it)};\n k_val(it) = add_fixed(k_val(it));\n if (p_count) {\n for (auto l_it{people.begin()}; l_it != it; ++l_it)\n if (h_val(it) <= h_val(l_it)) --p_count;\n auto keeper{move(*it)};\n auto it_prev{it};\n auto it_next{it + 1};\n while (p_count) {\n if (is_fixed(k_val(it_next))) {\n if (h_val(keeper) <= h_val(it_next)) --p_count;\n } else {\n --p_count;\n *it_prev = move(*it_next);\n it_prev = it_next;\n }\n ++it_next;\n }\n *it_prev = move(keeper);\n }\n while (it != people.end() && is_fixed(k_val(it))) ++it;\n }\n for (auto&& prsn : people) {\n k_val(prsn) = rem_fixed(k_val(prsn));\n }\n return move(people);\n }\n\n private:\n static inline int& h_val(vector<int>& v) { return v[0]; };\n static inline const int& h_val(const vector<int>& v) { return v[0]; };\n static inline int& h_val(std::vector<std::vector<int>>::iterator& it) {\n return (*it)[0];\n };\n static inline int& k_val(vector<int>& v) { return v[1]; };\n static inline const int& k_val(const vector<int>& v) { return v[1]; };\n static inline int& k_val(std::vector<std::vector<int>>::iterator& it) {\n return (*it)[1];\n };\n constexpr bool is_fixed(int k) { return k & 2048; }\n constexpr int add_fixed(int k) { return k | 2048; }\n constexpr int rem_fixed(int k) { return k ^ 2048; }\n};", "memory": "14500" }
406
<p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p> <p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] <strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] <strong>Explanation:</strong> Person 0 has height 5 with no other people taller or the same height in front. Person 1 has height 7 with no other people taller or the same height in front. Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1. Person 3 has height 6 with one person taller or the same height in front, which is person 1. Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3. Person 5 has height 7 with one person taller or the same height in front, which is person 1. Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] <strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= people.length &lt;= 2000</code></li> <li><code>0 &lt;= h<sub>i</sub> &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k<sub>i</sub> &lt; people.length</code></li> <li>It is guaranteed that the queue can be reconstructed.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n std::vector<std::vector<int>> ans(people.size());\n std::vector<std::pair<int16_t, int16_t>> lst(people.size(), std::pair{-1, 1});\n\n std::sort(people.begin(), people.end());\n\n int height = -1, head = 0, tail = people.size(), cur = 0, i = 0;\n for (auto& p: people) {\n if (p[0] > height) {\n height = p[0];\n cur = 0;\n i = head;\n }\n\n int k = p[1];\n while (k > cur) {\n ++cur;\n i += lst[i].second;\n }\n\n p.swap(ans[i]);\n\n if (i > head)\n lst[i + lst[i].first].second += lst[i].second;\n else\n head += lst[i].second;\n\n if (i + lst[i].second < tail)\n lst[i + lst[i].second].first += lst[i].first;\n }\n\n return ans;\n }\n};", "memory": "14700" }
406
<p>You are given an array of people, <code>people</code>, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with <strong>exactly</strong> <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.</p> <p>Reconstruct and return <em>the queue that is represented by the input array </em><code>people</code>. The returned queue should be formatted as an array <code>queue</code>, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (<code>queue[0]</code> is the person at the front of the queue).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]] <strong>Output:</strong> [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] <strong>Explanation:</strong> Person 0 has height 5 with no other people taller or the same height in front. Person 1 has height 7 with no other people taller or the same height in front. Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1. Person 3 has height 6 with one person taller or the same height in front, which is person 1. Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3. Person 5 has height 7 with one person taller or the same height in front, which is person 1. Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]] <strong>Output:</strong> [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= people.length &lt;= 2000</code></li> <li><code>0 &lt;= h<sub>i</sub> &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= k<sub>i</sub> &lt; people.length</code></li> <li>It is guaranteed that the queue can be reconstructed.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {\n sort(people.begin(), people.end(), [](auto& a, auto& b) {\n if (a[0] != b[0]) {\n return a[0] < b[0];\n }\n return a[1] > b[1];\n });\n vector<vector<int>> ans(people.size());\n int n = people.size();\n for (auto& p : people) {\n for (int cnt = 0, j = 0; j < n; ++j) {\n if (!ans[j].empty()) {\n continue;\n }\n if (cnt == p[1]) {\n // printf(\"[%d,%d] : %d\\n\", p[0], p[1], j);\n ans[j] = move(p);\n break;\n }\n ++cnt;\n }\n }\n return ans;\n }\n};", "memory": "14800" }