id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\nprivate:\n int solve(int n, vector<vector<bool>> places, int idx) {\n if(idx == n) return 1;\n int ans = 0;\n for(int i = 0; i < n; i++) {\n if(places[idx][i]) {\n vector<vector<bool>> temp = places;\n for(int J = idx; J < n...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\nvoid recur(int row, int n, vector<vector<int>> vis, int &result) {\n if (row == n) { \n result++;\n return;\n }\n\n for (int col = 0; col < n; col++) {\n if (!vis[row][col]) {\n vector<vector<int>> temp = vis;\n vis[row][col] = tr...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\nbool safe(vector<vector<char>>&board,int i,int j,int n){\n if(i==0)return true;\n vector<pair<int,int>>vec;\n for(int k=0;k<i;k++){\n for(int l=0;l<n;l++){\n if(board[k][l]=='Q')\n vec.push_back({k,l});\n }\n }\n for(auto x:vec){...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n int res=0;\n bool ischeck( vector<pair<int,int>> track, int r, int c){\n for(int i=0; i<track.size(); i++){\n if(track[i].first ==r || track[i].second == c) return false;\n if(abs(track[i].first-r) == abs(track[i].second-c)) return false;\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n bool isValidPos(int i,int j, vector<vector<char>> board){\n int rr = i-1 , cc = j-1;\n // up left diagonal ....\n while(rr >=0 && cc >=0){\n if(board[rr][cc] == 'Q') return false;\n rr-- , cc-- ;\n }\n // up right diago...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "void traverse(int row, int n, vector<int> col, vector<vector<int>> valid, int &ans)\n{\n if(row == n)\n {\n ans++;\n }\n else\n {\n for(int j = 0; j < n; j++)\n {\n if(col[j])\n {\n if(valid[row][j])\n {\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\nprivate:\n int trueN;\n int result = 0;\n void backtracking(int row, unordered_map<int, bool> badCol, unordered_map<int, bool> posDiag, unordered_map<int, bool> negDiag) {\n if (row == trueN) {\n result++;\n return;\n }\n\n for (int i = ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n void solve(int r, vector<string>board, unordered_set<int>cols, unordered_set<int>mdiag, unordered_set<int>adiag, vector<vector<string>> &ans, int n){\n if(r>=n){\n ans.push_back(board); \n return;\n }\n for(int i=0; i<n; i++){\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\nprivate:\n int trueN;\n int result = 0;\n void backtracking(int row, unordered_map<int, bool> badCol, unordered_map<int, bool> posDiag, unordered_map<int, bool> negDiag) {\n if (row == trueN) {\n result++;\n }\n\n for (int i = 0; i < trueN; i++) {\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n int cnt;\n bool isValid(vector<string> &grid, int n,int row,int col) {\n vector<pair<int,int>> vp;\n for(int i=0;i<row;i++) {\n for(int j=0;j<n;j++) {\n if(grid[i][j]=='Q') vp.push_back({i,j});\n }\n }\n if(row==0) return true;\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n void Try(int i, int n, vector<int> cot, vector<int> d1, vector<int> d2,vector<int> x, int& count) {\n for (int j = 1; j <= n; j++) {\n if (cot[j] == 1 && d1[i - j + n] == 1 && d2[i + j - 1] == 1) {\n x[i] = j;\n cot[j] = d1[i - ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\nvoid rec(vector<vector<string>>& res,int i,int n,vector<string>& tmp,vector<vector<int>>& pos){\n if(i>=n){\n res.push_back(tmp);\n // cout<<\"asda\"<<endl;\n return;\n }\n \n for(int j=0;j<n;++j){\n int check=1;\n for(auto it:pos){\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n int count = 0;\n void mark(int i,int j,vector<vector<int>> &vis){\n for(int k=0;k<vis.size();k++){\n vis[k][j] = 1;\n }\n for(int k=0;k<vis[0].size();k++){\n vis[i][k] = 1;\n }\n int row = i;\n int col = j;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n int count = 0;\n void mark(int i,int j,vector<vector<int>> &vis){\n for(int k=0;k<vis.size();k++){\n vis[k][j] = 1;\n }\n for(int k=0;k<vis[0].size();k++){\n vis[i][k] = 1;\n }\n int row = i;\n int col = j;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n int count = 0;\n void mark(int i,int j,vector<vector<int>> &vis){\n for(int k=0;k<vis.size();k++){\n vis[k][j] = 1;\n }\n for(int k=0;k<vis[0].size();k++){\n vis[i][k] = 1;\n }\n int row = i;\n int col = j;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n int count = 0;\n void mark(int i,int j,vector<vector<int>> &vis){\n for(int k=0;k<vis.size();k++){\n vis[k][j] = 1;\n }\n for(int k=0;k<vis[0].size();k++){\n vis[i][k] = 1;\n }\n int row = i;\n int col = j;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n\n int count = 0;\n void mark(int i,int j,vector<vector<int>> &vis){\n for(int k=0;k<vis.size();k++){\n vis[k][j] = 1;\n }\n for(int k=0;k<vis[0].size();k++){\n vis[i][k] = 1;\n }\n int row = i;\n int col = j;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\nprivate:\n int size;\n int sol = 0;\n unordered_map<string,int> ma;\n // Making use of a helper function to get the\n // solutions in the correct output format\n string createBoard(vector<vector<char>> state) {\n string board=\"\";\n for (int row = 0; row <...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n vector<vector<string>> ans;\n string temp;\n\n void dfs(vector<string> now, vector<vector<bool>> vis, int z, int n){\n if(z==n){ans.push_back(now);return;}\n for(int i=0;i<n;i++){\n if(vis[z][i])continue;\n string s = temp;\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\nvoid nqueens(int i,int n,vector<vector<bool>>visited,vector<string>s,vector<vector<string>>&ans){\n if(i==n){\n ans.push_back(s);\n return;\n }\n for(int j=0;j<n;j++){\n if(visited[i][j]==0){\n vector<vector<boo...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "typedef struct node{\n pair<int,int> pos;//last node\n vector<vector<bool>> map;//for restore the state\n}node;\n\n\nclass Solution {\npublic:\n int pos[8][2]={{1,0},{1,-1},{1,1},{0,1},{0,-1},{-1,0},{-1,-1},{-1,1}};\n \n void put_queen( vector<vector<bool>> &map, int pos_x , int pos_y ){//en...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n bool isSafe(int n,int row, int col, vector<int>colp, vector<int>ldia, vector<int>rdia){\n if(colp[col] || ldia[n+row-col] || rdia[row+col]) return false;\n else return true;\n }\n int helper(int n, int row,vector<int>&colp, vector<int>&ldia, vector<int>&rd...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n void backtracking (int l,int n,vector<string> at,vector<vector<bool>> hash,set<vector<string>>&out) {\n for (int j=0;j<n;j++) {\n if(hash[l][j]==false) {\n vector<vector<bool>> check=hash;\n vector<string> nat=at;\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n struct State {\n int c_i = 0;\n int c_j = 0;\n int queen = 0;\n vector<vector<bool>> can_attack;\n\n explicit State(int n) {\n can_attack = vector<vector<bool>>(n, vector<bool>(n, false));\n }\n };\n int totalNQueens(...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n map<int,int> mp;\n int totalNQueens(int n) {\n vector<vector<char>> board(n,vector<char>(n,'.'));\n vector<int> dp(n,0);\n m(0,board,dp);\n return mp[0];\n\n \n \n }\n void m(int i ,vector<vector<char>> board,vector<int>& ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n bool isValidPos(int i,int j, vector<vector<int>> vec){\n int rr = i , cc = j;\n while(rr>=0){\n if(vec[rr][j] == 1) return false;\n rr--;\n }\n rr = i;\n while(rr >=0 && cc >=0){\n if(vec[rr][cc] == 1) return...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n bool isValidPos(int i,int j, vector<vector<int>> vec){\n int rr = i , cc = j;\n while(rr>=0){\n if(vec[rr--][j] == 1) return false;\n }\n rr = i;\n while(rr >=0 && cc >=0){\n if(vec[rr--][cc--] == 1) return false;\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n int totalNQueens(int n) {\n vector<vector<vector<int>>> solutions;\n set<int> used_cols;\n set<int> used_diagonals;\n set<int> used_diagonals_2;\n vector<vector<int>> starter(n, vector<int>(n));\n helper(starter, 0, used_cols, used_di...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n void mark(vector<vector<bool>> &marker,int i,int j,int n){\n for(int k=0;k<n;k++){\n marker[i][k]=false;\n marker[k][j]=false;\n }\n int tempi=i,tempj=j;\n while(tempi>=0 && tempj>=0){\n marker[tempi][tempj]=false;\...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n bool isSafe(int i, int j, int n, vector<int> row, vector<int> upperDiag, vector<int> lowerDiag){\n if(row[i]) return false;\n if(upperDiag[n - 1 + j - i]) return false;\n if(lowerDiag[i + j]) return false;\n return true;\n }\n int f(int col, ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\n bool possible(vector<string> cur, int i, int n){\n int straight = i;\n int left = i-1;\n int right = i+1;\n for(int j=cur.size()-1; j>=0; j--){\n if(cur[j][straight] == 'Q') return 0;\n if(left>=0){\n if(cur[j][left] == ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\nprivate:\n int ans;\n\n bool validQueenPosition(int row, int n, const vector<string>& board, int x, int y) {\n if (board[x][y] != 'Q') {\n return false;\n }\n\n // check in the row that there are no more queens\n int queens = 0;\n for (i...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n // Check karte hai agar current row aur column par Queen place karna safe hai ya nahi\n bool isSafe(int row,int col, vector<string> board ,int n){\n\n int diaRow=row;\n int diaCol=col;\n\n // Upper left diagonal check karte hai\n while(row>=0 &&...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n vector<vector<string>>ans;\n bool validate(int r,int c, vector<string>mp)\n {\n int a=r;\n int b=c;\n int n=mp.size();\n for(int i=0;i<n;i++)\n {\n if(mp[i][b]=='Q')\n {\n return false;\n }\n if(mp[a][i]=='Q')\n {\n ...
52
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
3
{ "code": "class Solution {\npublic:\n int n, res = 0;\n\t// Check whether placing a new queen at given point is safe or not\n bool isSafe(vector<string> arr, int x, int y){\n for (int row=0; row<x; row++){\n if (arr[row][y]=='Q'){\n return false;\n }\n }\n ...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n\n vector<int> ans;\n int row = matrix.size();\n int col = matrix[0].size();\n int count = 0;\n int total =row*col;\n int startingRow = 0;\n int startingCol = 0;\n ...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n int top=0;\n int left=0;\n int right=matrix[0].size()-1;\n int bottom=matrix.size()-1;\n vector<int>ans;\n while(top<=bottom && left<=right)\n {\n for(int i=le...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n vector<int> result;\n\n int top = 0, bottom = matrix.size()-1;\n int left = 0, right = matrix[0].size()-1;\n\n while(top<=bottom && left<=right){\n for(int i = left; i<=right; i++)...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n int row=matrix.size();\n int col=matrix[0].size();\n int count=0;\n int starting_row=0;\n int starting_col=0;\n int ending_row=row-1;\n int ending_col=col-1;\n vec...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n int m=matrix.size();\n int n=matrix[0].size();\n int left=0,up=0,bottom=m-1,right=n-1;\n vector<int>res;\n while(left<=right && up<=bottom){\n for(int i=left;i<=right;i++)\n ...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n vector<int>res;\n int n = matrix.size();\n int m = matrix[0].size();\n int count =0;\n int total = n*m;\n int sr = 0;\n int sc= 0;\n int er = n-1;\n int ec ...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n int y=0,x=0,op=0,my=matrix.size(),mx=matrix[0].size();\n vector<int> ans;\n while(ans.size()<my*mx){\n if(matrix[y][x]!=-1000)ans.push_back(matrix[y][x]);\n \n matri...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
0
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n vector<int> ans;\n int m=matrix.size(), n=matrix[0].size(), rs=0, cs=0, re=m-1, ce=n-1, k=m*n;\n\n while(k>0) {\n // left\n int c=cs;\n\n while(k>0 && c<=ce) {\n ...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
1
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n\n vector<int> ans;\n \n int rowsize = matrix.size();\n int colsize = matrix[0].size();\n\n int top = 0;\n int left = 0;\n int right = colsize-1;\n int bottom = row...
54
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
1
{ "code": "class Solution {\npublic:\n vector<int> spiralOrder(vector<vector<int>>& matrix) {\n vector<int> ans;\n\n int n=matrix.size();//rows\n int m=matrix[0].size();//columns\n\n int top=0;\n int bottom=n-1;\n int left=0;\n int right=m-1;\n\n while(left<...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "int speedUp = []\n{std::ios::sync_with_stdio(0); std::cin.tie(0); return 0; }();\n\nint digit(char c)\n{\n return c & 15;\n}\nbool isDigit(char c)\n{\n return '0' <= c && c <= '9';\n}\nint init = []\n{\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "int speedUp = []\n{std::ios::sync_with_stdio(0); std::cin.tie(0); return 0; }();\n\nint digit(char c)\n{\n return c & 15;\n}\nbool isDigit(char c)\n{\n return '0' <= c && c <= '9';\n}\nint init = []\n{\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "int speedUp = []\n{std::ios::sync_with_stdio(0); std::cin.tie(0); return 0; }();\n\nint digit(char c)\n{\n return c & 15;\n}\nbool isDigit(char c)\n{\n return '0' <= c && c <= '9';\n}\nint init = []\n{\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "int speedUp = []\n{std::ios::sync_with_stdio(0); std::cin.tie(0); return 0; }();\n\nint digit(char c)\n{\n return c & 15;\n}\nbool isDigit(char c)\n{\n return '0' <= c && c <= '9';\n}\nint init = []\n{\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "int speedUp = []\n{std::ios::sync_with_stdio(0); std::cin.tie(0); return 0; }();\n\nint digit(char c)\n{\n return c & 15;\n}\nbool isDigit(char c)\n{\n return '0' <= c && c <= '9';\n}\nint init = []\n{\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n //Cách làm: Lùi Goal về các pos đằng trc \n //VD: [2,3,1,1,4(G)]\n //<-\n // [2,3,1,1(G),4(G)] vì thấy 1 có thể đi đến đc 4\n // [2,3,1(G),1(G), 4(G)] vì thấy 1 có thể đi đến tk 1 đá...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int reachIndex = 0;\n for(int i = 0; i < nums.size(); i++){\n if(i > reachIndex){\n return false;\n }\n if(nums[i] + i >= reachIndex){\n reachIndex = nums[i] + i;\...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n if (nums.size() < 2) {\n return true;\n }\n int goal = nums.size() - 1;\n for(int i=goal;i>=0;i--){\n if (i + nums[i] >= goal) {\n goal = i;\n }\n }\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n if(nums.size()<=1) return true;\n int last = nums.size() - 1;\n for (int i = last; i >= 0; i--) {\n if (i + nums[i] >= last) {\n last = i;\n }\n }\n return last == 0;\n...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
0
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n \n if (nums.size() < 2) {\n return true;\n }\n int goal = nums.size() - 1;\n for (int i = goal; i >= 0; i--){\n if (i + nums[i] >=goal) {\n goal = i;\n }\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n queue<int> q;\n for (int i = 0; i < n; i++) {\n if (nums[i] == 0) q.push(i);\n }\n if (q.empty()) \n return true;\n \n for (int i = 0; i < n; i++)...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n queue<int> q;\n for (int i = 0; i < n; i++) {\n if (nums[i] == 0) q.push(i);\n }\n if (q.empty()) \n return true;\n \n for (int i = 0; i < n; i++)...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n queue<int> q;\n for (int i = 0; i < n; i++) {\n if (nums[i] == 0) q.push(i);\n }\n if (q.empty()) \n return true;\n \n for (int i = 0; i < n; i++)...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n queue<int> q;\n for (int i = 0; i < n; i++) {\n if (nums[i] == 0) q.push(i);\n }\n if (q.empty()) \n return true;\n \n for (int i = 0; i < n; i++)...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n vector<bool> vec(n, false);\n vec[0] = true;\n for(int i=0; i<n; i++) {\n int val = vec[i] ? nums[i] : 0;\n if (vec[i] && val+i >= n) return true;\n int j =...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n vector<bool> reachable(nums.size());\n int highest = 0;\n int target = nums.size() - 1;\n\n reachable[0] = true;\n for (int i = 0; i < nums.size() - 1; i++) {\n if (i + nums[i] > highest && reac...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n vector<bool> reachable(nums.size());\n int highest = 0;\n int target = nums.size() - 1;\n\n reachable[0] = true;\n for (int i = 0; i < nums.size() - 1; i++) {\n if (i + nums[i] > highest && reac...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n=nums.size();\n vector<bool> vis(n, false);\n vis[0]=true;\n for(int i=0;i<n;i++) {\n if(!vis[i])\n return false;\n for(int j=1;j<=nums[i];j++) {\n if(i+j<...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int len_nums = nums.size();\n vector<bool> reach;\n reach.resize(nums.size());\n for (int i=0;i<len_nums;i++)\n reach[i]=false;\n\n reach[0]=true;\n //for (int i=0;i<len_nums;i++)\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n//greeedy algorithm is very different from recursion and it also can be done using recursion and memoization. In this question. I cannot know whether or not i can use the recursion or greedy over here. \n//FIRST Let me code the normal recursion here. using memoization.\n int d...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n\nint t[10001];\nbool solver(vector<int>&nums, int n, int idx){\n\n if(idx==n-1){\n return true;\n }\n\n if(t[idx] != -1){\n return t[idx];\n }\n\n for(int i=1; i<=nums[idx]; i++){\n\n if(solver(nums, n, idx+i)==true){\n return true;...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n const int N = nums.size();\n bool dp[10000] = {false};\n dp[N-1] = true;\n\n for (int i = N-2; i >= 0; --i) {\n int maxJumps = nums[i];\n while (maxJumps > 0) {\n if (i + maxJ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n int t[10001];\n\n bool solve(vector<int>& nums, int ind, int n){\n if(ind == n-1) return true;\n \n if(t[ind] != -1) return t[ind];\n\n for(int i=1 ; i<=nums[ind] ; i++){\n if(solve(nums, ind+i, n) == true) return t[ind] = true;\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\n\nint dp[10001];\n\nprivate:\n bool dfs(int i, vector<int>& nums) {\n if (i >= nums.size()) { return false; }\n if (i == nums.size() - 1) {\n return true;\n }\n if (nums[i] == 0) { return false; }\n if (dp[i] != -1) { return dp[i]; }\n\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n int dp[10000];\n bool solve(vector<int>& nums, int pos){\n if(pos == nums.size()-1) return true;\n if(dp[pos] != -1) return dp[pos];\n for(int i=1; i<=nums[pos]; i++){\n if(pos+i < nums.size()) if(solve(nums, pos+i)) return dp[pos] = true;\n...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n const int N = nums.size();\n bool dp[10000] = {false};\n dp[N-1] = true;\n\n for (int i = N-2; i >= 0; --i) {\n int maxJumps = nums[i];\n while (maxJumps > 0) {\n if (i + maxJ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\nint dp[100000];\nbool solve(vector<int>& nums,int start){\n if(start>=nums.size()-1){\n return dp[start] = true ;\n }\n if(nums[start]==0){\n return dp[start] = false ;\n }\n if(dp[start]!=-1){\n return dp[start];\n }\n \n for(int i ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n int dp[100000];\n bool rec(int l,vector<int> &nums)\n {\n if(l>=nums.size()-1)return 1;\n if(dp[l]!=-1)return dp[l];\n for(int i=1;i<=nums[l];i++)\n {\n int ans = rec(l+i,nums);\n if(ans)return dp[l]=ans;\n }\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int size = nums.size();\n if(nums[0] >= size-1){\n return true;\n }\n\n\n vector<int> memo(size,-1);\n memo[size-1] = true;\n for(int i = size-2; i > -1; i--){\n for(int j = i;...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\nstatic const int N=1e5;\nlong long dp[N];\nbool hmm(vector<int>& pew,int index)\n{\n if(index<=0)\n {\n return 1;\n }\n if(dp[index]!=-1)\n {\n return dp[index];\n }\n for(int i=index-1;i>-1;i--)\n {\n if(pew[i]+i>=index)\n {\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n vector<bool> colors(nums.size(), false);\n if (nums.front() >= nums.size() - 1) {\n return true;\n }\n stack<int> indexes;\n indexes.push(0);\n while (!indexes.empty()) {\n int current_index = indexes.top...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n int* dp = new int[n]; \n int reachable = 0;\n for(int i = 0; i < n ; i++){\n if(reachable < i) return false;\n dp[i] = i + nums[i];\n reachable = max(dp[i] ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "enum Index { GOOD, BAD, UNKNOWN };\nclass Solution {\npublic:\n bool canJump(vector<int>& nums) {\n vector<Index> memo(nums.size(), UNKNOWN);\n memo[memo.size() - 1] = GOOD;\n for (int i = nums.size() - 2; i >= 0; i--) {\n int furthestJump = min(i + nums[i], (int)nums.siz...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size(); \n vector<int> dp(n,INT_MAX); \n if(n==1){\n return true; \n }\n if(nums[0]==0){\n return false; \n }\n dp[0] = 0; \n\n for(int i=1; i<n; i++...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n, 0);\n\n dp[n - 1] = 1;\n\n for (int i = n - 2; i >= 0; --i) {\n for (int jump = 1; jump <= nums[i]; ++jump) {\n int nextPosition = i + jump;\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int>& nums,int ind,int n,vector<bool>& dp)\n {\n if(ind==n-1) return dp[ind]= true;\n if(ind>=n) return false;\n\n if(dp[ind]) return true;\n\n int mxJump = nums[ind];\n for(int i=1;i<=mxJump;i++)\n {\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n vector<int> dp(n, 0);\n dp[0] = 1;\n for (int i = 0; i < n; i++)\n {\n if (dp[i])\n {\n for (int j = i + 1; j <= i + nums[i] && j < n; j++)\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int len=nums.size();\n vector<unsigned int> dp(len,0);\n if(nums.size()==1)return 1;\n if(!nums[0])return 0;\n dp[0]=1;\n for(int i=0;i<len;i++){\n for(int j=1;j<=nums[i]&&i+j<len;j++){\n...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int len=nums.size();\n vector<unsigned int> dp(len,0);\n if(nums.size()==1)return 1;\n if(!nums[0])return 0;\n dp[0]=1;\n for(int i=0;i<len;i++){\n for(int j=1;j<=nums[i]&&i+j<len;j++){\n...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n \n \n bool canJump(vector<int>& nums) {\n int n = nums.size();\n \n \n vector<int> t(n, false);\n //t[i] = True means, you can reach index i\n \n t[0] = true; //Already at starting index\n \n for(int i = ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n int final_answer=false;\n bool canJump(vector<int>& nums) {\n int n=nums.size();\n vector<int>dp(n,-1); \n \n dp[n-1]=1;\n \n for(int ind=n-2;ind>=0;ind--) {\n bool ans=false;\n for(int i=1;i<=nums[ind];i++) {...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool util(vector<int> &nums, int ind, vector<int>& dp)\n {\n if (ind >= nums.size()-1)\n {\n return true;\n }\n\n if (dp[ind] != -1) return dp[ind];\n bool ans = false;\n for (int i=1; i<= nums[ind]; i++)\n {\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n // This is a classic recursive question:\n // Suppose that at position k we have the number m.\n // So the question is whether we can reach the last index from position k+1, up to k+m.\n // If we receive a positive answer then we can abort the recursion.\n // In o...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool canJump(vector<int>& nums) {\n int n = nums.size();\n vector<bool> dp(n - 1, false);\n return rf(nums, 0, dp, n);\n }\n bool rf(vector<int>& nums, int i, vector<bool>& dp, int n) {\n bool flag = false;\n int limit = min(i + nums[i...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n\n template<typename T>\n void printVec(vector<T>& nums)\n {\n for(auto e : nums)\n {\n std::cout << std::to_string(e) << \", \";\n }\n std::cout << std::endl;\n }\n\n bool canJump(vector<int>& nums) \n {\n if(nums.s...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n bool find(int i, vector<int>&nums, vector<int>&vis)\n {\n if(i>=nums.size())return false;\n if(i==nums.size()-1)return true;\n if(vis[i]!=-1)return vis[i];\n bool ans = false;\n for(int k=nums[i]; k>=1; k--)\n {\n if(fin...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n vector<int>dp;\n bool solve(int ind,vector<int>&nums){\n if(ind>=nums.size()-1){\n return true;\n }\n if(dp[ind]!=-1) return dp[ind];\n for(int i=1;i<=nums[ind];i++){\n if(solve(ind+i,nums)){\n return dp[ind]...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\npublic:\n \n bool solve(vector<int>&nums,int start,vector<int>&dp){\n int n = nums.size();\n if(start == n-1){\n return true;\n }\n if(start>=n){\n return false;\n }\n\n if(dp[start]!=-1){\n return dp[start];...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "class Solution {\n bool rec(int ind,vector<int>& nums,vector<int>&dp){\n int n=nums.size();\n if(ind==0){\n return true;\n }\n if(dp[ind]!=-1){\n return dp[ind];\n }\n for(int i=ind-1;i>=0;i--){\n if(i+nums[i]>=ind){\n ...
55
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
3
{ "code": "// You are given an integer array nums. You are initially positioned at the arrays first index, and each element in the array represents your maximum jump length at that position.\n\n// Return true if you can reach the last index, or false otherwise.\n// nums = [2,3,1,1,4]\n// take 2 then jump two indi...
56
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
0
{ "code": "class Solution final {\npublic:\n constexpr vector<vector<int>> merge(\n vector<vector<int>> &intervals\n ) const noexcept {\n const auto last{intervals.end()};\n auto left{intervals.begin()};\n sort(left, last);\n\n for (auto right{next(left)}; right != last; ++rig...
56
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
0
{ "code": "class Solution final {\nprivate:\n template<class C>\n static constexpr bool intersects(const C &first, const C &second) noexcept {\n return max(first[0], second[0]) <= min(first[1], second[1]);\n }\n\n template<class C>\n static constexpr void merge(\n const C &first,\n ...
56
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
0
{ "code": "class Solution final {\nprivate:\n template<class C>\n static constexpr bool intersects(const C &first, const C &second) noexcept {\n return max(first[0], second[0]) <= min(first[1], second[1]);\n }\n\n template<class C>\n static constexpr void merge(C &first, const C &second) noexcep...
56
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
0
{ "code": "\nbool merge_into(const std::vector<int>& source, std::vector<int>& target) {\n if (source[0] > target[1]) return false;\n if (source[1] < target[0]) return false;\n\n target[0] = std::min(target[0], source[0]);\n target[1] = std::max(target[1], source[1]);\n return true;\n}\n\nclass Solutio...
56
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
0
{ "code": "\nbool merge_into(const std::vector<int>& source, std::vector<int>& target) {\n if (source[0] > target[1]) return false;\n\n target[1] = std::max(target[1], source[1]);\n return true;\n}\n\nclass Solution {\npublic:\n std::vector<std::vector<int>> merge(std::vector<std::vector<int>>& intervals)...