id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n \n int c1 = count(word.begin(), word.end(), word[0]);\n int c2 = count(word.begin(), word.end(), word.back());\n\n if (c1 > c2) {\n reverse(word.begin(), word.end());\n }\n\...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n \n int c1 = count(word.begin(), word.end(), word[0]);\n int c2 = count(word.begin(), word.end(), word.back());\n\n if (c1 > c2) {\n reverse(word.begin(), word.end());\n }\n\...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n if (word.size() > board.size() * board.front().size()) return false;\n\n for (auto c : word) {\n bool found_in_board = false;\n for (const auto& row : board) {\n for (c...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n int ROWS = board.size();\n int COLS = board[0].size();\n bool res = false;\n set<pair<int,int>> path;\n function <bool(int, int, int)> backtrack = [&] (int wordIdx, int row, int col) {...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool solve(vector<vector<char>>& board,int row,int col,int index,set<pair<int,int>> &visited,string &word){\n if(row<0 || col<0 || row>=board.size() || col>=board[0].size() || board[row][col]!=word[index] || visited.find(make_pair(row,col))!=visited.end())return false;\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(\n const vector<vector<char>> &board,\n const string &word,\n set<pair<int, int>> &visited,\n int string_pos,\n int x, int y\n ) {\n if (x < 0 || x >= board.size()) return false;\n if (y < 0 || y >= board[x].size()) return fals...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\nprivate:\n unordered_map<char, vector<pair<int, int>>> char_pos;\n\n void init(const vector<vector<char>>& board) {\n for (int i = 0; i < board.size(); ++i)\n for (int j = 0; j < board[i].size(); ++j)\n char_pos[board[i][j]].push_back({i, j});\n }...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n struct Position {\n Position(int x, int y)\n : x(x), y(y) {}\n\n bool operator==(const Position& other) {\n return this->x == other.x && this->y == other.y;\n }\n\n bool operator!=(const Position& other) {\n return this...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\n int m_m{}, m_n{};\n using pair_t = std::pair<int, int>;\n\n bool is_valid (int i, int j) { return i >= 0 && i < m_m && j >= 0 && j < m_n; }\n\npublic:\n bool check_recursively (pair_t coords, string &current, vector<vector<char>>& board, vector<bool> &visited) {\n if (...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\n int m_m{}, m_n{};\n using pair_t = std::pair<int, int>;\n\n bool is_valid (int i, int j) { return i >= 0 && i < m_m && j >= 0 && j < m_n; }\n\npublic:\n bool check_recursively (pair_t coords, string &current, vector<vector<char>>& board, vector<vector<bool>> &visited) {\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n if (board[0].empty()) return false;\n\n const int m = board.size();\n const int n = board[0].size();\n unordered_map<char, int> freq;\n\n // for (int i = 0; i < m; i++) {\n // ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "// loop through half of the word, if at any point, the current char appears more frequently than the char on the opposite side of the word, reverse it\n// catch obvious cases: if the length of the word is larger than the number of chars on the board, or if the frequency of any character in the word is less...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n int m;\n int n;\n bool exist(vector<vector<char>>& board, string word) {\n m = board.size();\n n = board[0].size();\n\n unordered_map<char, int> freq;\n for(int row=0; row < m; row++){\n for(int col=0; col < n; col++){\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n\n const vector<pair<int,int>> dirs = {{-1,0}, {1,0}, {0,-1}, {0,1}};\n\n bool ok(vector<vector<char>>& board, pair<int,int>& pos)\n {\n int m = board.size();\n int n = board[0].size();\n return (pos.first >= 0 && pos.first < m && pos.second >= 0 && ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n /*\n Idea: start with all letters on board that match the first character.\n From there on, try to match word.\n We need to track the current seleted letters as we can't use them twice.\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\nprivate: \n void check(int idx, vector<vector<int>> dp, int row, int rowsize, int col, int colsize, vector<vector<bool>>& visited, bool& ret){\n if(idx < 1 || ret){\n ret = true;\n return;\n }\n\n int curr = col + 1 < colsize ? dp[idx-1]...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\nprivate: \n void check(int idx, vector<vector<int>> dp, int row, int rowsize, int col, int colsize, vector<vector<bool>>& visited, bool& ret){\n if(idx < 1 || ret){\n ret = true;\n return;\n }\n\n int curr = col + 1 < colsize ? dp[idx-1]...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word)\n {\n std::vector<std::vector<int>> table(board.size(), std::vector<int>(board[0].size(), 1));\n std::stack<std::tuple<int, int, bool>> sta;\n int idx = 0;\n for (int y = 0; y<board.size(); ++...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word)\n {\n std::vector<std::vector<int>> table(board.size(), std::vector<int>(board[0].size(), 1));\n std::stack<std::tuple<int, int, bool>> sta;\n int idx = 0;\n for (int y = 0; y<board.size(); ++...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word)\n {\n std::vector<std::vector<int>> table(board.size(), std::vector<int>(board[0].size(), 1));\n std::stack<std::tuple<int, int, bool>> sta;\n int idx = 0;\n for (int y = 0; y<board.size(); ++...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word)\n {\n std::vector<std::vector<int>> table(board.size(), std::vector<int>(board[0].size(), 1));\n std::stack<std::tuple<int, int, bool>> sta;\n int idx = 0;\n for (int y = 0; y<board.size(); ++...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word)\n {\n std::vector<std::vector<int>> table(board.size(), std::vector<int>(board[0].size(), 1));\n std::stack<std::tuple<int, int, bool>> sta;\n int idx = 0;\n for (int y = 0; y<board.size(); ++...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n size_t rows = board.size();\n size_t cols = board.front().size();\n std::reverse(word.begin(), word.end());\n char start = word.front();\n for (size_t i = 0; i < rows; ++i) {\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n size_t rows = board.size();\n size_t cols = board.front().size();\n std::reverse(word.begin(), word.end());\n char start = word.front();\n std::set<std::pair<size_t, size_t>> used{};\n...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n\n using Pos=pair<int,int>;\n bool inBounds(Pos p, vector<vector<char>>& board)\n {\n return p.first >= 0 && p.first < board.size() && p.second >= 0 && p.second < board[0].size();\n }\n\n bool dfs(set<Pos>& visiting, const Pos& p, vector<vector<char>>& board...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n vector<int>dx={0,0,-1,1};\n vector<int>dy={-1,1,0,0};\n int n,m;\n bool solve(int &i,int &j,vector<vector<char>>& board, string &word,string &s,set<pair<int,int>>&visited){\n if(word==s)return true;//this also prevent us from going out of bound\n if(s.s...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n\n int rows = board.size(), cols = rows? board[0].size(): 0;\n vector<pair<int,int>> dirs{{1,0},{-1,0},{0,1},{0,-1}};\n \n if(word.size() == 0|| !rows)\n return false;\n \n\n...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n \n vector<vector<int>> directions{{0,1},{0,-1},{1,0},{-1,0}};\n bool solve(vector<vector<char>>& board,int n,int m,string &word,int i,int j,int ind,int l){\n \n if(ind>=l)return true;\n \n if(i<0 || i>=n || j<0 || j>=m || board[i][j]!=word[in...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\nprivate:\n \n bool recurseFindWord(vector<vector<char>>& board, string word, vector<vector<bool>>& used, int x, int y){\n if (word.size() == 0){\n return true;\n }\n if (y < 0 || y==board.size() || x<0 || x==board[0].size()){\n return false...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "typedef struct{\n int x;\n int y;\n}move_t;\n\nclass Solution {\npublic:\n\n void backtrack(int x,int y,int k,vector<vector<bool>> &V,vector<vector<char>> &board,string word,bool &esito){\n\n if(esito){\n return;\n }\n\n if(k == word.size()){\n esito = tr...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool isVisited(vector<pair<int, int>> &visited, pair<int, int> p) {\n for (const auto &pt : visited)\n if (pt.first == p.first && pt.second == p.second)\n return true;\n return false;\n }\n vector<pair<int, int>> searchAdj(pair<int, int> s, char t,\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n\n bool calc(vector<vector<char>>& board, string& word, vector<vector<bool>>& state, int i, int j, int l, int& n, int& m, int& wl) {\n if(l==wl)\n return true;\n if(i==n || j==m || i<0 || j<0)\n return false;\n\n if(state[i][j]==true || board...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\n struct T{\n T * ch[60];\n T() {\n for(int i=0; i<60; i++) ch[i] = nullptr;\n }\n };\n bool f[6][6];\n void ff(T * rt, int i, int j, int c, int tl, vector<vector<char>>& board){\n if(c > tl) return;\n f[i][j]=1;\n if((i-1)>=...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "\nstruct Point{\n int x;\n int y;\n Point(int x, int y) : x{x}, y{y} {}\n \n};\n\nbool operator==(const Point& lhs, const Point& rhs) {\n return (lhs.x == rhs.x) && (lhs.y == rhs.y);\n}\nclass Solution {\npublic:\n \n\n bool chain(vector<vector<char>>& board, vector<Point> points, stri...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n void backtracking(vector<vector<char>>& board, string& word, bool& exist,\n unordered_set<char*>& checkSet, int checkIdx, int i,\n int j) {\n if (exist || checkSet.find(&board[i][j]) != checkSet.end())\n return;\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n void backtracking(vector<vector<char>>& board, string& word, bool& exist,\n unordered_set<char*>& checkSet, int checkIdx, int i,\n int j) {\n if (exist || checkSet.find(&board[i][j]) != checkSet.end())\n return;\n ...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n int n = board.size();\n int m = board[0].size();\n \n for (int row = 0; row < n; ++row) {\n for (int col = 0; col < m; ++col) {\n if (depthFirstSearch(board, word, r...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "class Solution {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n int n = board.size();\n int m = board[0].size();\n \n for (int row = 0; row < n; ++row) {\n for (int col = 0; col < m; ++col) {\n if (depthFirstSearch(board, word, r...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "// 我们以二维数组中每一个数都作为起点和给定字符串做匹配,我们还需要一个和原数组等大小的 visited 数组,是 bool 型的,用来记录当前位置是否已经被访问过,因为题目要求一个 cell 只能被访问一次。如果二维数组 board 的当前字符和目标字符串 word 对应的字符相等,则对其上下左右四个邻字符分别调用 DFS 的递归函数,只要有一个返回 true,那么就表示可以找到对应的字符串,否则就不能找到\nclass Solution1 {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n if...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "// 我们以二维数组中每一个数都作为起点和给定字符串做匹配,我们还需要一个和原数组等大小的 visited 数组,是 bool 型的,用来记录当前位置是否已经被访问过,因为题目要求一个 cell 只能被访问一次。如果二维数组 board 的当前字符和目标字符串 word 对应的字符相等,则对其上下左右四个邻字符分别调用 DFS 的递归函数,只要有一个返回 true,那么就表示可以找到对应的字符串,否则就不能找到\nclass Solution1 {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n if...
79
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
3
{ "code": "// 我们以二维数组中每一个数都作为起点和给定字符串做匹配,我们还需要一个和原数组等大小的 visited 数组,是 bool 型的,用来记录当前位置是否已经被访问过,因为题目要求一个 cell 只能被访问一次。如果二维数组 board 的当前字符和目标字符串 word 对应的字符相等,则对其上下左右四个邻字符分别调用 DFS 的递归函数,只要有一个返回 true,那么就表示可以找到对应的字符串,否则就不能找到\nclass Solution1 {\npublic:\n bool exist(vector<vector<char>>& board, string word) {\n if...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int j = 0;\n for(int i = 0; i < nums.size(); ++i){\n if(j < 2 || nums[i] > nums[j-2]){\n nums[j] = nums[i];\n j++;\n }\n }\n return j;\n }\n};", "m...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n void sendBack(vector<int>& nums){\n int n=nums.size();\n int left=0;\n int right=0;\n while(left<n){\n if(nums[left]==-189){\n cout<<\"ent\"<<endl;\n right=left;\n cout<<\"ri: \"<<right<<endl;...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& arr) {\n int n=arr.size();\n if(n<=2){\n return n;\n }\n int j=2;\n for(int i=2;i<n;i++){\n if(arr[i]!=arr[j-2]){\n swap(arr[i],arr[j]);\n j++;\n }\n }\n...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums)\n {\n int j = 0;\n int k = 0;\n char dupl = 0;\n if (nums.size() <= 2)\n {\n k = nums.size();\n }\n else\n {\n dupl == 0;\n for (int i = 1; i < ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int n = nums.size(), j=0, i=0;\n for(int i=0; i<n; i++){\n if(j == 0 || j == 1 || nums[j-2] != nums[i]){\n nums[j] = nums[i];\n j++;\n }\n }\n return j;...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n void printVector(vector<int> nums) {\n for (auto i : nums) {\n std::cout << i;\n }\n std::cout << \"\\n\";\n }\n int removeDuplicates(vector<int>& nums) {\n if (nums.size() <= 2)\n return nums.size();\n\n int j = ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
0
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n if (nums.size() < 2) {\n return nums.size();\n }\n int index = 2;\n for(int i=2;i<nums.size();i++) {\n if (nums[i-1] != nums[i] || nums[index-2] != nums[i]) {\n nums[i...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
1
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n if (nums.empty()) return 0;\n if (nums.size() == 1) return 1;\n\n int index = 2;\n for(int i=2; i<nums.size(); i++){\n if(nums[i] != nums[index-2]){\n nums[index] = nums[i];\n ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
1
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n if (nums.size() <= 2) return nums.size(); // If size is less than or equal to 2, no need to process\n \n int i = 2; // Start from index 2 since we allow the first two elements as they are\n \n fo...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
2
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums)\n {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int i = 1, j = 1, n = nums.size();\n bool first = 1;\n int prev = nums[0];\n int pprev = -10001;\n i...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
2
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int offset = 0;\n if (nums[0]<0) {\n offset = -nums[0];\n }\n vector<int>hash(nums[nums.size()-1]+offset+1,0);\n for (int i = 0; i < nums.size(); i++) {\n hash[nums[i]+offset]...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n vector<int> resultVec;\n int count = 0;\n int currentValue = nums[0]; \n for (int i = 0; i < nums.size(); i ++) {\n if (currentValue == nums[i]) {\n count++;\n } else ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n void swapNElement(vector<int>& nums, int si, int ei) {\n int size = ei - si;\n vector<int> v(size, 0);\n int n = nums.size();\n for (int i = 0; i < size; i++) {\n v.push_back(nums[si + i]);\n }\n for (int j = si; j < n - si...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n vector<int> resultVec;\n int count = 0;\n int currentValue = nums[0]; \n for (int i = 0; i < nums.size(); i ++) {\n if (currentValue == nums[i]) {\n count++;\n } else ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int flag = 0;\n vector<int>temp;\n temp.push_back(nums[0]);\n for(int i=1;i<nums.size();i++)\n {\n if(nums[i] == nums[i-1] && flag == 1)\n {\n continue;\n ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int j=2;\n vector<int> v;\n\n if(nums.size() < 2){\n return nums.size();\n }\n\n for(auto it : nums){\n v.push_back(it);\n }\n for(int i=2 ; i<v.size() ; i++){\n ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n std::vector<int> result;\n std::vector<int> sorted(nums.begin(), nums.end());\n sorted.erase(std::unique(sorted.begin(), sorted.end()), sorted.end());\n for (auto& element : sorted) {\n int cou...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int total = 1;\n unordered_map<int,int> pastNumbers;\n for(int i = 1; i < nums.size(); ++i){\n if(nums[i-1] != nums[i]){\n nums[total] = nums[i];\n ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int count_r = 0;\n int c_size = 0;\n bool condition = true;\n int* count = new int[nums.size()]{};\n vector<int> complete;\n vector<int> output;\n\n for (int i = 0; i < nums.size(); i...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n void check(int n,vector<int>& nums1,vector<int>& nums,int &c)\n {\n for(int i=0;i<nums.size();i++)\n {\n if(n==nums[i])\n {\n c++;\n }\n }\n if((c)>2)\n {\n for(int i=0;i<2;i++)\n...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n void check(int n,vector<int>& nums1,vector<int>& nums,int &c)\n {\n for(int i=0;i<nums.size();i++)\n {\n if(n==nums[i])\n {\n c++;\n }\n }\n if((c)>2)\n {\n for(int i=0;i<2;i++)\n...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n void check(int n,vector<int>& nums1,vector<int>& nums,int &c)\n {\n for(int i=0;i<nums.size();i++)\n {\n if(n==nums[i])\n {\n c++;\n }\n }\n if((c)>2)\n {\n for(int i=0;i<2;i++)\n...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n void check(int n,vector<int>& nums1,vector<int>& nums,int &c)\n {\n for(int i=0;i<nums.size();i++)\n {\n if(n==nums[i])\n {\n c++;\n }\n }\n if((c)>2)\n {\n for(int i=0;i<2;i++)\n...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n map<int,int> mm;\n map<int,int>::iterator it;\n int removeDuplicates(vector<int>& nums) {\n for(int i = 0 ; i < nums.size();)\n {\n cout << nums[i] << \": \";\n if(mm.find(nums[i])!=mm.end()) //find\n {\n if(...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n map<int, int> mp;\n int k = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (mp[nums[i]] < 2) { \n mp[nums[i]]++;\n }\n }\n \n for (auto it = mp.begin(...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n int k=0,n=nums.size();\n map<int,int>mp;\n for(int i=0;i<n;i++)\n {\n mp[nums[i]]++;\n if(mp[nums[i]]<=2)\n {\n nums[k]=nums[i];\n k++;\n ...
80
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
3
{ "code": "class Solution {\npublic:\n int removeDuplicates(vector<int>& nums) {\n map<int, int> mp;\n int k = 0;\n for (int i = 0; i < nums.size(); i++) {\n if (mp[nums[i]] < 2) { \n mp[nums[i]]++;\n }\n }\n \n for (auto it = mp.begin(...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\n static bool compare(const vector<int>& meeting_a, const vector<int>& meeting_b) {\n return meeting_a[2] < meeting_b[2];\n }\n\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n sort(meetings.begin(), meetings.end(), comp...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "bool comp(vector<int>&v1,vector<int>&v2){\n return v1[2]<v2[2];\n}\nvector<int>sz;\nvector<int>parent;\nint par(int x){\n if(parent[x]==x)\n return x;\n return parent[x]=par(parent[x]);\n}\nvoid un(int x,int y){\n int p_x=par(x);\n int p_y=par(y);\n if(p_x==p_y)\n return;\n if(sz...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n vector<int> sets;\n\n int find(int person) {\n if (sets[person] != person) {\n sets[person] = find(sets[person]);\n }\n\n return sets[person];\n }\n\n void merge(int p1, int p2) {\n int root1 = find(p1);\n int root2 = fin...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n vector<int>p;\n vector<bool>v;\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson){\n int N=meetings.size();\n vector<int>ans;\n p.resize(n);\n v.resize(n);\n for(int i=0;i<n;i++){\n p[i]=i;\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "\nclass UnionFind {\n vector<int> id;\npublic:\n UnionFind(int n) : id(n) {\n iota(begin(id), end(id), 0);\n }\n void connect(int a, int b) {\n id[find(b)] = find(a);\n }\n int find(int a) {\n return id[a] == a ? a : (id[a] = find(id[a]));\n }\n bool connected(i...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "// OJ: https://leetcode.com/problems/find-all-people-with-secret/\n// Author: github.com/lzl124631x\n// Time: O(MlogM + (M + N) * logN) where `M` is the length of `meetings`\n// Can be reduced to `O(MlogM + (M + N) * alpha(N))`\n// Space: O(M + N). Can be reduced to O(N) if we make `ppl` an `unorder...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "bool compare(vector<int>&v1,vector<int>&v2){\n \n return v1[2]<v2[2];\n}\nclass Solution {\npublic:\n int find(int i,vector<int>&parent){\n int a=parent[i];\n if(a==i){\n return i;\n }\n return parent[i]=find(a,parent);\n }\n void union2(int a,int b,vect...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class DSU {\npublic:\n vector<int> parent;\n vector<int> rank;\n\n DSU(int n) {\n parent.resize(n, 0);\n rank.resize(n, 0);\n for (int i = 0; i < n; i++)\n parent[i] = i;\n }\n\n int find(int node) {\n return parent[node] =\n (parent[n...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass UnionFind {\n vector<int> root, rank;\n\npublic:\n UnionFind(int n) : root(n), rank(n) {\n rank.assign(n, 1);\n iota(root.begin(), root.end(), 0);\n }\n\n int Find(int x) {\n if (x == root[x])\n return x;\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class DSU {\npublic:\n vector<int> Parent, Rank;\n DSU(int n) {\n Parent.resize(n);\n Rank.resize(n, 0);\n for (int i = 0; i < n; i++) Parent[i] = i;\n }\n int Find(int x) {\n return Parent[x] = (Parent[x] == x ? x : Find(Parent[x]));\n }\n void reset(int node)...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class DSU{\npublic:\n vector<int> par,rank;\n DSU(int n)\n {\n par.resize(n);\n rank.resize(n);\n for(int i=0; i<n; i++) par[i]=i;\n }\n\n int findUpar(int node)\n {\n if(node==par[node]) return node;\n return par[node] = findUpar(par[node]);\n }\n\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n vector<int> parent;\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<int> res;\n parent.resize(n);\n sort(meetings.begin(), meetings.end(),[] (const auto& lhs, const auto& rhs) {\n return lhs[2] <...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class disjointset{\n vector<int>parent,rank;\n public:\n disjointset(int n){\n parent.resize(n);\n for(int i=0;i<n;i++) parent[i]=i;\n rank.resize(n,1);\n }\n int finduparent(int u){\n if(parent[u]==u) return u;\n return parent[u]=finduparent(parent[u]);\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n int par[100005], r[100005];\n void init() {\n for (int i=0; i<=100000; i++) {\n par[i]=i; r[i]=1;\n }\n }\n int fd(int x) {\n if (x==par[x]) return par[x];\n return par[x]=fd(par[x]);\n }\n bool join(int a, int b) {\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n int par[100005], r[100005];\n void init() {\n for (int i=0; i<=100000; i++) {\n par[i]=i; r[i]=1;\n }\n }\n int fd(int x) {\n if (x==par[x]) return par[x];\n return par[x]=fd(par[x]);\n }\n bool join(int a, int b) {\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "struct UnionFind {\n vector<int> parent, rank;\n UnionFind(int n) {\n parent.resize(n), rank.resize(n, 1);\n iota(parent.begin(), parent.end(), 0);\n }\n int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }\n bool isConnected(int x, int y) { return find(...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "struct UnionFind {\n vector<int> parent, rank;\n UnionFind(int n) {\n parent.resize(n), rank.resize(n, 0);\n iota(parent.begin(), parent.end(), 0);\n }\n int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }\n bool isConnected(int x, int y) { return find(...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "// BFS\nclass Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int, int>> adjList[n];\n meetings.push_back({0, firstPerson, 0});\n for(int i = 0; i < meetings.size(); i++) {\n adjList[meetings[i][0]].pu...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int, int>> adjList[n];\n meetings.push_back({0, firstPerson, 0});\n for(int i = 0; i < meetings.size(); i++) {\n adjList[meetings[i][0]].push_back(...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\n vector<int> root, rank;\n int FindRoot(int a) {\n if (a == root[a]) return root[a];\n root[a] = FindRoot(root[a]);\n return root[a];\n }\n void merge(int a, int b) {\n int rootA = FindRoot(root[a]);\n int rootB = FindRoot(root[b]);\...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
0
{ "code": "class Solution {\npublic:\nvoid solve(int f, vector<long long int>& dist, vector<pair<int, int>> g[]) {\n dist[0] = 0;\n dist[f] = 0;\n // time node \n priority_queue<pair<int, int>> pq;\n pq.push({0, 0});\n pq.push({0, f});\n while(!pq.empty()) {\n auto it = pq.top(); pq.pop();...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\npublic:\n\n vector<int> solve(int n, vector<vector<pair<int, int>>>& graph, vector<int>& time, int firstPerson) {\n priority_queue<pair<int, int>> pq;\n pq.push(make_pair(0, 0));\n pq.push(make_pair(0, firstPerson));\n time[0] = 0;\n time[firstPerson]...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "#include <vector>\n#include <queue>\n#include <iostream>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<bool> knowsSecret(n, false);\n vector<vector<pair<int, int>>> adj(n);\n\n for (co...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\nprivate:\n vector<int> parent;\n vector<int> rank;\npublic:\n void make_set(int v) {\n parent[v] = v;\n rank[v] = 1;\n }\n\n int find_set(int v) {\n if (v == parent[v]) {\n return v;\n }\n return parent[v] = find_set(parent[v]);...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\nprivate:\n // DSU Template by jiangly\n struct DSU {\n std::vector<int> f, siz;\n \n DSU() {}\n DSU(int n) {\n init(n);\n }\n \n void init(int n) {\n f.resize(n);\n std::iota(f.begin(), f.end(), 0);\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int,int>> adj[n];\n for(int i=0;i<meetings.size();i++){\n adj[meetings[i][0]].push_back({meetings[i][1],meetings[i][2]});\n adj[meetings[i][1]]...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int, int>> adj[n];\n vector<int> ans;\n meetings.push_back({0, firstPerson, 0});\n for(auto it:meetings){\n adj[it[0]].push_back({it[1], it[...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\npublic:\n \n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int,int>>adj[n];\n \n for(auto it: meetings){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
1
{ "code": "class Solution {\npublic:\n \n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<pair<int,int>>adj[n];\n \n for(auto it: meetings){\n adj[it[0]].push_back({it[1], it[2]});\n adj[it[1]].push_back({it[0], it[2]});\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
2
{ "code": "class Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n vector<vector<pair<int, int>>> adj(n);\n for(auto it:meetings){\n int u = it[0];\n int v = it[1];\n int wt = it[2];\n adj[u].push_bac...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
2
{ "code": "class Solution {\npublic:\n vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {\n queue<pair<int,int>> q;\n vector<int> earliestTime(n, INT_MAX);\n earliestTime[0] = 0;\n earliestTime[firstPerson] = 0;\n vector<vector<pair<int, int>>> adjList(n);\n ...
2,213
<p>You are given an integer <code>n</code> indicating there are <code>n</code> people numbered from <code>0</code> to <code>n - 1</code>. You are also given a <strong>0-indexed</strong> 2D integer array <code>meetings</code> where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates tha...
2
{ "code": "\nbool cmp(vector<int> &a,vector<int> &b){\n return a[2]<b[2];\n}\nclass Solution {\npublic:\n void dfs(int i,int time,vector<int> &visTime,vector<vector<pair<int,int>>> &g){\n for(auto nbr_p:g[i]){\n if(visTime[nbr_p.first]>nbr_p.second and nbr_p.second>=time){\n vis...