id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\n vector<int> H;\n vector<vector<int>> C;\n int M, N, T, INF = 1e9 + 7;\n vector<vector<vector<int>>> memo;\n int dp(int i, int last, int cnt) {\n if (cnt > T) return INF;\n if (i == M) return cnt == T ? 0 : INF;\n if (memo[i][last][cnt] != -1) return me...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\n int helper(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target, int i, int prevColor, vector<vector<vector<int>>> &dp){\n if (i==m){\n if (target==0) return 0;\n else return INT_MAX;\n }\n if (target<0) return INT_MAX;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n int f(int i, int prev, int target, int n, vector<int>& houses, vector<vector<int>>& cost, vector<vector<vector<int>>> &dp){\n if(target<0){\n return 1e8;\n }\n if(i==0){\n if(target==1){\n if(houses[0] != 0){\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>&h, vector<vector<int>>&c, int &m, int &n, int &t, int i, int j, int num, vector<vector<vector<int>>>&dp){\n if(num > t) return 1e9;\n if(i >= h.size()) {\n if(num == t) return 0;\n return 1e9;\n }\n if(...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\n const int MAXCOST = 1e6+1;\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<int>>> dp(m, vector<vector<int>>(n+1,vector<int>(m, -1)));\n int res = recurse(houses, cost, target, 0,0,0, dp);\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\n int solve(vector<int> &houses, vector<vector<int>> &costs, int n, int m, int target, int ind, int prev, vector<vector<vector<int>>> &dp) {\n if(ind == n && target == 1) {\n return 0;\n }\n \n if(ind == n || target <= 0) return 1e9;\n \n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n int solve(int idx,int p,int cnt,vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar,vector<vector<vector<int>>>&dp){\n if(idx==m){\n if(cnt==tar) return 0;\n return 1e9;\n }\n\n if(dp[idx][p][cnt]!=-1) return dp[idx][p...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n int solve(int prev,vector<int>& houses, vector<vector<int>>& cost,int index, int &n, int &m, int target,vector<vector<vector<int>>> &dp){\n if(index>=n && target == 0) return 0;\n if(index>=n || target<0) return INT_MAX;\n // cout<<prev<<\" \"<<index<<\" \...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n int solve(int prev,vector<int>& houses, vector<vector<int>>& cost,int index, int &n, int &m, int target,vector<vector<vector<int>>> &dp){\n if(index>=n && target == 0) return 0;\n if(index>=n || target<0) return INT_MAX;\n // cout<<prev<<\" \"<<index<<\" \...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n int solve(int idx,int p,int cnt,vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar,vector<vector<vector<int>>>&dp){\n if(idx==m){\n if(cnt==tar) return 0;\n return 1e9;\n }\n\n if(dp[idx][p][cnt]!=-1) return dp[idx][p...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(int idx,int p,int cnt,vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar,vector<vector<vector<int>>>&dp){\n if(idx==m){\n if(cnt==tar) return 0;\n return 1e9;\n }\n\n if(dp[idx][p][cnt]!=-1) return dp[idx][p...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(int idx,int p,int cnt,vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar,vector<vector<vector<int>>>&dp){\n if(idx==m){\n if(cnt==tar) return 0;\n return 1e9;\n }\n\n if(dp[idx][p][cnt]!=-1) return dp[idx][p...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> memo;\n int M, N;\n vector<int> * _houses;\n vector<vector<int>> * _costs;\n int dp(int i, int remain, int color)\n {\n if (i >= M)\n {\n if (remain != 0)\n {\n return -1;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> memo;\n int M, N;\n vector<int> * _houses;\n vector<vector<int>> * _costs;\n int dp(int i, int remain, int color)\n {\n if (i >= M)\n {\n if (remain != 0)\n {\n return -1;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n long int solve(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target,int i ,int prev_color,vector<vector<vector<int>>>&dp){ \n if(i == m)//all houses analysed\n return (target==0) ? 0: INT_MAX;\n if(target < 0)\n r...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int help(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target,int i,vector<vector<vector<int>>>& dp,int prev){\n if(i==m && target>1) return 1e9;\n if(i==m && target==1) return 0;\n if(target==0) return 1e9;\n if(dp[i][target][p...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "#define vvi vector<vector<int>>\n#define vvvi vector<vector<vector<int>>>\n\n#pragma GCC optimize(\"O3, unroll-loops\")\n#pragma GCC target(\"avx2, bmi, bmi2, lzcnt, popcnt\")\n\nstatic auto _ = []() { \n cin.tie(nullptr) -> ios_base::sync_with_stdio(false); \n return nullptr; \n} ();\n\nclass Soluti...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\nint SolveByDP(vector<int>& houses, vector<vector<int>>& cost, int &m, int &n, int &target,int i,int groups,int prevcol,vector<vector<vector<int>>>&dp){\n if(groups > target)return INT_MAX;\n if(i>=m){\n if(groups==target){\n return 0;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& houses, vector<vector<int>>& cost, int n, int m, int target,int ind,int prev,vector<vector<vector<int>>>&dp){\n if(target==0 && ind==m){\n return 0;\n }\n if(ind>=m || target<0){\n return 1e9;\n }\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& houses, vector<vector<int>>& cost, int clrs, int target, int idx, int prevClr, vector<vector<vector<int>>>& dp) {\n if (target < 0) return 1e7; // Impossible to achieve target\n if (idx == houses.size()) {\n if (target == 0) re...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> dp;\n int solver(int i, int nbrhd, int prevCol, vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target){\n // cout<<i<<\" \"<<nbrhd<<endl;\n if(nbrhd > target && i<m) return INT_MAX;\n if(i >= m){\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int> & houses, vector<vector<int>>& cost, int target, int prev, int index,vector<vector<vector<int>>>& dp){\n if(index>=houses.size()){\n return (target==0)?0:INT_MAX;\n } \n if(target<0) return INT_MAX; \n\n if(d...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& houses, vector<vector<int>>& cost, \n int m, int n, int target,int ind,int prev,\n vector<vector<vector<int>>> &dp){\n //base case:\n if(ind>=m){\n if(target==0){\n return 0;\n }\n retu...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int solve(int i,vector<int>&house,vector<vector<int>>&cost,int target,int cnt,int prev, vector<vector<vector<int>>>&dp){\n if(i==house.size()){\n // cout<<target<<\" \"<<cnt<<endl;\n if(target==cnt){\n return 0;\n }\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\nprivate:\n vector<vector<vector<int>>> dp;\n int util(vector<int>& houses, vector<vector<int>>& cost, int i, int target, int p) {\n if(i == houses.size() || target < 0) \n return target==0? 0 : 1e6+1;\n if(houses[i] != 0)\n return util(houses, cos...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> dp;\n\nint f(vector<int>& houses, vector<vector<int>>& cost, int target, int idx, int count, int prev){\n //base cases\n if(idx == houses.size()) return count==target ? 0 : 1e9;\n \n //memoization\n if(dp[idx][count][prev]!=-1) retur...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> dp;\n\nint f(vector<int>& houses, vector<vector<int>>& cost, const int& target, int idx, int count, int prev){\n if (count>target) return 1e9;\n if (idx == houses.size()) return count==target ? 0 : 1e9;\n if (dp[idx][count][prev] != -1) re...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& A, vector<vector<int>>& B, int m, int n, int t) {\n int M = 1e9;\n vector<vector<int>> v(m, vector<int>(n + 3, M));\n for(int &p : v[0])\n p = 0;\n for(int i = 0; i < m; ++i)\n {\n auto u(v);\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& A, vector<vector<int>>& B, int m, int n, int t) {\n int M = 1e9;\n vector<vector<int>> v(m, vector<int>(n + 3, M));\n for(int &p : v[0])\n p = 0;\n for(int i = 0; i < m; ++i)\n {\n auto u(v);\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int helper(int ind, vector<vector<int>>&cost, int m, int n, int target, vector<int>&houses, int prev, vector<vector<vector<int>>>&dp){\n if(target<0){\n return 1e9;\n }\n if(ind == m){\n if(target == 0)return 0;\n else ret...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // dp[i][j][target] = min(dp[i - 1][j][target] + cost[i][j],\n // dp[i - 1][not j][target - 1] + cost[i - 1][not j])\n // cout << \"m=\" << m ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<long> > > dp(m, vector<vector<long> > (n, vector<long>(target + 1, INT_MAX)));\n if (houses[0] != 0) {\n int color = houses[0] - 1;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // dp[i][j][target] = min(dp[i - 1][j][target] + cost[i][j],\n // dp[i - 1][not j][target - 1] + cost[i - 1][not j])\n // cout << \"m=\" << m ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<long> > > dp(m, vector<vector<long> > (n, vector<long>(target + 1, INT_MAX)));\n if (houses[0] != 0) {\n int color = houses[0] - 1;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // dp[i][j][k] - i-th house painted j-th color with k neighbourhoods\n // if houses[i] == 0 || houses[i] == j then \n // dp[i][j][k] = min(x=1..n, x != j min(d...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n // constraints: m <= 100 and cost[i][j] <= 10000 so total cost at most 1e6\n const int infty = 1e6+1;\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<int>>> dp(m, vector<vector<int>>(target+1, vecto...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n,\n int target) {\n vector<vector<vector<long long>>>dynamic(\n m, vector<vector<long long>>(n + 1, vector<long long>(target + 1, -1)));\n auto dp = [&](auto && sel...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n,\n int target) {\n vector<vector<vector<long long>>>dynamic(\n m, vector<vector<long long>>(n + 1, vector<long long>(target + 1, -1)));\n auto dp = [&](auto && sel...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\nlong long solve(vector<int> & house,vector<vector<int>> &cost,int target,int prev,int index,vector<vector<vector<long long>>> &dp){\n int n=cost.size();\n int m=cost[0].size();\n if(target==0 && index>=n){\n return 0;\n }\n if(target<0){\n return INT_...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<long long>>> dp(m + 1, vector(n + 1, vector(target + 1, (long long) INT_MAX)));\n\n dp[0] = vector(n + 1, vector(target + 1, 0LL));\n\n long lon...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "const static auto speedup = [] () {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n \n return 1;\n} ();\n\nclass Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<ve...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\nprivate:\n int solve(int i, int last, int cnt, vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target, vector<vector<vector<int>>>& dp){\n //cout << i << \" \" << last << \" \" << cnt << endl;\n if(cnt > target) return 1e9;\n if(i == m){\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\nprivate:\n int solve(int i, int last, int cnt, vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target, vector<vector<vector<int>>>& dp){\n //cout << i << \" \" << last << \" \" << cnt << endl;\n if(i == m){\n if(cnt == target) return 0;\n ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "#define ll long long\nclass Solution {\n vector<int> h;\n vector<vector<int>> c;\n int n, m;\n vector<vector<vector<ll>>> dp;\n \npublic:\n int solve(int ind, int prevColor, int target){\n if (target < 0) return INT_MAX;\n if (ind == m) return target == 0 ? 0 : INT_MAX; \n\n...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "#define ll long long\nclass Solution {\n vector<int> h;\n vector<vector<int>> c;\n int n, m;\n vector<vector<vector<ll>>> dp;\n \npublic:\n int solve(int ind, int prevColor, int target){\n if (target < 0) return INT_MAX;\n if (ind == m) return target == 0 ? 0 : INT_MAX; \n\n...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
3
{ "code": "class Solution {\npublic:\n long long func (int i,int prev,int t,vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target, vector <vector <vector <long long > > > & dp){\n if (i==m){\n if (t==target)return 0;\n return INT_MAX;\n }\n if (t>target...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "class Solution {\npublic:\n int rob(vector<int>& nums) {\n int rob_prev_skip = 0, rob_prev = nums[0], max_rob = nums[0];\n\n for(int i = 1; i < nums.size(); ++i) {\n max_rob = max(rob_prev, rob_prev_skip + nums[i]);\n\n rob_prev_skip = rob_prev;\n rob_prev ...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "class Solution {\nprivate:\n int solve(vector<int>& nums) {\n int n = nums.size();\n if (n == 0) return 0;\n int prev = nums[0]; \n int prev2 = 0; \n \n for (int i = 1; i < n; i++) {\n int pick = nums[i]; \n if (i > 1)\n pi...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n int t[401];\n\n int dp(int i,vector<int> &a){\n if(i>=a.size())\n return 0;\n if(t[i]!=-1)\n return t[i];\n\n return t[i] = max(a[i]+dp(i+2,a), dp(i+1,a));\n }\n\n int rob(vector<int...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "class Solution {\npublic:\n int rob(vector<int>& nums) {\n int n=nums.size();\n if(n==0)\n return 0;\n if(n==1)\n return nums[0];\n int one=nums[0];\n int second=max(nums[1],nums[0]);\n for(int i=2; i<nums.size(); i++){\n nums[i]=max(nums[i]+one...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "class Solution {\npublic:\n int t[101];\n int solve(vector<int>&nums,int i,int n){\n if(i>=n){\n return 0;\n }\n if(t[i]!=-1){\n return t[i];\n }\n int steal=nums[i]+solve(nums,i+2,n);\n int skip=solve(nums,i+1,n);\n return t[i]=m...
198
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
0
{ "code": "class Solution {\npublic:\n int rob(vector<int>& nums) {\n int a = 0, b = 0, c = 0;\n for(auto i : nums){\n int temp = max(a, b) + i;\n a = b;\n b = c;\n c = temp;\n }\n return max(c, b);\n }\n};", "memory": "9300" }
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "\nclass Solution {\npublic:\n int levels(TreeNode* root){\n if(root==NULL)return 0;\n return 1+max(levels(root->left),levels(root->right));\n }\n // void nthLevel(TreeNode* root,vector<int>&v,int curr,int n){\n // if (root==NULL)return;\n // //bfs traversing\n // if(curr...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
199
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
0
{ "code": "class Solution {\npublic:\n\n void solve(int i,int j,int n,int m,vector<vector<char>>& grid){\n if(i>=n ||j>=m)return ;\n if(i<0||j<0)return ;\n if(grid[i][j]=='0'){\n return ;\n }\n else{\n grid[i][j]='0';\n solve(i+1,j,n,m,grid);\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
0
{ "code": "class Solution {\n int n, m;\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int count = 0;\n n = grid.size();\n m = grid[0].size();\n\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < m; j++) {\n if(grid[i][j] == '1') {\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
0
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size(), m = n ? grid[0].size() : 0, islands = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == '1') {\n islands++;\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
0
{ "code": "class Solution {\npublic:\n int n,m;\n bool vis[305][305];\n vector<pair<int,int>> d = {{0,1}, {0,-1}, {-1,0}, {1,0}};\n bool valid(int ci, int cj)\n {\n if(ci >= 0 && ci <n && cj >= 0 && cj < m) return true;\n else return false;\n }\n void dfs(int si, int sj, vector<vect...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int x[4] = {-1,1,0,0};\n int y[4] = {0,0,-1,1};\n void dfs(vector<vector<char>>& grid,vector<vector<bool>>& visited,int i,int j,int m,int n){\n visited[i][j] = 1;\n for(int k=0;k<4;k++){\n if(i+x[k] >=0 && i+x[k] < m && j+y[k] >=0 && j+y[k] < n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\nprivate:\n void dfs(int row , int col , vector<vector<char>>& grid , vector<vector<bool>> &vis)\n {\n int m = grid.size();\n int n = grid[0].size();\n\n //base case --> out of bound\n if(row < 0 ||row > m-1 || col < 0 || col > n-1 || grid[row][col] != '1' || vis[row][col...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic: \n int m,n;\n vector<vector<int>> directions {{1,0},{-1,0},{0,1},{0,-1}};\n void dfs(vector<vector<char>>& grid,int row,int col)\n {\n if(row<0 ||col<0 || row>=m ||col>=n || grid[row][col]=='0') return;\n grid[row][col]='0';\n for(auto...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n\n void dfs(int x, int y, vector<vector<char>> &grid, vector<vector<bool>> &v) {\n v[x][y] = true;\n\n int ofX[] = {1, -1, 0, 0};\n int ofY[] = {0, 0, -1, 1};\n\n int n = grid.size();\n int m = grid[0].size();\n\n for(int i = 0 ; i < 4...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\n void func(int i,int j,vector<vector<char>> &grid,vector<vector<bool>> &vis,int &n,int &m){\n if(i<0 || i>=n || j<0 || j>=m || vis[i][j] || grid[i][j]=='0'){\n return;\n }\n vis[i][j]=true;\n func(i+1,j,grid,vis,n,m);\n func(i-1,j,grid,vis,...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n bool check(int i , int j , int n , int m , vector<vector<char>>& grid, vector<vector<bool>> &vis){\n if(i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == '0' || vis[i][j] == true){\n return false;\n }else{\n return true;\n }\n }...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\n \n int X, Y;\n int islands = 0;\n const int dx[4] = {-1, 1, 0, 0};\n const int dy[4] = {0, 0,-1, 1};\n int visited[301][301] = {0};\n void search(int y, int x, vector<vector<char>> &m)\n {\n if (y < 0 || x < 0 || y >= Y || x >= X)\n {\n re...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\n const vector<vector<int>> DIRS = { {0,1}, {1,0}, {0,-1}, {-1,0}};\n\n void visitIsland(const vector<vector<char>>& grid, int rows, int cols, int row, int col, vector<vector<bool>>& visited) {\n if (row < 0 || row >= rows || col < 0 || col >= cols || visited[row][col] || grid...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\n \n int X, Y;\n int islands = 0;\n const int dx[4] = {-1, 1, 0, 0};\n const int dy[4] = {0, 0,-1, 1};\n int visited[301][301] = {0};\n void search(int y, int x, vector<vector<char>> &m)\n {\n if (y < 0 || x < 0 || y >= Y || x >= X)\n {\n re...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<char>> &grid, int row, int col) {\n grid[row][col] = '0';\n\n constexpr pair<int, int> delta[] = {\n {1, 0},\n {-1, 0},\n {0, 1},\n {0, -1},\n };\n\n for (const auto &[drow, dcol] :...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n\n vector<vector<bool>> visited;\n vector<vector<bool>> grd;\n\n int dr[4] = {-1,0,0,1};\n int dc[4] = {0,1,-1,0};\n\n void dfs(int r, int c)\n {\n if(visited[r][c]) return;\n visited[r][c] = true;\n\n for(int i = 0; i < 4; i++)\n {\n...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<int> v(m*n, 0);\n for (int i = 0; i < v.size(); i++) {\n v[i] = i;\n }\n auto find = [&](int p) -> int {\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int n;\n int m;\n \n void dfs(int i, int j, vector<int> &visited, vector<vector<char>>& grid) {\n visited[i * m + j] = 1;\n if (j < m - 1 && grid[i][j + 1] == '1' && !visited[i * m + (j + 1)]) //right\n dfs(i, j + 1, visited, grid);\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n queue<pair<int, int>> que;\n int cnt = 0;\n for(int i = 0; i < grid.size(); i++) {\n for(int j = 0; j < grid[0].size(); j++) {\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if (grid.empty() || grid[0].empty()) {\n return 0;\n }\n \n int numIslands = 0;\n int m = grid.size();\n int n = grid[0].size();\n vector<pair<int, int>> directions = {{...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n const vector<int> dx = {-1, 1, 0, 0};\n const vector<int> dy = {0, 0, 1, -1};\n int n, m, vis[302][302], a[302][302];\n\n void dfs(int i, int j) {\n vis[i][j] = true;\n for (int z = 0; z < 4; z++) {\n int x = i + dx[z], y = j + dy[z];\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n void isIsland(vector<vector<char>>& grid,int x,int y,vector<vector<int>>& visited){\n if(!visited[x][y]){\n visited[x][y] = 1;\n int dx[4] = {+1,-1,0,0};\n int dy[4] = {0,0,-1,+1};\n\n for(int i=0; i<4; i++){\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\nprivate:\n void dfs(int r, int c, vector<vector<int>>& vis, vector<vector<char>>& grid, int drow[], int dcol[]){\n vis[r][c] = 1;\n int n = grid.size();\n int m = grid[0].size();\n //Traverse\n for(int i = 0; i < 4; i++){\n int nr = r + dro...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if(grid.empty() || grid[0].empty())\n return 0;\n \n int rows = grid.size();\n int cols = grid[0].size();\n int islands = 0;\n \n function<void(int, int)> dfs = [&](int ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
1
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};\n queue<pair<int, int>> q;\n int re...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if(grid.empty() || grid[0].empty())\n return 0;\n \n int rows = grid.size();\n int cols = grid[0].size();\n int islands = 0;\n \n function<void(int, int)> dfs = [&](int ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<int>>& visited, int i, int j, vector<vector<char>>& grid, int n, int m){\n queue<pair<int,int>> q;\n q.push({i, j});\n while(!q.empty()){\n auto temp = q.front();\n q.pop();\n i = temp.first;\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n queue<pair<int,int>> q;\n int m=grid.size(),n=grid[0].size();\n std::vector<std::vector<bool>> visited(m, std::vector<bool>(n, false));\n //vector<vector<bool>> visited=(m,vector<bool>(n,false));\n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n\n int m = grid.size(), n = grid[0].size();\n vector<vector<bool>> isVisited(m, vector<bool>(n, 0));\n\n int numIslands = 0;\n\n\n function<void(int, int)> dfs = [&](int i, int j) {\n \n ...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int num = 0;\n vector<vector<bool>> vis(grid.size(), vector<bool>(grid[0].size(), false));\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n if...
200
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
2
{ "code": "class Solution {\npublic:\n void visit(vector<vector<char>>& grid, int i, int j) {\n std::vector<std::pair<int, int>> toVisit;\n toVisit.emplace_back(i, j);\n int m = grid.size();\n int n = grid[0].size();\n while (!toVisit.empty()) {\n auto [row, col] = toV...