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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 memo[i][last][cnt];\n if (H[i]) return memo[i][last][cnt] = dp(i + 1, H[i], H[i] == last ? cnt : (cnt + 1));\n int ans = INF;\n for (int j = 0; j < N; ++j) ans = min(ans, C[i][j] + dp(i + 1, j + 1, j + 1 == last ? cnt : (cnt + 1)));\n return memo[i][last][cnt] = ans;\n }\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n H = houses, C = cost, M = m, N = n, T = target;\n memo.assign(M, vector<vector<int>>(N + 1, vector<int>(T + 1, -1)));\n int ans = dp(0, 0, 0);\n return ans == INF ? -1 : ans;\n }\n\t// If you find helpful than upvote\n};", "memory": "18739" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if (dp[i][prevColor][target]!=-1) return dp[i][prevColor][target];\n int ans=INT_MAX;\n if (houses[i]!=0) {\n if (prevColor==houses[i]) ans=helper(houses, cost, m, n, target, i+1, prevColor, dp);\n else ans=ans=helper(houses, cost, m, n, target-1, i+1, houses[i], dp);\n } else {\n for (int j=1;j<=n;j++){\n if (j==prevColor) {\n int h=helper(houses, cost, m, n, target, i+1, prevColor, dp);\n if (h!=INT_MAX) ans=min(ans, cost[i][j-1]+h);\n }\n else {\n int h=helper(houses, cost, m, n, target-1, i+1, j, dp);\n if (h!=INT_MAX) ans=min(ans, cost[i][j-1]+h);\n }\n }\n }\n return dp[i][prevColor][target]=ans;\n }\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+2, vector<int>(target+1, -1)));\n int ans=helper(houses, cost, m, n, target, 0, 0, dp);\n if (ans!=INT_MAX) return ans;\n return -1;\n }\n};", "memory": "18925" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 return prev == houses[0] ? 1e8 : 0;\n }\n int minLast = 1e8;\n for(int k = 1; k<=n; k++){\n if(prev!=k){\n minLast = min(minLast,cost[0][k-1]);\n }\n }\n return minLast;\n }\n else if(target==0){\n if(houses[0] != 0){\n return prev == houses[0] ? 0 : 1e8;\n }\n return cost[0][prev-1];\n }\n return 1e8;\n }\n if(dp[i][prev][target] != -1){\n return dp[i][prev][target];\n }\n if(houses[i] != 0){\n if(prev<=n && prev == houses[i]){\n return dp[i][prev][target] = f(i-1,prev,target,n,houses,cost,dp);\n }\n return dp[i][prev][target] = f(i-1,houses[i],target-1,n,houses,cost,dp);\n }\n else{\n int mini = 1e8;\n int res;\n for(int k=1;k<=n;k++){\n if(prev==k){\n res = cost[i][k-1] + f(i-1,k,target,n,houses,cost,dp);\n }\n else{\n res = cost[i][k-1] + f(i-1,k,target-1,n,houses,cost,dp);\n }\n mini = min(mini,res);\n }\n return dp[i][prev][target] = mini;\n }\n }\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+2,vector<int>(target+1,-1)));\n int x = f(m-1,n+1,target,n,houses,cost,dp);\n return x>=1e8? -1:x;\n }\n};", "memory": "19111" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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(dp[i][j][num] != -1) return dp[i][j][num];\n if(h[i] == 0){\n int t1 = c[i][j-1] + solve(h,c,m,n,t,i+1,j,num, dp);\n int t2 = 1e9;\n for(int k=0;k<c[0].size();k++){\n if(k+1 == j) continue;\n t2 = min(t2, c[i][k] + solve(h, c, m, n, t, i+1, k+1, num+1, dp));\n }\n return dp[i][j][num] = min(t1, t2);\n }\n else{\n if(j == h[i]){\n return dp[i][j][num] = solve(h, c, m, n, t, i+1, j, num, dp);\n }\n else{\n return dp[i][j][num] = solve(h, c, m, n, t, i+1, h[i], num+1, dp);\n }\n }\n return dp[i][j][num] = 0;\n }\n\n int minCost(vector<int>& h, vector<vector<int>>& c, int m, int n, int t) {\n int ans = 1e9;\n vector<vector<vector<int>>> dp(m+1, vector<vector<int>>(n+2, vector<int>(t+1, -1)));\n if(h[0] != 0){\n ans = solve(h, c, m, n, t, 1, h[0], 1, dp);\n if(ans >= 1e9) return -1;\n return ans;\n }\n for(int i=0;i<n;i++){\n ans = min(ans, c[0][i] + solve(h, c, m, n, t, 1, i+1, 1, dp));\n }\n if(ans >= 1e9) return -1;\n return ans;\n }\n};", "memory": "19298" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if (res == MAXCOST) return -1;\n else return res;\n\n }\n\n int recurse(const vector<int>& houses, const vector<vector<int>>& cost, int target, int h, int prevColor, int currNei, vector<vector<vector<int>>>& dp){\n // cout << h << \" , \" << prevColor << \" , \" << currNei << endl;\n if (h == houses.size() ) {\n if (currNei == target)\n return 0;\n else return MAXCOST;\n }\n else if ( dp[h][prevColor][currNei] != -1) return dp[h][prevColor][currNei];\n\n int res = MAXCOST;\n if (houses[h] == 0){\n for (int j = 1; j <= cost[0].size(); j++){\n if (prevColor == j ) res = min(res, cost[h][j-1] + recurse(houses, cost, target, h+1, prevColor, currNei, dp));\n else res = min(res,cost[h][j-1] + recurse(houses, cost, target, h+1, j, currNei+1, dp));\n }\n return dp[h][prevColor][currNei] = res;\n } else {\n if (prevColor == houses[h]) return dp[h][prevColor][currNei] = recurse(houses, cost, target, h+1, prevColor,currNei, dp);\n else return dp[h][prevColor][currNei] = recurse(houses, cost, target, h+1, houses[h],currNei+1, dp);\n }\n\n }\n};", "memory": "19484" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if(dp[ind][prev+1][target] != -1) return dp[ind][prev+1][target];\n \n if(houses[ind] != 0) {\n bool flag = false;\n if(prev != houses[ind]) {\n flag = true;\n if(prev == -1) target++;\n }\n return dp[ind][prev+1][target] = solve(houses, costs, n, m, target - flag, ind+1, houses[ind], dp);\n }\n int ans = 1e9;\n \n if(prev == -1) target++;\n for(int i=0; i<costs[ind].size(); i++) {\n int col = i+1;\n if(col != prev) {\n ans = min(ans, costs[ind][i] + solve(houses, costs, n, m, target-1, ind+1, col, dp));\n }\n else ans = min(ans, costs[ind][i] + solve(houses, costs, n, m, target, ind+1, col, dp));\n }\n \n return dp[ind][prev+1][target] = ans; \n }\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int n, int m, int target) {\n \n vector<vector<vector<int>>> dp(n+1, vector<vector<int>> (m+2, vector<int> (target+2, -1)));\n \n int ans = solve(houses, cost, n, m, target, 0, -1, dp);\n if(ans == 1e9) return -1;\n return ans;\n }\n};", "memory": "19670" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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][cnt];\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }else{\n if(arr[idx]==p){\n ans = solve(idx+1,arr[idx],cnt,arr,cost,m,n,tar,dp);\n }else{\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + solve(idx+1,cur,cnt,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + solve(idx+1,cur,cnt+1,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }\n }\n }\n\n return dp[idx][p][cnt] = ans;\n }\n int minCost(vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar) {\n\n vector<vector<vector<int>>>dp(m,vector<vector<int>>(n+1,vector<int>(m+1,-1)));\n int ans = solve(0,0,0,arr,cost,m,n,tar,dp);\n\n if(ans==1e9) ans = -1;\n return ans;\n }\n};", "memory": "19856" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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<<\" \"<<target<<endl;\n if(dp[prev][index][target] != -1) return dp[prev][index][target];\n int x = INT_MAX,y = INT_MAX,z = INT_MAX;\n if(houses[index] != 0){\n if(prev == houses[index]) x = solve(prev,houses,cost,index+1,n,m,target,dp);\n else if(target-1 >= 0) x = solve(houses[index],houses,cost,index+1,n,m,target-1,dp);\n }\n else{\n\n y = solve(prev,houses,cost,index+1,n,m,target,dp);\n if(y != INT_MAX) y += cost[index][prev-1];\n\n int temp = 0;\n if(target-1>=0){\n for(int i = 0;i<m;i++){\n if(i+1 == prev) continue;\n temp = solve(i+1,houses,cost,index+1,n,m,target-1,dp);\n if(temp != INT_MAX) temp += cost[index][i];\n z = min(z,temp);\n }\n }\n }\n\n return dp[prev][index][target] = min({x,y,z});\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int n, int m, int target) {\n int temp,mini=INT_MAX;\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(target+1,-1)));\n if(houses[0] != 0){\n temp = solve(houses[0],houses,cost,1,n,m,target-1,dp);\n mini = min(mini,temp);\n }\n else{\n for(int i = 0;i<m;i++){\n temp = solve(i+1,houses,cost,1,n,m,target-1,dp);\n if(temp != INT_MAX) temp += cost[0][i];\n mini = min(mini,temp);\n }\n }\n\n return mini==INT_MAX?-1:mini;\n }\n}; ", "memory": "20601" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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<<\" \"<<target<<endl;\n if(dp[prev][index][target] != -1) return dp[prev][index][target];\n int x = INT_MAX,y = INT_MAX,z = INT_MAX;\n if(houses[index] != 0){\n if(prev == houses[index]) x = solve(prev,houses,cost,index+1,n,m,target,dp);\n else if(target-1 >= 0) x = solve(houses[index],houses,cost,index+1,n,m,target-1,dp);\n }\n else{\n\n y = solve(prev,houses,cost,index+1,n,m,target,dp);\n if(y != INT_MAX) y += cost[index][prev-1];\n\n int temp = 0;\n if(target-1>=0){\n for(int i = 0;i<m;i++){\n if(i+1 == prev) continue;\n temp = solve(i+1,houses,cost,index+1,n,m,target-1,dp);\n if(temp != INT_MAX) temp += cost[index][i];\n z = min(z,temp);\n }\n }\n }\n\n return dp[prev][index][target] = min({x,y,z});\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int n, int m, int target) {\n int temp,mini=INT_MAX;\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(target+1,-1)));\n if(houses[0] != 0){\n temp = solve(houses[0],houses,cost,1,n,m,target-1,dp);\n mini = min(mini,temp);\n }\n else{\n for(int i = 0;i<m;i++){\n temp = solve(i+1,houses,cost,1,n,m,target-1,dp);\n if(temp != INT_MAX) temp += cost[0][i];\n mini = min(mini,temp);\n }\n }\n\n return mini==INT_MAX?-1:mini;\n }\n}; ", "memory": "20788" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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][cnt];\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }else{\n if(arr[idx]==p){\n ans = solve(idx+1,arr[idx],cnt,arr,cost,m,n,tar,dp);\n }else{\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + solve(idx+1,cur,cnt,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + solve(idx+1,cur,cnt+1,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }\n }\n }\n\n return dp[idx][p][cnt] = ans;\n }\n int minCost(vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar) {\n\n // vector<vector<vector<int>>>dp(m,vector<vector<int>>(n+1,vector<int>(m+1,-1)));\n // int ans = solve(0,0,0,arr,cost,m,n,tar,dp);\n\n // if(ans==1e9) ans = -1;\n // return ans;\n\n\n\n // tabulation\n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(n+2,vector<int>(m+2,1e9)));\n\n \n for(int p = n; p >= 0; p--){\n for(int cnt = m; cnt >= 0; cnt--){\n if(cnt==tar) dp[m][p][cnt] = 0;\n else\n dp[m][p][cnt] = 1e9;\n }\n }\n \n for(int idx = m-1; idx>=0; idx--){\n for(int p = n; p >= 0; p--){\n for(int cnt = m; cnt >= 0; cnt--){\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = dp[idx+1][arr[idx]][cnt+1];\n }else{\n if(arr[idx]==p){\n ans = dp[idx+1][arr[idx]][cnt];\n }else{\n ans = dp[idx+1][arr[idx]][cnt+1];\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + dp[idx+1][cur][cnt];\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + dp[idx+1][cur][cnt+1];\n ans = min(ans,res);\n }\n }\n } \n dp[idx][p][cnt] = ans; \n }\n }\n }\n if(dp[0][0][0]==1e9) dp[0][0][0] = -1;\n return dp[0][0][0];\n }\n};", "memory": "20974" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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][cnt];\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }else{\n if(arr[idx]==p){\n ans = solve(idx+1,arr[idx],cnt,arr,cost,m,n,tar,dp);\n }else{\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + solve(idx+1,cur,cnt,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + solve(idx+1,cur,cnt+1,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }\n }\n }\n\n return dp[idx][p][cnt] = ans;\n }\n int minCost(vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar) {\n\n // vector<vector<vector<int>>>dp(m,vector<vector<int>>(n+1,vector<int>(m+1,-1)));\n // int ans = solve(0,0,0,arr,cost,m,n,tar,dp);\n\n // if(ans==1e9) ans = -1;\n // return ans;\n\n\n\n // tabulation\n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(n+2,vector<int>(m+2,1e9)));\n\n \n for(int p = n+1; p >= 0; p--){\n for(int cnt = m+1; cnt >= 0; cnt--){\n if(cnt==tar) dp[m][p][cnt] = 0;\n else\n dp[m][p][cnt] = 1e9;\n }\n }\n \n for(int idx = m-1; idx>=0; idx--){\n for(int p = n; p >= 0; p--){\n for(int cnt = m; cnt >= 0; cnt--){\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = dp[idx+1][arr[idx]][cnt+1];\n }else{\n if(arr[idx]==p){\n ans = dp[idx+1][arr[idx]][cnt];\n }else{\n ans = dp[idx+1][arr[idx]][cnt+1];\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + dp[idx+1][cur][cnt];\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + dp[idx+1][cur][cnt+1];\n ans = min(ans,res);\n }\n }\n } \n dp[idx][p][cnt] = ans; \n }\n }\n }\n if(dp[0][0][0]==1e9) dp[0][0][0] = -1;\n return dp[0][0][0];\n }\n};", "memory": "21160" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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][cnt];\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }else{\n if(arr[idx]==p){\n ans = solve(idx+1,arr[idx],cnt,arr,cost,m,n,tar,dp);\n }else{\n ans = solve(idx+1,arr[idx],cnt+1,arr,cost,m,n,tar,dp);\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + solve(idx+1,cur,cnt,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + solve(idx+1,cur,cnt+1,arr,cost,m,n,tar,dp);\n ans = min(ans,res);\n }\n }\n }\n\n return dp[idx][p][cnt] = ans;\n }\n int minCost(vector<int>& arr, vector<vector<int>>& cost, int m, int n, int tar) {\n\n // vector<vector<vector<int>>>dp(m,vector<vector<int>>(n+1,vector<int>(m+1,-1)));\n // int ans = solve(0,0,0,arr,cost,m,n,tar,dp);\n\n // if(ans==1e9) ans = -1;\n // return ans;\n\n\n\n // tabulation\n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(n+2,vector<int>(m+2,1e9)));\n\n \n for(int p = n+1; p >= 0; p--){\n for(int cnt = m+1; cnt >= 0; cnt--){\n if(cnt==tar) dp[m][p][cnt] = 0;\n else\n dp[m][p][cnt] = 1e9;\n }\n }\n \n for(int idx = m-1; idx>=0; idx--){\n for(int p = n+1; p >= 0; p--){\n for(int cnt = m; cnt >= 0; cnt--){\n int ans = 1e9;\n if(arr[idx]!=0){\n if(p==0){\n ans = dp[idx+1][arr[idx]][cnt+1];\n }else{\n if(arr[idx]==p){\n ans = dp[idx+1][arr[idx]][cnt];\n }else{\n ans = dp[idx+1][arr[idx]][cnt+1];\n }\n }\n \n }else{\n for(int j = 0; j < n; j++){\n int cur = j+1;\n if(cur==p){\n int res = cost[idx][j] + dp[idx+1][cur][cnt];\n ans = min(ans,res);\n }else{\n int res = cost[idx][j] + dp[idx+1][cur][cnt+1];\n ans = min(ans,res);\n }\n }\n } \n dp[idx][p][cnt] = ans; \n }\n }\n }\n if(dp[0][0][0]==1e9) dp[0][0][0] = -1;\n return dp[0][0][0];\n }\n};", "memory": "21346" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 }\n else\n {\n return 0;\n }\n }\n if (remain < 0) {return -1;}\n if (memo[i][remain][color] != -2) {return memo[i][remain][color];}\n \n vector<int>& houses = * _houses;\n vector<vector<int>>& costs = *_costs;\n if (houses[i] == color)\n {\n memo[i][remain][color] = dp(i+1, remain, color);\n }\n else if (houses[i] != color && houses[i] != 0)\n {\n memo[i][remain][color] = dp(i+1, remain-1, houses[i]);\n }\n else\n {\n int cost = INT_MAX;\n for (int c = 1; c <= N; c++)\n {\n int remain_cost = 0;\n if (c != color)\n {\n remain_cost = 1;\n }\n int next_cost = dp(i+1, remain - remain_cost, c);\n if (next_cost < 0) {continue;}\n cost = std::min(cost, costs[i][c - 1] + next_cost);\n }\n if (cost == INT_MAX)\n {\n cost = -1;\n }\n memo[i][remain][color] = cost;\n }\n return memo[i][remain][color];\n }\n \n \n int minCost(vector<int>& houses, vector<vector<int>>& costs, int m, int n, int target) {\n M = m;\n N = n;\n _houses = & houses;\n _costs = & costs;\n memo.resize(m, vector<vector<int>>(target + 1, vector<int>(n + 1, -2)));\n\n if (houses[0] != 0)\n {\n return dp(1, target - 1, houses[0]);\n }\n else\n {\n int cost = INT_MAX;\n for (int c = 1; c <= n; c++)\n {\n int next_cost = dp(1, target - 1, c);\n if (next_cost == -1)\n {\n continue;\n }\n \n cost = std::min(cost, costs[0][c - 1] + next_cost);\n }\n if (cost == INT_MAX)\n {\n return -1;\n }\n return cost;\n } \n }\n};", "memory": "21533" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 }\n else\n {\n return 0;\n }\n }\n if (remain < 0) {return -1;}\n if (memo[i][remain][color] != -2) {return memo[i][remain][color];}\n \n vector<int>& houses = * _houses;\n vector<vector<int>>& costs = *_costs;\n if (houses[i] == color)\n {\n memo[i][remain][color] = dp(i+1, remain, color);\n }\n else if (houses[i] != color && houses[i] != 0)\n {\n memo[i][remain][color] = dp(i+1, remain-1, houses[i]);\n }\n else\n {\n int cost = INT_MAX;\n for (int c = 1; c <= N; c++)\n {\n int remain_cost = 0;\n if (c != color)\n {\n remain_cost = 1;\n }\n int next_cost = dp(i+1, remain - remain_cost, c);\n if (next_cost < 0) {continue;}\n cost = std::min(cost, costs[i][c - 1] + next_cost);\n }\n if (cost == INT_MAX)\n {\n cost = -1;\n }\n memo[i][remain][color] = cost;\n }\n return memo[i][remain][color];\n }\n \n \n int minCost(vector<int>& houses, vector<vector<int>>& costs, int m, int n, int target) {\n M = m;\n N = n;\n _houses = & houses;\n _costs = & costs;\n memo.resize(m, vector<vector<int>>(target + 1, vector<int>(n + 1, -2)));\n\n if (houses[0] != 0)\n {\n return dp(1, target - 1, houses[0]);\n }\n else\n {\n int cost = INT_MAX;\n for (int c = 1; c <= n; c++)\n {\n int next_cost = dp(1, target - 1, c);\n if (next_cost == -1)\n {\n continue;\n }\n \n cost = std::min(cost, costs[0][c - 1] + next_cost);\n }\n if (cost == INT_MAX)\n {\n return -1;\n }\n return cost;\n } \n }\n};", "memory": "21533" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 return INT_MAX;\n if(dp[i][target][prev_color] != -1)\n return dp[i][target][prev_color];\n \n long int ans = INT_MAX;\n if(houses[i] != 0)//color assigned already\n //grp is increased by 1 only if color is same\n //color of current house becomes the previous color\n //only the cost of unpainted house will be added\n ans = std::min(ans,solve(houses,cost,m,n,target-( houses[i]==prev_color ? 0 : 1),i+1,houses[i],dp));\n else{\n //check possible combinations after assigned each color\n for(int k = 1 ; k <= n ; k++){\n //grp count remains same if color is same as prev_color\n long int curr_cost = solve(houses,cost,m,n, target-( (k==prev_color)? 0:1 ) ,i+1,k, dp);\n if(curr_cost != INT_MAX)\n ans = min(ans,cost[i][k-1]+curr_cost);\n }\n }\n dp[i][target][prev_color] = ans;\n return ans; \n }\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, vector<int>(n + 1, -1)));\n long int res = solve(houses,cost,m,n,target,0,0,dp);\n return ( res == INT_MAX )? -1 : res; \n }\n};", "memory": "21719" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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][prev]!=-1) return dp[i][target][prev];\n if(houses[i]!=0) {\n if(i>0 && houses[i]!=houses[i-1]){\n return dp[i][target][prev]= help(houses,cost,m,n,target-1,i+1,dp,houses[i]);\n }else return dp[i][target][prev]= help(houses,cost,m,n,target,i+1,dp,houses[i]);\n }\n int ans=1e9;\n for(int color=1;color<=n;color++){\n houses[i]=color;\n if(i>0 && houses[i]!=houses[i-1]) ans=min(ans,cost[i][color-1]+help(houses,cost,m,n,target-1,i+1,dp,color));\n else ans=min(ans,cost[i][color-1]+help(houses,cost,m,n,target,i+1,dp,color));\n // dp[i][0][target]=min(dp[i][0][target],dp[i][houses[i]][target]);\n houses[i]=0;\n }\n return dp[i][target][prev]=ans;\n // for(auto i:houses) cout<<i<<\" \";cout<<ans<<endl;\n // return ans;\n\n }\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,vector<int>(n+1,-1)));\n int ans=help(houses,cost,m,n,target,0,dp,0);\n return ans>=1e9?-1:ans;\n }\n};", "memory": "21905" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 Solution {\n public:\n int minCost(vector<int>& houses, vvi& cost, int m, int n, int T) {\n vvvi mem(T + 1, vvi(m, vector<int>(n + 1)));\n const int c = mincost(houses, cost, m, n, T, 0, 0, mem);\n return c == INF ? -1 : c;\n }\n\n private:\n static constexpr int INF = 1000001;\n // min cost to paint houses[i..m) into k neighborhoods, s.t. \n // houses[i - 1] colors = prev_clr\n int mincost(const vector<int>& houses, const vvi& cost,\n const int& m, const int& n, int k, int i, int prev_clr,\n vvvi& mem) {\n if (i == m || k < 0) return k == 0 ? 0 : INF;\n if (mem[k][i][prev_clr] > 0) return mem[k][i][prev_clr];\n if (houses[i] > 0) return mincost(houses, cost, m, n, k - (prev_clr != houses[i]), i + 1, houses[i], mem);\n int res = INF;\n // paint houses[i] with color in [n]\n for (int clr = 1; clr <= n; ++clr) {\n res = min(res, cost[i][clr - 1] + mincost(houses, cost, m, n, k - (prev_clr != clr), i + 1, clr, mem));\n }\n return mem[k][i][prev_clr] = res;\n }\n};", "memory": "22091" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 }\n return INT_MAX;\n }\n if(dp[i][groups][prevcol]!=-1){\n return dp[i][groups][prevcol];\n }\n if(houses[i]!=0){\n \n if(houses[i]==prevcol){\n //\n return SolveByDP(houses,cost,m,n,target,i+1,groups,prevcol,dp);\n\n }\n else{\n //\n return SolveByDP(houses,cost,m,n,target,i+1,groups+1,houses[i],dp);\n }\n }\n int ans=INT_MAX;\n \n \n for(int j=0;j<n;j++){\n if(j+1==prevcol){\n int temp=SolveByDP(houses,cost,m,n,target,i+1,groups,prevcol,dp);\n if(temp!=INT_MAX){\n temp+=cost[i][j];\n }\n \n ans=min(ans,temp);\n }\n else{\n int temp=SolveByDP(houses,cost,m,n,target,i+1,groups+1,j+1,dp);\n if(temp!=INT_MAX){\n temp+=cost[i][j];\n }\n \n ans=min(ans,temp);\n }\n \n }\n \n return dp[i][groups][prevcol]=ans;\n }\n int SolveByRecursion(vector<int>& houses, vector<vector<int>>& cost, int &m, int &n, int &target,int i,int groups,int prevcol){\n if(groups > target)return INT_MAX;\n if(i>=houses.size()){\n if(groups==target){\n return 0;\n }\n return INT_MAX;\n }\n if(houses[i]!=0){\n \n if(houses[i]==prevcol){\n //\n return SolveByRecursion(houses,cost,m,n,target,i+1,groups,prevcol);\n\n }\n else{\n //\n return SolveByRecursion(houses,cost,m,n,target,i+1,groups+1,houses[i]);\n }\n }\n int ans=INT_MAX;\n \n \n for(int j=0;j<cost[i].size();j++){\n if(j+1==prevcol){\n int temp=SolveByRecursion(houses,cost,m,n,target,i+1,groups,prevcol);\n if(temp!=INT_MAX){\n temp+=cost[i][j];\n }\n \n ans=min(ans,temp);\n }\n else{\n int temp=SolveByRecursion(houses,cost,m,n,target,i+1,groups+1,j+1);\n if(temp!=INT_MAX){\n temp+=cost[i][j];\n }\n \n ans=min(ans,temp);\n }\n \n }\n \n return ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n \n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(target+1,vector<int>(n+1,-1)));\n int ans= SolveByDP(houses,cost,m,n,target,0,0,0,dp);\n if(ans==INT_MAX){\n return -1;\n }\n return ans;\n }\n};", "memory": "22278" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if(houses[ind]!=0){\n if(prev==houses[ind]){\n return solve(houses,cost,n,m,target,ind+1,prev,dp);\n }\n else{\n return solve(houses,cost,n,m,target-1,ind+1,houses[ind],dp);\n }\n }\n if(dp[target][ind][prev]!=-1){\n return dp[target][ind][prev];\n }\n\n int mini=1e9;\n for(int i=1;i<=n;i++){\n int ans=1e9;\n if(prev==i){\n ans=cost[ind][i-1]+solve(houses,cost,n,m,target,ind+1,prev,dp);\n }\n else{\n ans=cost[ind][i-1]+solve(houses,cost,n,m,target-1,ind+1,i,dp);\n }\n mini=min(ans,mini);\n }\n return dp[target][ind][prev]=mini;\n }\n\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<int>>>dp(target+1,vector<vector<int>>(m+1,vector<int>(n+1,-1)));\n\n\n return solve(houses,cost,n,m,target,0,0,dp)==1e9?-1:solve(houses,cost,n,m,target,0,0,dp);\n }\n};", "memory": "22464" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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) return 0; // All houses processed and exact neighborhoods\n return 1e7; // Target not met\n }\n\n if (dp[idx][target][prevClr + 1] != -1) return dp[idx][target][prevClr + 1];\n\n if (houses[idx] != 0) { // House is already painted\n if (houses[idx] == prevClr) {\n return dp[idx][target][prevClr + 1] = solve(houses, cost, clrs, target, idx + 1, houses[idx], dp);\n } else {\n return dp[idx][target][prevClr + 1] = solve(houses, cost, clrs, target - 1, idx + 1, houses[idx], dp);\n }\n }\n\n int mini = INT_MAX;\n for (int i = 0; i < clrs; i++) {\n int cst = cost[idx][i];\n if (i + 1 == prevClr) {\n mini = min(mini, cst + solve(houses, cost, clrs, target, idx + 1, i + 1, dp));\n } else {\n mini = min(mini, cst + solve(houses, cost, clrs, target - 1, idx + 1, i + 1, dp));\n }\n }\n return dp[idx][target][prevClr + 1] = mini;\n }\n\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, vector<int>(n + 2, -1)));\n int ans = solve(houses, cost, n, target, 0, -1, dp);\n if (ans >= 1e7) return -1;\n return ans;\n }\n};\n", "memory": "22650" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if(nbrhd == target)return 0;\n else return INT_MAX;\n }\n if(dp[i][nbrhd][prevCol] != -1) return dp[i][nbrhd][prevCol];\n int ans = INT_MAX;\n if(i == 0){\n if(houses[i] != 0) ans = solver(i+1, 1, houses[i], houses, cost, m, n, target);\n else{\n for(int col = 1; col <= n; col++){\n // houses[i] = col;\n int fetch = solver(i+1, 1, col, houses, cost, m, n, target);\n if(fetch != INT_MAX) ans = min(ans, cost[i][col-1] + fetch);\n // cout<<\"-\"<<i<<\" \"<<fetch<<endl;\n }\n }\n }\n else{\n if(houses[i] != 0) {\n if(prevCol == houses[i]) ans = solver(i+1, nbrhd, houses[i], houses, cost, m, n, target);\n else ans = solver(i+1, nbrhd + 1, houses[i], houses, cost, m, n, target);\n }\n else{\n for(int col = 1; col <= n; col++){\n // houses[i] = col;\n int fetch = col == prevCol ? solver(i+1, nbrhd, col, houses, cost, m, n, target) : solver(i+1, nbrhd + 1, col, houses, cost, m, n, target);\n // cout<<\"-\"<<i<<\" \"<<fetch<<endl;\n if(fetch != INT_MAX) ans = min(ans, cost[i][col-1] + fetch);\n }\n }\n }\n // cout<<ans<<endl;\n return dp[i][nbrhd][prevCol] = ans; \n }\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n dp = vector<vector<vector<int>>>(m+1, vector<vector<int>>(target + 1, vector<int>(n+2, -1)));\n int ans = solver(0, 0, 0, houses, cost, m, n, target);\n return ans == INT_MAX? -1:ans;\n }\n};", "memory": "22836" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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(dp[index][target][prev+1]!=-1) return dp[index][target][prev+1]; \n\n if(houses[index]!=0) return dp[index][target][prev+1]=solve(houses,cost,(houses[index]==prev?target:target-1),houses[index],index+1,dp);\n\n int ans=INT_MAX;\n\n for(int i=0;i<cost[index].size();i++){\n int colour=i+1;\n int price=cost[index][i];\n\n int recurans=solve(houses,cost,colour==prev?target:target-1,colour,index+1,dp);\n\n if(recurans!=INT_MAX) ans=min(ans,recurans+price);\n } \n return dp[index][target][prev+1]=ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n\n vector<vector<vector<int>>> dp(m,vector<vector<int>>(target+1,vector<int>(n+3,-1)));\n int ans= solve(houses, cost, target, -1,0,dp);\n return ans==INT_MAX?-1:ans;\n }\n};", "memory": "22836" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 return 1e9;\n }\n if(dp[ind][prev+1][target]!=-1){\n return dp[ind][prev+1][target];\n }\n int ans=1e9;\n //if house is already painted last summer:\n //if prev is not painted or painted with different colour:\n if(houses[ind]>0&&(prev==-1||houses[ind]!=prev)&&target>0){\n ans=solve(houses,cost,m,n,target-1,ind+1,houses[ind],dp);\n }\n else if(houses[ind]>0&&houses[ind]==prev){\n ans=solve(houses,cost,m,n,target,ind+1,houses[ind],dp);\n }\n //if house is not painted yet:\n if(houses[ind]==0){\n //possiblities of all paints:\n for(int j=0;j<n;j++){\n //paint the ind-th house with (j+1)-th paint:\n int res=1e9;\n if((prev==-1||(j+1)!=prev)&&target>0){\n res=cost[ind][j]+solve(houses,cost,m,n,target-1,\n ind+1,j+1,dp);\n }\n else if(j+1==prev){\n res=cost[ind][j]+solve(houses,cost,m,n,target,\n ind+1,j+1,dp);\n }\n ans=min(ans,res);\n }\n }\n return dp[ind][prev+1][target]=ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, \n int m, int n, int target) {\n int ind=0;\n int prev=-1;\n vector<vector<vector<int>>> dp(m+3,vector<vector<int>>(n+3,\n vector<int>(m+3,-1)));\n int ans=solve(houses,cost,m,n,target,ind,prev,dp);\n if(ans>=1e9){\n return -1;\n }\n return ans;\n }\n};", "memory": "23023" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 return 1e9;\n }\n if(dp[i][cnt][prev]!=-1)return dp[i][cnt][prev];\n if(house[i]){\n if(prev==house[i]){\n return solve(i+1,house,cost,target,cnt,prev,dp);\n }\n return solve(i+1,house,cost,target,cnt+1,house[i],dp);\n }\n int ans=1e9;\n for(int j=0;j<cost[i].size();j++){\n ans=min(ans,cost[i][j]+solve(i+1,house,cost,target,cnt+(prev!=(j+1)),j+1,dp)); \n }\n return dp[i][cnt][prev]= ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(m+1,vector<int>(n+1,-1)));\n int ans=solve(0,houses,cost,target,0,0,dp);\n return ans==1e9?-1:ans;\n }\n};", "memory": "23023" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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, cost, i + 1, target - (p != houses[i]), houses[i]);\n\n if(dp[i][target][p]) return dp[i][target][p]; \n int res = 1e6+1;\n for(int c=1; c<=cost[i].size(); c++) {\n res = min(res, cost[i][c-1] + util(houses, cost, i + 1, target - (p != c), c));\n }\n return dp[i][target][p] = res;\n }\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n dp.resize(m+1, vector<vector<int>>(m+1, vector<int>(n+1, 0)));\n int res = util(houses, cost, 0, target, 0);\n return res > 1e6 ? -1 : res;\n }\n};", "memory": "23209" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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) return dp[idx][count][prev];\n \n int minCost = 1e9;\n //If already painted\n if(houses[idx]){\n \n //if prev house is of different color, increase the count by 1\n if(houses[idx]!=prev) minCost = f(houses,cost,target,idx+1,count+1,houses[idx]);\n \n else minCost = f(houses,cost,target,idx+1,count,houses[idx]);\n \n }else{\n //we have to try all the colors\n for(int j=0;j<cost[0].size();j++){\n int tmp; //store the current cost\n \n //if prev house is of different color, increase the count by 1\n if((j+1)!=prev) tmp = cost[idx][j] + f(houses,cost,target,idx+1,count+1,j+1);\n \n else tmp = cost[idx][j] + f(houses,cost,target,idx+1,count,j+1);\n \n minCost = min(minCost,tmp);\n }\n }\n //store before returning\n return dp[idx][count][prev] = minCost;\n}\n\nint minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n dp.resize(m+1, vector<vector<int>>(m+1, vector<int>(n+1, -1)));\n int ans = f(houses,cost,target, 0, 0, 0);\n if (ans == 1e9) return -1;\n return ans;\n}\n};", "memory": "23395" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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) return dp[idx][count][prev];\n \n int minCost = 1e9;\n if (houses[idx]){\n if (houses[idx] != prev) minCost = f(houses, cost, target, idx+1, count+1, houses[idx]);\n else minCost = f(houses, cost, target, idx+1, count, houses[idx]);\n }\n else{\n for (int j=0; j<cost[idx].size(); j++){\n int tmp; \n if ((j+1)!=prev) tmp = cost[idx][j] + f(houses, cost, target, idx+1, count+1, j+1);\n else tmp = cost[idx][j] + f(houses, cost, target, idx+1, count, j+1);\n minCost = min(minCost, tmp);\n }\n }\n return dp[idx][count][prev] = minCost;\n}\n\nint minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n dp.resize(m+1, vector<vector<int>>(m+1, vector<int>(n+1, -1)));\n int ans = f(houses, cost, target, 0, 0, 0);\n if (ans == 1e9) return -1;\n return ans;\n}\n};", "memory": "23395" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 for(int j = min(i, t - 1); j >= 0; --j)\n {\n v[j][n + 1] = v[j][n + 2] = M;\n for(int c = 0; c < n; ++c)\n {\n if(A[i] && A[i] != c + 1)\n {\n v[j][c] = M;\n continue;\n }\n int p = (A[i] ? 0 : B[i][c]) +\n min(u[j][c], !j ? M : u[j - 1][n + 1 + (u[j - 1][n] == c)]);\n if(p <= v[j][n + 1])\n {\n v[j][n + 2] = v[j][n + 1];\n v[j][n + 1] = p;\n v[j][n] = c;\n }\n else if(p < v[j][n + 2])\n v[j][n + 2] = p;\n v[j][c] = p;\n }\n }\n }\n return v[t - 1][n + 1] < M ? v[t - 1][n + 1] : -1;\n }\n};", "memory": "23581" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 // for(int j = min(i, t - 1); j >= 0; --j)\n for(int j = 0; j < min(i + 1, t); ++j)\n {\n v[j][n + 1] = v[j][n + 2] = M;\n for(int c = 0; c < n; ++c)\n {\n if(A[i] && A[i] != c + 1)\n {\n v[j][c] = M;\n continue;\n }\n int p = (A[i] ? 0 : B[i][c]) +\n min(u[j][c], !j ? M : u[j - 1][n + 1 + (u[j - 1][n] == c)]);\n if(p <= v[j][n + 1])\n {\n v[j][n + 2] = v[j][n + 1];\n v[j][n + 1] = p;\n v[j][n] = c;\n }\n else if(p < v[j][n + 2])\n v[j][n + 2] = p;\n v[j][c] = p;\n }\n }\n }\n return v[t - 1][n + 1] < M ? v[t - 1][n + 1] : -1;\n }\n};", "memory": "23581" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 return 1e9;\n }\n if(dp[ind][target][prev] != -1)return dp[ind][target][prev];\n int ans = 1e9;\n if(houses[ind] != 0){\n if(houses[ind] == prev){\n ans = min(ans,helper(ind+1,cost,m,n,target,houses,houses[ind],dp));\n }\n else{\n ans = min(ans,helper(ind+1,cost,m,n,target-1,houses,houses[ind],dp));\n }\n }\n else{\n for(int i=1; i<=n; i++){\n if(i!=prev){\n ans = min(ans,helper(ind+1,cost,m,n,target-1,houses,i,dp)+cost[ind][i-1]);\n }\n else{\n ans = min(ans,helper(ind+1,cost,m,n,target,houses,i,dp)+cost[ind][i-1]);\n }\n }\n }\n return dp[ind][target][prev] = ans;\n }\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>>(m+1,vector<int>(n+2,-1)));\n int ans = helper(0,cost,m,n,target,houses,0,dp);\n if(ans == 1e9)return -1;\n return ans;\n }\n};", "memory": "23768" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 << \" n=\" << n << \" target=\" << target << endl;\n vector<vector<vector<long> > > dp(m, vector<vector<long> > (n, vector<long>(target + 1, -1)));\n if (houses[0] != 0) {\n int color = houses[0] - 1;\n dp[0][color][1] = 0;\n } else {\n for (int j = 0; j < n; j++) {\n dp[0][j][1] = cost[0][j];\n }\n }\n\n // for (int i = 0; i < n; i++) {\n // cout << dp[0][i][1] << \" \" ;\n // }\n // cout << endl;\n for (int i = 1; i < m; i++) {\n if (houses[i] != 0) {\n int color = houses[i] - 1;\n for (int j = 0; j < n; j++) {\n for (int t = 0; t < target; t++) {\n if (color == j) {\n if (dp[i - 1][j][t + 1] == -1) {\n continue;\n }\n // cout << \"same \" << i << \" \" << color << \" \" << (t + 1) << endl;\n if (dp[i][color][t + 1] == -1) {\n dp[i][color][t + 1] = INT_MAX;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t + 1]);\n // dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t + 1] + cost[i][color]);\n // cout << dp[i - 1][j][t + 1] << \" \" << cost[i][color] << \" \" << dp[i][color][t + 1] << endl;\n } else {\n if (dp[i - 1][j][t] == -1) {\n continue;\n }\n // cout << \"not same \" << i << \" \" << color << \" \" << (t + 1) << endl;\n if (dp[i][color][t + 1] == -1) {\n dp[i][color][t + 1] = INT_MAX;\n }\n // dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t] + cost[i][color]);\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t]);\n // cout << dp[i][color][t + 1] << endl;\n }\n }\n // dp[i][color][t + 1] = min(dp[i - 1][color][t + 1], dp[i - 1][not color][t])\n }\n } else {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n for (int t = 0; t < target; t++) {\n if (j == k) {\n if (i != 0 && dp[i - 1][j][t + 1] == -1) {\n continue;\n }\n if (dp[i][j][t + 1] == -1) {\n dp[i][j][t + 1] = INT_MAX;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t + 1] + cost[i][j]);\n } else {\n if (i != 0 && dp[i - 1][k][t] == -1) {\n continue;\n }\n if (dp[i][j][t + 1] == -1) {\n dp[i][j][t + 1] = INT_MAX;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t] + cost[i][j]);\n }\n }\n\n }\n }\n }\n }\n long res = INT_MAX;\n for (int i = 0; i < n; i++) {\n // cout << \"res[\" << i << \"] is \" << dp[m - 1][i][target] << endl;\n if (dp[m - 1][i][target] == -1) {\n continue;\n }\n res = min(res, dp[m - 1][i][target]);\n }\n if (res == INT_MAX) {\n return -1;\n }\n return res;\n }\n};", "memory": "23954" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 dp[0][color][1] = 0;\n } else {\n for (int j = 0; j < n; j++) {\n dp[0][j][1] = cost[0][j];\n }\n }\n\n for (int i = 1; i < m; i++) {\n if (houses[i] != 0) {\n int color = houses[i] - 1;\n for (int j = 0; j < n; j++) {\n for (int t = 0; t < target; t++) {\n if (color == j) {\n if (dp[i - 1][j][t + 1] == INT_MAX) {\n continue;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][color][t + 1]);\n } else {\n if (dp[i - 1][j][t] == INT_MAX) {\n continue;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t]);\n }\n }\n }\n } else {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n for (int t = 0; t < target; t++) {\n if (j == k) {\n if (dp[i - 1][j][t + 1] == INT_MAX) {\n continue;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t + 1] + cost[i][j]);\n } else {\n if (dp[i - 1][k][t] == INT_MAX) {\n continue;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t] + cost[i][j]);\n }\n }\n\n }\n }\n }\n }\n long res = INT_MAX;\n for (int i = 0; i < n; i++) {\n if (dp[m - 1][i][target] == -1) {\n continue;\n }\n res = min(res, dp[m - 1][i][target]);\n }\n if (res == INT_MAX) {\n return -1;\n }\n return res;\n }\n};", "memory": "24140" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 << \" n=\" << n << \" target=\" << target << endl;\n vector<vector<vector<long> > > dp(m, vector<vector<long> > (n, vector<long>(target + 1, -1)));\n if (houses[0] != 0) {\n int color = houses[0] - 1;\n dp[0][color][1] = 0;\n } else {\n for (int j = 0; j < n; j++) {\n dp[0][j][1] = cost[0][j];\n }\n }\n\n // for (int i = 0; i < n; i++) {\n // cout << dp[0][i][1] << \" \" ;\n // }\n // cout << endl;\n for (int i = 1; i < m; i++) {\n if (houses[i] != 0) {\n int color = houses[i] - 1;\n for (int j = 0; j < n; j++) {\n for (int t = 0; t < target; t++) {\n if (color == j) {\n if (dp[i - 1][j][t + 1] == -1) {\n continue;\n }\n // cout << \"same \" << i << \" \" << color << \" \" << (t + 1) << endl;\n if (dp[i][color][t + 1] == -1) {\n dp[i][color][t + 1] = INT_MAX;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t + 1]);\n // dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t + 1] + cost[i][color]);\n // cout << dp[i - 1][j][t + 1] << \" \" << cost[i][color] << \" \" << dp[i][color][t + 1] << endl;\n } else {\n if (dp[i - 1][j][t] == -1) {\n continue;\n }\n // cout << \"not same \" << i << \" \" << color << \" \" << (t + 1) << endl;\n if (dp[i][color][t + 1] == -1) {\n dp[i][color][t + 1] = INT_MAX;\n }\n // dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t] + cost[i][color]);\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t]);\n // cout << dp[i][color][t + 1] << endl;\n }\n }\n // dp[i][color][t + 1] = min(dp[i - 1][color][t + 1], dp[i - 1][not color][t])\n }\n } else {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n for (int t = 0; t < target; t++) {\n if (j == k) {\n if (i != 0 && dp[i - 1][j][t + 1] == -1) {\n continue;\n }\n if (dp[i][j][t + 1] == -1) {\n dp[i][j][t + 1] = INT_MAX;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t + 1] + cost[i][j]);\n } else {\n if (i != 0 && dp[i - 1][k][t] == -1) {\n continue;\n }\n if (dp[i][j][t + 1] == -1) {\n dp[i][j][t + 1] = INT_MAX;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t] + cost[i][j]);\n }\n }\n\n }\n }\n }\n }\n long res = INT_MAX;\n for (int i = 0; i < n; i++) {\n // cout << \"res[\" << i << \"] is \" << dp[m - 1][i][target] << endl;\n if (dp[m - 1][i][target] == -1) {\n continue;\n }\n res = min(res, dp[m - 1][i][target]);\n }\n if (res == INT_MAX) {\n return -1;\n }\n return res;\n }\n};", "memory": "24326" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 dp[0][color][1] = 0;\n } else {\n for (int j = 0; j < n; j++) {\n dp[0][j][1] = cost[0][j];\n }\n }\n\n for (int i = 1; i < m; i++) {\n if (houses[i] != 0) {\n int color = houses[i] - 1;\n for (int j = 0; j < n; j++) {\n for (int t = 0; t < target; t++) {\n if (color == j) {\n if (dp[i - 1][j][t + 1] == INT_MAX) {\n continue;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][color][t + 1]);\n } else {\n if (dp[i - 1][j][t] == INT_MAX) {\n continue;\n }\n dp[i][color][t + 1] = min(dp[i][color][t + 1], dp[i - 1][j][t]);\n }\n }\n }\n } else {\n for (int j = 0; j < n; j++) {\n for (int k = 0; k < n; k++) {\n for (int t = 0; t < target; t++) {\n if (j == k) {\n if (dp[i - 1][j][t + 1] == INT_MAX) {\n continue;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t + 1] + cost[i][j]);\n } else {\n if (dp[i - 1][k][t] == INT_MAX) {\n continue;\n }\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t] + cost[i][j]);\n }\n }\n\n }\n }\n }\n }\n long res = INT_MAX;\n for (int i = 0; i < n; i++) {\n if (dp[m - 1][i][target] == -1) {\n continue;\n }\n res = min(res, dp[m - 1][i][target]);\n }\n if (res == INT_MAX) {\n return -1;\n }\n return res;\n }\n};", "memory": "24326" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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(dp[i - 1][x][k - 1]),\n // min[i - 1][j][k]) + cost[i][j], \n vector<vector<vector<int>>> dp(m + 1, vector<vector<int>>(n + 1, vector<int>(target + 1, 1e9)));\n \n for(int j = 0; j <= n; ++j) {\n dp[0][j][0] = 0;\n }\n for(int h = 1; h <= target; ++h) {\n for(int i = 1; i <= m; ++i) {\n \n int min1 = n, min2 = n;\n if(i == 1 || houses[i - 2] == 0) {\n // find two best costs of painting up to i-1 th house\n priority_queue<pair<int, int>> pq;\n for(int j = 0; j < n; ++j) {\n if(pq.size() < 2) {\n pq.push({dp[i - 1][j][h - 1], j});\n } else if(pq.top().first > dp[i - 1][j][h - 1]) {\n pq.pop();\n pq.push({dp[i - 1][j][h - 1], j});\n }\n }\n min1 = pq.top().second;\n pq.pop();\n if(!pq.empty())\n min2 = pq.top().second;\n else\n min2 = n;\n \n } else {\n min1 = min2 = houses[i - 2] - 1;\n }\n // if already painted\n if(houses[i - 1] > 0) {\n int color = houses[i - 1] - 1;\n dp[i][color][h] = min({dp[i - 1][min1][h - (min1 != color)],\n dp[i - 1][min2][h - (min2 != color)],\n dp[i - 1][color][h]});\n } else {\n for(int j = 0; j < n; ++j) {\n dp[i][j][h] = min({dp[i - 1][min1][h - (j != min1)],\n dp[i - 1][min2][h - (j != min2)],\n dp[i - 1][j][h]}) + cost[i - 1][j];\n }\n }\n }\n }\n int ans = 1e9;\n for(int j = 0; j < n; ++j) {\n ans = min(ans, dp[m][j][target]);\n }\n \n return ans == 1e9 ? -1 : ans;\n }\n};", "memory": "24513" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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, vector<int>(n, infty)));\n for (int color = 1; color <= n; ++color) {\n if (houses[0] == 0)\n dp[0][1][color-1] = cost[0][color-1];\n else if (houses[0] == color)\n dp[0][1][color-1] = 0;\n }\n for (int house = 1; house < m; ++house) {\n for (int nbrhood = 1; nbrhood <= min(target, house+1); ++nbrhood) {\n // find cheapest two colors to color previous house\n int diff1 = 1; \n int diff2 = 2;\n vector<int> prev = dp[house-1][nbrhood-1];\n if (prev[diff1-1] > prev[diff2-1]) {\n diff1 = 2;\n diff2 = 1;\n }\n for (int color = 3; color <= n; ++color) {\n if (prev[color-1] < prev[diff1-1]) {\n diff2 = diff1;\n diff1 = color;\n }\n else if (prev[color-1] < prev[diff2-1])\n diff2 = color;\n }\n for (int color = 1; color <= n; ++color) {\n if (houses[house] && houses[house] != color)\n continue;\n int same_nbr_cost = dp[house-1][nbrhood][color-1];\n int diff_nbr_cost = diff1 == color ? prev[diff2-1] : prev[diff1-1];\n int curr_cost = houses[house] > 0 ? 0 : cost[house][color-1];\n dp[house][nbrhood][color-1] = curr_cost + min(same_nbr_cost, diff_nbr_cost);\n }\n }\n }\n int res = *min_element(dp[m-1][target].begin(), dp[m-1][target].end());\n return res < infty ? res : -1;\n }\n};", "memory": "24699" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 && self, int currentindex, int lastcolor,\n int target)->long long {\n if (currentindex == m && target == 1) {\n return 0;\n }\n if (target <= 0||currentindex==m) {\n return INT_MAX;\n }\n if (dynamic[currentindex][lastcolor][target] != -1) {\n return dynamic[currentindex][lastcolor][target];\n }\n long long count = INT_MAX;\n if (houses[currentindex] != 0) {\n if (houses[currentindex] == lastcolor) {\n count = self(self, currentindex + 1, lastcolor, target);\n } else {\n count = self(self, currentindex + 1, houses[currentindex],\n target - 1);\n }\n dynamic[currentindex][lastcolor][target]=count;\n return dynamic[currentindex][lastcolor][target];\n }\n\n for (int j = 0; j < n; j++) {\n\n if (lastcolor == j+1) {\n count = min(count,(long long) cost[currentindex][j]+self(self, currentindex + 1, j+1, target));\n\n } else {\n count =\n min(count,(long long)cost[currentindex][j]+self(self, currentindex + 1, j+1, target - 1));\n }\n }\n dynamic[currentindex][lastcolor][target]=count;\n return dynamic[currentindex][lastcolor][target];\n };\n if(houses[0]!=0){\n long long ans=dp(dp,1,houses[0],target);\n if(ans>=INT_MAX){\n return -1;\n }\n else{\n return ans;\n }\n }\n else {\n long long mint =LLONG_MAX;\n for(int j=0;j<n;j++){\n mint=min(cost[0][j]+ dp(dp,1,j+1,target),mint);\n }\n if(mint>=INT_MAX){\n return -1;\n }\n else{\n return mint;\n }\n \n }\n \n }\n};", "memory": "24885" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 && self, int currentindex, int lastcolor,\n int target)->long long {\n if (currentindex == m && target == 1) {\n return 0;\n }\n if (target <= 0||currentindex==m) {\n return INT_MAX;\n }\n if (dynamic[currentindex][lastcolor][target] != -1) {\n return dynamic[currentindex][lastcolor][target];\n }\n long long count = INT_MAX;\n if (houses[currentindex] != 0) {\n if (houses[currentindex] == lastcolor) {\n count = self(self, currentindex + 1, lastcolor, target);\n } else {\n count = self(self, currentindex + 1, houses[currentindex],\n target - 1);\n }\n dynamic[currentindex][lastcolor][target]=count;\n return dynamic[currentindex][lastcolor][target];\n }\n\n for (int j = 0; j < n; j++) {\n\n if (lastcolor == j+1) {\n count = min(count,(long long) cost[currentindex][j]+self(self, currentindex + 1, j+1, target));\n\n } else {\n count =\n min(count,(long long)cost[currentindex][j]+self(self, currentindex + 1, j+1, target - 1));\n }\n }\n dynamic[currentindex][lastcolor][target]=count;\n return dynamic[currentindex][lastcolor][target];\n };\n if(houses[0]!=0){\n long long ans=dp(dp,1,houses[0],target);\n if(ans>=INT_MAX){\n return -1;\n }\n else{\n return ans;\n }\n }\n else {\n long long mint =LLONG_MAX;\n for(int j=0;j<n;j++){\n mint=min(cost[0][j]+ dp(dp,1,j+1,target),mint);\n }\n if(mint>=INT_MAX){\n return -1;\n }\n else{\n return mint;\n }\n \n }\n \n }\n};", "memory": "25071" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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_MAX;\n }\n if(index>=n){\n return INT_MAX;\n }\n if(dp[index][prev][target]!=-1){\n return dp[index][prev][target];\n }\n if(house[index]!=0){\n if(prev==house[index]){\n return solve(house,cost,target,prev,index+1,dp);\n }\n else{\n return solve(house,cost,target-1,house[index],index+1,dp);\n }\n }\n long long ans=INT_MAX;\n for(int i=0;i<m;i++){\n if(prev==i+1){\n ans=min(ans,cost[index][i]+solve(house,cost,target,prev,index+1,dp));\n }\n else{\n ans=min(ans,cost[index][i]+solve(house,cost,target-1,i+1,index+1,dp));\n }\n }\n return dp[index][prev][target]=ans;\n}\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<vector<long long>>(n+1,vector<long long>(target+1,-1)));\n int ans=solve(houses,cost,target,0,0,dp);\n if(ans==INT_MAX){\n return -1;\n }\n return ans;\n }\n};", "memory": "25258" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 long res = INT_MAX;\n\n for (int house = 1; house <= m; ++house) {\n for (int prevColor = 1; prevColor <= n; ++prevColor) {\n for (int color = 1; color <= n; ++color) {\n if (houses[house - 1] && color != houses[house - 1]) {\n continue;\n }\n\n for (int neighborhoods = 1; neighborhoods <= std::min(target, house); ++neighborhoods) {\n\n long long val = houses[house - 1] ? 0 : cost[house - 1][color - 1];\n\n if (prevColor == color) {\n val += dp[house - 1][prevColor][neighborhoods]; \n } else {\n val += dp[house - 1][prevColor][neighborhoods - 1]; \n }\n\n dp[house][color][neighborhoods] = std::min(\n dp[house][color][neighborhoods],\n val\n );\n\n if (target == neighborhoods && house == m) {\n res = std::min(res, dp[house][color][neighborhoods]);\n }\n }\n }\n }\n }\n\n\n return res == INT_MAX ? -1 : res; \n }\n};", "memory": "26003" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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<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 long res = INT_MAX;\n\n for (int house = 1; house <= m; ++house) {\n for (int prevColor = 1; prevColor <= n; ++prevColor) {\n for (int color = 1; color <= n; ++color) {\n if (houses[house - 1] && color != houses[house - 1]) {\n continue;\n }\n\n for (int neighborhoods = 1; neighborhoods <= std::min(target, house); ++neighborhoods) {\n\n long long val = houses[house - 1] ? 0 : cost[house - 1][color - 1];\n\n if (prevColor == color) {\n val += dp[house - 1][prevColor][neighborhoods]; \n } else {\n val += dp[house - 1][prevColor][neighborhoods - 1]; \n }\n\n dp[house][color][neighborhoods] = std::min(\n dp[house][color][neighborhoods],\n val\n );\n\n if (target == neighborhoods && house == m) {\n res = std::min(res, dp[house][color][neighborhoods]);\n }\n }\n }\n }\n }\n\n\n return res == INT_MAX ? -1 : res; \n }\n};", "memory": "26189" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if(cnt == target) return 0;\n else return 1e9;\n }\n if(dp[i][last + 1][cnt] != -1) return dp[i][last + 1][cnt];\n\n int ans;\n int mini = 1e9;\n for(int j = 0;j<n;j++){\n if(houses[i] == 0){\n if(j == last) ans = cost[i][j] + solve(i + 1, last, cnt, houses, cost, m, n, target, dp);\n else ans = cost[i][j] + solve(i + 1, j, cnt + 1, houses, cost, m, n, target, dp);\n }\n else if(last == houses[i] - 1) ans = solve(i + 1, houses[i] - 1, cnt, houses, cost, m, n, target, dp);\n else ans = solve(i + 1, houses[i] - 1, cnt + 1, houses, cost, m, n, target, dp);\n mini = min(mini, ans);\n }\n return dp[i][last + 1][cnt] = mini;\n }\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>(101,-1)));\n int ans = solve(0, -1, 0, houses, cost, m, n, target, dp);\n if(ans == 1e9) return -1;\n else return ans;\n }\n};", "memory": "26375" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 else return 1e9;\n }\n if(dp[i][last + 1][cnt] != -1) return dp[i][last + 1][cnt];\n\n int ans;\n int mini = 1e9;\n for(int j = 0;j<n;j++){\n if(houses[i] == 0){\n if(j == last) ans = cost[i][j] + solve(i + 1, last, cnt, houses, cost, m, n, target, dp);\n else ans = cost[i][j] + solve(i + 1, j, cnt + 1, houses, cost, m, n, target, dp);\n }\n else if(last == houses[i] - 1) ans = solve(i + 1, houses[i] - 1, cnt, houses, cost, m, n, target, dp);\n else ans = solve(i + 1, houses[i] - 1, cnt + 1, houses, cost, m, n, target, dp);\n mini = min(mini, ans);\n }\n return dp[i][last + 1][cnt] = mini;\n }\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>(101,-1)));\n int ans = solve(0, -1, 0, houses, cost, m, n, target, dp);\n if(ans == 1e9) return -1;\n else return ans;\n }\n};", "memory": "26561" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if (dp[ind][prevColor + 1][target] != -1) return dp[ind][prevColor + 1][target];\n\n ll ans = INT_MAX;\n\n if (h[ind] != 0) {\n int newTarget = target - (prevColor != h[ind]);\n return dp[ind][prevColor + 1][target] = solve(ind + 1, h[ind], newTarget);\n } \n\n for (int i = 1; i <= n; i++) { \n int newTarget = target - (prevColor != i);\n ll currCost = 1LL*c[ind][i - 1] + solve(ind + 1, i, newTarget);\n ans = min(ans, currCost); \n }\n return dp[ind][prevColor + 1][target] = ans; \n }\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n h = houses, c = cost;\n this->n = n;\n this->m = m;\n dp.resize(m, vector<vector<ll>>(n + 2, vector<ll>(target + 1, -1))); \n int res = solve(0, -1, target);\n return res == INT_MAX ? -1 : res;\n }\n};\n", "memory": "26748" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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 if (dp[ind][prevColor + 1][target] != -1) return dp[ind][prevColor + 1][target];\n\n ll ans = INT_MAX;\n\n if (h[ind] != 0) {\n int newTarget = target - (prevColor != h[ind]);\n return dp[ind][prevColor + 1][target] = solve(ind + 1, h[ind], newTarget);\n } \n\n for (int i = 1; i <= n; i++) { \n int newTarget = target - (prevColor != i);\n if(newTarget < 0) continue;\n ll currCost = 1LL*c[ind][i - 1] + solve(ind + 1, i, newTarget);\n ans = min(ans, currCost); \n }\n return dp[ind][prevColor + 1][target] = ans; \n }\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n h = houses, c = cost;\n this->n = n;\n this->m = m;\n dp.resize(m, vector<vector<ll>>(n + 2, vector<ll>(target + 1, -1))); \n int res = solve(0, -1, target);\n return res == INT_MAX ? -1 : res;\n }\n};\n", "memory": "26748" }
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 are painted with the same color.</p> <ul> <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> </ul> <p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> <ul> <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> </ul> <p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 9 <strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 <strong>Output:</strong> 11 <strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. Cost of paint the first and last house (10 + 1) = 11. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3 <strong>Output:</strong> -1 <strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == houses.length == cost.length</code></li> <li><code>n == cost[i].length</code></li> <li><code>1 &lt;= m &lt;= 100</code></li> <li><code>1 &lt;= n &lt;= 20</code></li> <li><code>1 &lt;= target &lt;= m</code></li> <li><code>0 &lt;= houses[i] &lt;= n</code></li> <li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li> </ul>
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)return INT_MAX;\n if (dp[i][prev][t]!=-1)return dp[i][prev][t];\n if (houses[i]!=0){\n if (houses[i]!=prev)\n return dp[i][prev][t]=func(i+1,houses[i],t+1,houses,cost,m,n,target,dp);\n else return dp[i][prev][t]=func(i+1,houses[i],t,houses,cost,m,n,target,dp);\n }\n else {\n long long ans=INT_MAX;\n for (int j=0;j<cost[i].size();j++){\n if (j+1==prev&&prev!=0){\n ans=min(ans,cost[i][j]+func(i+1,prev,t,houses,cost,m,n,target,dp));\n }\n else {\n ans=min(ans,cost[i][j]+func(i+1,j+1,t+1,houses,cost,m,n,target,dp));\n }\n }\n return dp[i][prev][t]=ans;\n }\n }\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 <vector <long long > > (n+2,vector <long long> (target+2,-1)));\n if ( func(0,0,0,houses,cost,m,n,target,dp)==INT_MAX)return -1;\n return func(0,0,0,houses,cost,m,n,target,dp);\n }\n};", "memory": "26934" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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 = max_rob;\n }\n\n return max_rob;\n }\n};", "memory": "9000" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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 pick += prev2; \n \n int nonPick = prev; \n \n int cur_i = max(pick, nonPick); \n prev2 = prev; \n prev = cur_i; \n }\n \n return prev; \n }\n \npublic:\n int rob(vector<int>& nums) {\n int n = nums.size();\n if (n == 1) return nums[0];\n \n \n \n return solve(nums);\n }\n};\n", "memory": "9100" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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>& a) {\n \n memset(t,-1,sizeof(t));\n return dp(0,a);\n }\n};", "memory": "9100" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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,second);\n one=second;\n second=nums[i];\n }\n return second;\n }\n};", "memory": "9200" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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]=max(steal,skip);\n }\n int rob(vector<int>& nums) {\n int n=nums.size();\n memset(t,-1,sizeof(t));\n return solve(nums,0,n);\n }\n};", "memory": "9200" }
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 into on the same night</b>.</p> <p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1] <strong>Output:</strong> 12 <strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>0 &lt;= nums[i] &lt;= 400</code></li> </ul>
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n // vector<int> rightSideView(TreeNode* root) {\n // vector<int> answer;\n // if(root ==NULL)\n // {return answer;}\n // queue<TreeNode*> q;\n // q.push(root);\n // while(!q.empty())\n // {\n // int s = q.size();\n // vector<int> v;\n // for(int i=0; i<s; i++)\n // {\n // if(q.front()->left != NULL)\n // {q.push(q.front()->left);}\n // if(q.front()->right != NULL)\n // {q.push(q.front()->right);}\n // v.push_back(q.front()->val);\n // q.pop();\n // }\n // answer.push_back(v[s-1]);\n // }\n // return answer;\n // }\n\n vector<int> ans;\n\n void inorder(TreeNode* node, int level) {\n if (!node) {\n return;\n }\n\n if(ans.size() == level) {\n ans.push_back(node->val);\n }\n inorder(node->right, level+1);\n inorder(node->left, level+1);\n }\n\n vector<int> rightSideView(TreeNode* root) {\n if(!root) return ans;\n inorder(root, 0);\n root->left=NULL;\n root->right=NULL;\n return ans;\n }\n};", "memory": "13300" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n // vector<int> rightSideView(TreeNode* root) {\n // vector<int> answer;\n // if(root ==NULL)\n // {return answer;}\n // queue<TreeNode*> q;\n // q.push(root);\n // while(!q.empty())\n // {\n // int s = q.size();\n // vector<int> v;\n // for(int i=0; i<s; i++)\n // {\n // if(q.front()->left != NULL)\n // {q.push(q.front()->left);}\n // if(q.front()->right != NULL)\n // {q.push(q.front()->right);}\n // v.push_back(q.front()->val);\n // q.pop();\n // }\n // answer.push_back(v[s-1]);\n // }\n // return answer;\n // }\n\n vector<int> ans;\n\n void inorder(TreeNode* node, int level) {\n if (!node) {\n return;\n }\n\n if(ans.size() == level) {\n ans.push_back(node->val);\n }\n inorder(node->right, level+1);\n inorder(node->left, level+1);\n }\n\n vector<int> rightSideView(TreeNode* root) {\n if(!root) return ans;\n inorder(root, 0);\n root->left=NULL;\n root->right=NULL;\n return ans;\n }\n};", "memory": "13400" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n//class Solution {\n//public:\n// vector<int> rightSideView(TreeNode* root) {\n// vector<int> v1;\n// vector<int> v2;\n// vector<int> v;\n// if(root == nullptr) return v;\n// else{\n// v.push_back(root->val);\n// v1 = rightSideView(root->left);\n// v2 = rightSideView(root->right);\n// for(int i = 0; i<v2.size(); i++){\n// v.push_back(v2[i]);\n// }\n// if(v2.size()<v1.size()){\n// for(int i=v2.size(); i<v1.size(); i++){\n// v.push_back(v1[i]);\n// }\n// }\n// return v;\n// }\n// }\n// };\nclass Solution {\npublic:\n \n // vector<int> rightSideView(TreeNode* root) {\n // vector<int> answer;\n // if(root ==NULL)\n // {return answer;}\n // queue<TreeNode*> q;\n // q.push(root);\n // while(!q.empty())\n // {\n // int s = q.size();\n // vector<int> v;\n // for(int i=0; i<s; i++)\n // {\n // if(q.front()->left != NULL)\n // {q.push(q.front()->left);}\n // if(q.front()->right != NULL)\n // {q.push(q.front()->right);}\n // v.push_back(q.front()->val);\n // q.pop();\n // }\n // answer.push_back(v[s-1]);\n // }\n // return answer;\n // }\n\n vector<int> ans;\n\n void inorder(TreeNode* node, int level) {\n if (!node) {\n return;\n }\n\n if(ans.size() == level) {\n ans.push_back(node->val);\n }\n inorder(node->right, level+1);\n inorder(node->left, level+1);\n }\n\n vector<int> rightSideView(TreeNode* root) {\n if(!root) return ans;\n inorder(root, 0);\n root->left=NULL;\n root->right=NULL;\n return ans;\n }\n};", "memory": "13500" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n // vector<int> rightSideView(TreeNode* root) {\n // vector<int> answer;\n // if(root ==NULL)\n // {return answer;}\n // queue<TreeNode*> q;\n // q.push(root);\n // while(!q.empty())\n // {\n // int s = q.size();\n // vector<int> v;\n // for(int i=0; i<s; i++)\n // {\n // if(q.front()->left != NULL)\n // {q.push(q.front()->left);}\n // if(q.front()->right != NULL)\n // {q.push(q.front()->right);}\n // v.push_back(q.front()->val);\n // q.pop();\n // }\n // answer.push_back(v[s-1]);\n // }\n // return answer;\n // }\n\n vector<int> ans;\n\n void inorder(TreeNode* node, int level) {\n if (!node) {\n return;\n }\n\n if(ans.size() == level) {\n ans.push_back(node->val);\n }\n inorder(node->right, level+1);\n inorder(node->left, level+1);\n }\n\n vector<int> rightSideView(TreeNode* root) {\n if(!root) return ans;\n inorder(root, 0);\n root->left=NULL;\n root->right=NULL;\n return ans;\n }\n};", "memory": "13600" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n if(root==NULL) return {};\n vector<int> v;\n queue<TreeNode*> s;\n s.push(root);\n TreeNode* r = root;\n while(!s.empty()){\n int i = s.size(); bool flag=true;\n for(int j=0;j<i;j++){\n r=s.front(); s.pop();\n if(r==NULL) continue;\n if(flag) {\n v.push_back(r->val); flag = false;\n }\n if(r->right!=NULL) s.push(r->right);\n if(r->left!=NULL) s.push(r->left);\n }\n }\n root->left=root->right=NULL;\n root=NULL;\n free(root);\n return v;\n }\n};", "memory": "13700" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n if(root==NULL) return {};\n vector<int> v;\n queue<TreeNode*> s;\n s.push(root);\n TreeNode* r = root;\n while(!s.empty()){\n int i = s.size(); bool flag=true;\n for(int j=0;j<i;j++){\n r=s.front(); s.pop();\n if(r==NULL) continue;\n if(flag) {\n v.push_back(r->val); flag = false;\n }\n if(r->right!=NULL) s.push(r->right);\n if(r->left!=NULL) s.push(r->left);\n }\n }\n root->left=root->right=NULL;\n root=NULL;\n free(root);\n return v;\n }\n};", "memory": "13700" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
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==n){\n // v[curr]=root->val; //bar bar update hoyegi\n // return;\n // } \n // nthLevel(root->left,v,curr+1,n);\n // nthLevel(root->right,v,curr+1,n);\n // } \n // vector<int> rightSideView(TreeNode* root) {\n // int n=levels(root);\n // vector<int>v(n,0);\n // for(int i=0;i<n;i++){\n // nthLevel(root,v,0,i);\n // }\n // return v;\n void preorder(TreeNode* root,int level,vector<int>&v){\n if(root==NULL)return;\n v[level]=root->val;\n preorder(root->left,level+1,v);\n preorder(root->right,level+1,v);\n }\n vector<int> rightSideView(TreeNode* root){\n int n=levels(root);\n vector<int>v(n,0);\n preorder(root,0,v);\n return v;\n }\n};", "memory": "13800" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n vector<int>ans;\n dfs(root,0,ans);\n return ans;\n }\n private:\n void dfs(TreeNode* root, int depth, vector<int>& ans){\n if(root==nullptr)\n return;\n \n if(depth==ans.size())\n ans.push_back(root->val);\n dfs(root->right, depth+1 , ans);\n dfs(root->left, depth+1 , ans);\n }\n \n};", "memory": "13900" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void func(TreeNode * root, vector<int> &ans, int level){\n if(root==NULL) return ;\n if(ans.size()==level) ans.push_back(root->val);\n func(root->right,ans, level+1 );\n func(root->left, ans, level+1);\n }\n \n vector<int> rightSideView(TreeNode* root) {\n vector<int> ans;\n int level=0;\n func(root, ans, level);\n return ans;\n }\n};", "memory": "14000" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void fan(TreeNode * root , vector <int> & ans,vector <TreeNode*> &help){\n if(root == nullptr) return ;\n ans.push_back(help[help.size()-1]->val);\n int s= help.size();\n while(s> 0){\n s--;\n TreeNode * temp = help[0];\n help.erase(help.begin());\n if(temp->left ) help.push_back(temp->left);\n if(temp->right) help.push_back(temp->right);\n }\n if(help.size()>0) fan(root,ans,help);\n }\n vector<int> rightSideView(TreeNode* root) {\n vector <int> ans;\n vector <TreeNode*> help;\n help.push_back(root);\n fan(root,ans,help);\n return ans;\n }\n};", "memory": "14000" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n\n\n void helper(TreeNode*root , int level , vector<int>&rsv){\n\n if(!root) return;\n\n\n if(rsv.size() == level){\n rsv.push_back(root->val);\n }\n\n helper(root->right , level + 1 , rsv);\n\n helper(root->left , level + 1 ,rsv);\n\n\n\n }\n\n\n\npublic:\n vector<int> rightSideView(TreeNode* root) {\n\n \n vector<int>rsv;\n\n helper(root , 0 , rsv);\n return rsv;\n }\n};", "memory": "14100" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void func(TreeNode * root, vector<int> &ans, int level){\n if(root==NULL) return ;\n if(ans.size()==level) ans.push_back(root->val);\n func(root->right,ans, level+1 );\n func(root->left, ans, level+1);\n }\n \n vector<int> rightSideView(TreeNode* root) {\n vector<int> ans;\n int level=0;\n func(root, ans, level);\n return ans;\n }\n};", "memory": "14100" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void fun(TreeNode* root,vector<int>&v,int level){\n if(!root) return;\n if(v.size()==level)\n v.push_back(root->val);\n\n fun(root->right,v,level+1);\n fun(root->left,v,level+1);\n }\n vector<int> rightSideView(TreeNode* root) {\n vector<int>v;\n if(!root) return {};\n\n fun(root,v,0);\n return v;\n }\n};", "memory": "14200" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void visitTree(TreeNode *node, int level, vector<int> &view) {\n if (view.size() <= level) {\n view.push_back(node->val);\n }\n if (node->right != NULL) visitTree(node->right, level+1, view);\n if (node->left != NULL) visitTree(node->left, level+1, view);\n }\n\n vector<int> rightSideView(TreeNode* root) {\n vector<int> right_view;\n\n if (root == NULL) {\n return right_view;\n }\n\n visitTree(root, 0, right_view);\n\n\n return right_view;\n }\n};", "memory": "14200" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n\n TreeNode* fun(TreeNode* root, int cnt) {\n if (root == NULL) {\n return NULL;\n }\n\n cnt++;\n cout<<cnt<<endl;\n\n if (cnt == ans.size())\n {\n if (root->right)\n {\n ans.push_back(root->right->val);\n }\n else if (root->left)\n {\n ans.push_back(root->left->val);\n }\n }\n\n fun(root->right, cnt);\n fun(root->left, cnt);\n\n return root;\n }\n\n vector<int> rightSideView(TreeNode* root) {\n if (root == NULL) {\n return {};\n }\n\n ans.push_back(root->val);\n fun(root, 0);\n\n return ans;\n }\n};", "memory": "14300" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n\n if(!root) return {};\n\n\n queue<TreeNode*>q;\n\n q.push(root);\n\n vector<int>rsv;\n\n while(!q.empty()){\n\n int n = q.size();\n\n TreeNode* node = NULL;\n\n while(n--){\n\n\n node = q.front();\n q.pop();\n\n if(node->left){\n q.push(node->left);\n }\n\n if(node->right){\n q.push(node->right);\n }\n\n\n\n }\n\n\n rsv.push_back(node->val);\n\n\n\n\n\n\n }\n\n return rsv;\n\n }\n};", "memory": "14300" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n vector<int> arr;\n if(!root) return arr;\n map<int,int>mpp;\n queue<pair<TreeNode *,int>> q;\n q.push({root,0});\n while(!q.empty()){\n auto p= q.front();\n q.pop();\n TreeNode * node= p.first;\n int line = p.second;\n mpp[line]=node->val;\n if(node->left) q.push({node->left,line+1});\n if(node->right) q.push({node->right,line+1});\n }\n for(auto it:mpp){\n arr.push_back(it.second);\n }\n \n return arr;\n }\n};", "memory": "15000" }
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/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" /> <pre> <strong>Input:</strong> root = [1,2,3,null,5,null,4] <strong>Output:</strong> [1,3,4] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [1,null,3] <strong>Output:</strong> [1,3] </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> [] </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li> <li><code>-100 &lt;= Node.val &lt;= 100</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> rightSideView(TreeNode* root) {\n vector<int>ans;\n if(root == NULL)return ans;\n queue<pair<int,TreeNode*>>q;\n q.push({1,root});\n map<int,int>mp;\n while(!q.empty())\n {\n int n = q.size();\n while(n--)\n {\n int lev = q.front().first;\n TreeNode* node = q.front().second;\n if(mp.find(lev) == mp.end())mp[lev] = node->val;\n if(node->right)q.push({lev+1 , node->right});\n if(node->left)q.push({lev+1 , node->left});\n q.pop();\n }\n }\n\n for(auto it:mp) ans.push_back(it.second);\n return ans;\n }\n};", "memory": "15000" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 solve(i,j+1,n,m,grid);\n solve(i-1,j,n,m,grid);\n solve(i,j-1,n,m,grid);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n int ans=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]=='1'){\n ans++;\n solve(i,j,n,m,grid);\n }\n }\n }\n return ans;\n }\n};", "memory": "9568" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 dfs(grid, i, j);\n count++;\n }\n }\n }\n\n return count;\n }\n\n void dfs(vector<vector<char>>& grid, int i, int j) {\n if(i < 0 || j < 0 || i >= n || j >= m || grid[i][j] != '1') return;\n\n grid[i][j] = 0;\n\n dfs(grid, i+1, j);\n dfs(grid, i-1, j);\n dfs(grid, i, j+1);\n dfs(grid, i, j-1);\n }\n};", "memory": "9568" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 eraseIslands(grid, i, j);\n }\n }\n }\n return islands;\n }\nprivate:\n void eraseIslands(vector<vector<char>>& grid, int i, int j) {\n int n = grid.size(), m = grid[0].size();\n if (i < 0 || i == n || j < 0 || j ==m || grid[i][j] == '0') {\n return;\n }\n grid[i][j] = '0';\n eraseIslands(grid, i - 1, j);\n eraseIslands(grid, i + 1, j);\n eraseIslands(grid, i, j - 1);\n eraseIslands(grid, i, j + 1);\n }\n};", "memory": "9901" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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<vector<char>>& grid)\n {\n vis[si][sj] = true;\n for(int i = 0; i < 4; i++)\n {\n int ci = si + d[i].first;\n int cj = sj + d[i].second;\n if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj] == '1')\n {\n dfs(ci,cj, grid);\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n n = grid.size();\n m = grid[0].size();\n memset(vis, false, sizeof(vis));\n int ans = 0;\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j < m; j++)\n {\n if(!vis[i][j] && grid[i][j] == '1')\n {\n dfs(i,j, grid);\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "9901" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 && !visited[i+x[k]][j+y[k]] && grid[i+x[k]][j+y[k]] == '1'){\n dfs(grid,visited,i+x[k],j+y[k],m,n);\n }\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<vector<bool>> visited(m,vector<bool>(n,0));\n int ans = 0;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(!visited[i][j] && grid[i][j] == '1'){\n dfs(grid,visited,i,j,m,n);\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "10235" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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]){\n return;\n }\n\n vis[row][col] =1;\n\n dfs(row +1 , col , grid , vis);\n dfs(row -1 , col , grid , vis);\n dfs(row , col+1 , grid , vis);\n dfs(row , col-1 , grid , vis);\n }\n\npublic:\n int numIslands(vector<vector<char>>& grid) {\n \n int island =0;\n int m = grid.size();\n int n = grid[0].size();\n\n //isme n*n ka visted matrix bnaigai\n vector<vector<bool>> vis(m , vector<bool>(n , 0));\n\n for(int i=0; i<m; i++){\n for(int j=0; j<n; j++){\n if(grid[i][j] == '1' && !vis[i][j]){\n island++;\n dfs(i,j,grid, vis); \n }\n }\n }\n\n return island;\n }\n};", "memory": "10235" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 &dir:directions)\n {\n dfs(grid,row+dir[0],col+dir[1]);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int num=0;\n m=grid.size();\n n=grid[0].size();\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(grid[i][j]=='1')\n { num++;\n dfs(grid,i,j);\n }\n }\n }\n return num;\n }\n};", "memory": "10569" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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; i++) {\n int xi = x + ofX[i];\n int yi = y + ofY[i];\n\n if(xi >= 0 && yi >= 0 && yi < m && xi < n) {\n if(grid[xi][yi] == '1' && !v[xi][yi]) {\n dfs(xi, yi, grid, v);\n }\n }\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n int cnt = 0;\n\n vector<vector<bool>> v(grid.size(), vector<bool>(grid[0].size()));\n\n for(int i = 0; i < grid.size(); i++) {\n for(int j = 0; j < grid[0].size(); j++) {\n if(!v[i][j] && grid[i][j] == '1') {\n dfs(i, j, grid, v);\n cnt++;\n }\n }\n } \n\n return cnt;\n }\n};", "memory": "10569" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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,n,m);\n func(i,j+1,grid,vis,n,m);\n func(i,j-1,grid,vis,n,m);\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<bool>> vis(n,vector<bool>(m,false));\n int count=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(!vis[i][j] && grid[i][j]=='1'){\n count++;\n func(i,j,grid,vis,n,m);\n }\n }\n }\n return count;\n }\n};", "memory": "10903" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 }\n void dfs(int i , int j ,vector<vector<char>>& grid, vector<vector<bool>> &vis , int n , int m){\n vis[i][j] = true;\n \n // check all the four directions\n // up( i - 1 , j)\n if(check(i - 1 , j, n , m , grid , vis) == true){\n dfs(i - 1, j ,grid, vis , n ,m );\n }\n // down (i + 1 , j)\n if(check(i + 1, j , n , m , grid, vis) == true){\n dfs(i + 1, j ,grid, vis, n , m);\n }\n // right (i, j + 1)\n if(check(i , j+ 1, n , m , grid, vis) == true){\n dfs(i ,j + 1,grid , vis, n ,m );\n }\n // left (i, j -1)\n if(check(i , j - 1 , n , m , grid , vis) == true){\n dfs(i , j - 1, grid , vis, n , m);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int ans = 0;\n int n = grid.size() , m = grid[0].size();\n vector<vector<bool>> vis( n , vector<bool>(m , false));\n\n for(int i = 0 ; i < n ; i++){\n for(int j = 0 ; j < m ; j++){\n if(grid[i][j] == '1' && vis[i][j] == false){\n dfs(i , j , grid, vis , n , m);\n ans = ans + 1;\n }\n }\n }\n return ans;\n }\n};", "memory": "10903" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 return;\n }\n \n if (visited[y][x] == 0 && m[y][x] == '1')\n {\n visited[y][x] = 1;\n for(int i = 0; i < 4; i++)\n {\n if ((y+dy[i]) >= 0 && (x+dx[i]) >= 0 && (y+dy[i]) < Y && (x+dx[i]) < X && m[y+dy[i]][x+dx[i]] == '1')\n {\n search(y+dy[i], x+dx[i], m);\n }\n \n }\n \n \n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n \n Y = grid.size();\n X = grid[0].size();\n for(size_t j = 0; j < grid[0].size(); j++)\n {\n for (size_t i = 0; i < grid.size(); i++)\n \n {\n if ((grid[i][j] == '1') && !visited[i][j])\n {\n islands++;\n search(i, j, grid);\n }\n }\n }\n return islands;\n }\n};", "memory": "11236" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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[row][col] == '0') {\n return;\n }\n visited[row][col] = true;\n for (const vector<int>& dir: DIRS) {\n visitIsland(grid, rows, cols, row + dir[0], col + dir[1], visited);\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n if (grid.size() == 0 || grid[0].size() == 0) {\n return 0;\n }\n const int rows = grid.size();\n const int cols = grid[0].size();\n vector<vector<bool>> visited(rows, vector<bool>(cols, false));\n int numIslands = 0;\n\n for (int row = 0; row < rows; ++row) {\n //cout << \"row = \" << row << endl;\n for (int col = 0; col < cols; ++col) {\n //cout << \"col = \" << col << endl;\n if (!visited[row][col] && grid[row][col] == '1') {\n //cout << \"++numIslands\" << endl;\n ++numIslands;\n visitIsland(grid, rows, cols, row, col, visited);\n }\n }\n }\n\n return numIslands;\n }\n};", "memory": "11236" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 return;\n }\n \n if (visited[y][x] == 0 && m[y][x] == '1')\n {\n visited[y][x] = 1;\n for(int i = 0; i < 4; i++)\n {\n if ((y+dy[i]) >= 0 && (x+dx[i]) >= 0 && (y+dy[i]) < Y && (x+dx[i]) < X)\n {\n search(y+dy[i], x+dx[i], m);\n }\n \n }\n \n \n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n \n Y = grid.size();\n X = grid[0].size();\n for (size_t i = 0; i < grid.size(); i++)\n {\n for(size_t j = 0; j < grid[i].size(); j++)\n {\n if ((grid[i][j] == '1') && !visited[i][j])\n {\n islands++;\n search(i, j, grid);\n }\n }\n }\n return islands;\n }\n};", "memory": "11570" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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] : delta) {\n if (grid[row + drow][col + dcol] == '1') {\n bfs(grid, row + drow, col + dcol);\n }\n }\n }\n\n bool hasLand(vector<vector<char>> &grid) {\n for (int r = 0; r < grid.size(); r++) {\n for (int c = 0; c < grid[r].size(); c++) {\n if (grid[r][c] == '1')\n return true;\n }\n }\n\n return false;\n }\n\n int numIslands(vector<vector<char>>& grid) {\n vector<vector<char>> padded(grid.size() + 2, vector<char>(grid[0].size() + 2));\n for (int r = 0; r < grid.size(); r++) {\n for (int c = 0; c < grid[0].size(); c++) {\n padded[r+1][c+1] = grid[r][c];\n }\n }\n\n int nislands = 0;\n\n for (int r = 0; r < padded.size(); r++) {\n for (int c = 0; c < padded[r].size(); c++) {\n if (padded[r][c] == '1') {\n nislands++;\n bfs(padded, r, c);\n }\n }\n }\n\n return nislands;\n }\n};", "memory": "11570" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 int nr = r + dr[i];\n int nc = c + dc[i];\n if(nr >= 0 && nc >= 0 && nr < grd.size() && nc < grd[0].size() && grd[nr][nc]) dfs(nr,nc);\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n for(int i = 0; i < grid.size(); i++)\n {\n visited.push_back(vector<bool>());\n grd.push_back(vector<bool>());\n for(int j = 0; j < grid[i].size(); j++)\n {\n visited[i].push_back(false);\n grd[i].push_back(grid[i][j] == '1');\n }\n }\n\n int res=0;\n\n for(int i = 0; i < grid.size(); i++)\n {\n for(int j = 0; j < grid[i].size(); j++)\n {\n if(grd[i][j] && !visited[i][j])\n {\n dfs(i,j);\n res++;\n }\n }\n }\n\n return res;\n }\n};", "memory": "11904" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 while (p != v[p]) {\n p = v[p];\n }\n return p;\n };\n\n int rnd = 0;\n auto doUnion = [&](int x, int y) {\n if (rnd%2 == 0) {\n v[find(x)] = find(y);\n } else {\n v[find(y)] = find(x);\n }\n rnd++;\n };\n\n #define POS_MAP(x, y) (x*n + y)\n\n vector<pair<int, int>> dirs = {\n {-1, 0}, {1, 0}, {0, 1}, {0, -1}\n };\n for (int i = 0; i < m; i++) {\n for(int j = 0; j < n; j++) {\n if (grid[i][j] == '0') {\n continue;\n } \n \n for (auto& dir: dirs) {\n int x = i + dir.first;\n int y = j + dir.second;\n if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1') {\n doUnion(POS_MAP(i, j), POS_MAP(x, y));\n }\n }\n }\n }\n\n int cnt = 0;\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == '1' && find(POS_MAP(i, j)) == POS_MAP(i, j)) {\n cnt ++;\n }\n }\n }\n return cnt;\n }\n};", "memory": "11904" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 \n if (i < n - 1 && grid[i + 1][j] == '1' && !visited[(i + 1) * m + j]) // down\n dfs(i + 1, j, visited, grid);\n \n if (j > 0 && grid[i][j - 1] == '1' && !visited[i * m + (j - 1)]) //left\n dfs(i, j - 1, visited, grid);\n \n if (i > 0 && grid[i - 1][j] == '1' && !visited[(i - 1) * m + j]) // up\n dfs(i - 1, j, visited, grid);\n }\n \n int numIslands(vector<vector<char>>& grid) {\n n = grid.size(), m = grid[0].size();\n int island = 0;\n \n vector<int> visited(n * m, 0);\n for (int i = 0; i< n; i++) {\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == '1' && !visited[i * m + j]){\n dfs(i, j, visited, grid);\n island++;\n }\n }\n }\n return island;\n }\n};", "memory": "12238" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 if(grid[i][j] == '1') {\n que.emplace(pair{i, j});\n while(!que.empty()) {\n auto curr = que.front();\n grid[curr.first][curr.second]++;\n que.pop();\n for(const auto &dir: dirs) {\n auto x = dir[0] + curr.first;\n auto y = dir[1] + curr.second;\n if(x < 0 || y < 0 || x >= grid.size() || y >= grid[0].size()) {\n continue;\n }\n if(grid[x][y] == '1') {\n que.emplace(pair{x, y});\n grid[x][y]++;\n }\n }\n }\n cnt++;\n }\n }\n }\n return cnt;\n }\n};", "memory": "12238" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n \n queue<pair<int, int>> q;\n \n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i][j] == '1') {\n numIslands++;\n q.push({i, j});\n \n while (!q.empty()) {\n auto [x, y] = q.front();\n q.pop();\n \n if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != '1') {\n continue;\n }\n \n grid[x][y] = '0'; // mark as visited\n \n for (auto& dir : directions) {\n int nx = x + dir.first;\n int ny = y + dir.second;\n if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == '1') {\n q.push({nx, ny});\n }\n }\n }\n }\n }\n }\n \n return numIslands;\n }\n};", "memory": "12571" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 if (x < 0 || y < 0 || x >= n || y >= m || vis[x][y] || a[x][y] == 0) continue;\n dfs(x, y);\n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n n = grid.size(), m = grid[0].size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n a[i][j] = grid[i][j] - '0';\n }\n }\n\n int res = 0;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!vis[i][j] && a[i][j] == 1) {\n res++;\n dfs(i, j);\n }\n }\n }\n return res;\n }\n};\n", "memory": "12571" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 int row = x + dx[i];\n int col = y + dy[i];\n if(row < grid.size() && row >=0 && col < grid[0].size() && col>=0 && grid[row][col] == '1'){\n isIsland(grid,row,col,visited);\n }\n }\n\n \n }\n }\n\n int numIslands(vector<vector<char>>& grid) {\n ios_base::sync_with_stdio(false);\n cout.tie(NULL);\n cin.tie(NULL);\n\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> visited(n,vector<int>(m,0));\n \n int cnt = 0;\n\n for(int i=0; i < n; i++){\n for(int j =0; j < m; j++){\n if(grid[i][j] == '1' && !visited[i][j]){\n isIsland(grid,i,j,visited);\n cnt++;\n }\n }\n }\n\n return cnt;\n }\n};", "memory": "12905" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 + drow[i];\n int nc = c + dcol[i];\n\n if(nr >= 0 && nr < n && nc >= 0 && nc < m && \n !vis[nr][nc] && grid[nr][nc] == '1')\n dfs(nr, nc, vis, grid, drow, dcol);\n }\n }\npublic:\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n int drow[] = {0, 0, -1, +1};\n int dcol[] = {-1, +1, 0, 0};\n\n vector<vector<int>> vis(n, vector<int> (m, 0));\n int cnt = 0;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n if(!vis[i][j] && grid[i][j] == '1'){\n cnt++;\n dfs(i, j, vis, grid, drow, dcol);\n }\n }\n }\n\n return cnt;\n }\n};", "memory": "12905" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 row, int col) {\n if(row < 0 || col < 0 || row >= rows || col >= cols || grid[row][col] != '1')\n return;\n grid[row][col] = '0'; \n dfs(row - 1, col);\n dfs(row + 1, col);\n dfs(row, col - 1);\n dfs(row, col + 1);\n };\n \n for(int row = 0; row < rows; row++) {\n for(int col = 0; col < cols; col++) {\n if(grid[row][col] == '1') {\n dfs(row, col);\n islands++;\n }\n }\n }\n \n return islands;\n }\n};", "memory": "13239" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 res = 0;\n\n for ( int i=0; i<m; ++i ) {\n for ( int j=0; j<n; ++j ) {\n if ( visited[i][j] || grid[i][j] == '0' ) continue;\n q.push({i, j});\n while ( !q.empty() ) {\n int sz = q.size();\n for ( int k = 0; k<sz; ++k ) {\n const auto [x, y] = q.front(); q.pop();\n visited[x][y] = true;\n for ( int l=0; l<4; ++l ) {\n int nx = x + dir[l][0];\n int ny = y + dir[l][1];\n if ( nx < 0 || nx >= m || ny < 0 || ny >= n || visited[nx][ny] || grid[nx][ny] == '0' ) {\n continue;\n }\n visited[nx][ny] = true;\n q.push({nx, ny});\n }\n }\n }\n ++res;\n }\n }\n\n return res;\n }\n};", "memory": "13239" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 row, int col) {\n if(row < 0 || col < 0 || row >= rows || col >= cols || grid[row][col] != '1')\n return;\n grid[row][col] = '0'; \n dfs(row - 1, col);\n dfs(row + 1, col);\n dfs(row, col - 1);\n dfs(row, col + 1);\n };\n \n for(int row = 0; row < rows; row++) {\n for(int col = 0; col < cols; col++) {\n if(grid[row][col] == '1') {\n dfs(row, col);\n islands++;\n }\n }\n }\n \n return islands;\n }\n};", "memory": "13573" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 j = temp.second;\n if(i+1<n && grid[i+1][j] == '1' && !visited[i+1][j]) {q.push({i+1, j});visited[i+1][j] = 1;}\n if(j+1<m && grid[i][j+1] == '1' && !visited[i][j+1]) {q.push({i, j+1});visited[i][j+1] = 1;}\n if(i-1>-1 && grid[i-1][j] == '1' && !visited[i-1][j]) {q.push({i-1, j});visited[i-1][j] = 1;}\n if(j-1>-1 && grid[i][j-1] == '1' && !visited[i][j-1]) {q.push({i, j-1});visited[i][j-1] = 1;}\n }\n }\n\n void dfs(vector<vector<int>>& visited, int i, int j, vector<vector<char>>& grid, int n, int m){\n if(i+1<n && grid[i+1][j] == '1' && !visited[i+1][j]) {visited[i+1][j] = 1; dfs(visited, i+1, j, grid, n, m);}\n if(j+1<m && grid[i][j+1] == '1' && !visited[i][j+1]) {visited[i][j+1] = 1; dfs(visited, i, j+1, grid, n, m);}\n if(i-1>-1 && grid[i-1][j] == '1' && !visited[i-1][j]) {visited[i-1][j] = 1; dfs(visited, i-1, j, grid, n, m);}\n if(j-1>-1 && grid[i][j-1] == '1' && !visited[i][j-1]) {visited[i][j-1] = 1; dfs(visited, i, j-1, grid, n, m);}\n }\n int numIslands(vector<vector<char>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> visited(n, vector<int>(m, 0));\n int cnt = 0;\n for(int i = 0; i<n; i++){\n for(int j = 0; j<m; j++){\n if(grid[i][j] == '1' && visited[i][j] == 0){\n visited[i][j] = 1;\n // bfs(visited, i, j, grid, n, m);\n dfs(visited, i, j, grid, n, m);\n cnt+=1;\n }\n }\n }\n return cnt;\n }\n};", "memory": "13573" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 vector<int> dxs={0,1,0,-1};\n vector<int> dys={1,0,-1,0};\n int num=0;\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(grid[i][j]=='1'&&!visited[i][j]) //该区域是陆地且还未访问过\n {\n q.push(make_pair(i,j));\n visited[i][j] = true;\n while(!q.empty())\n {\n pair<int,int> coordinate=q.front();\n q.pop();\n //visited[coordinate.first][coordinate.second]=true;\n for(int s=0;s<dxs.size();s++)\n {\n int x=coordinate.first+dxs[s];\n int y=coordinate.second+dys[s];\n if(x>=0&&x<m&&y>=0&&y<n&&grid[x][y]=='1'&&!visited[x][y]) //四周格子在范围之内\n {\n q.push(make_pair(x,y));\n visited[x][y] = true;\n }\n }\n }\n num++;\n }\n }\n }\n return num;\n }\n};", "memory": "13906" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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 if(i < 0 || j < 0 || i >= m || j >= n ) return;\n\n if(grid[i][j] == '0' || isVisited[i][j]) return;\n else {\n isVisited[i][j] = 1;\n dfs(i-1, j);\n dfs(i+1, j);\n dfs(i, j-1);\n dfs(i, j+1);\n }\n };\n\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < n; j++) {\n if(!isVisited[i][j] && grid[i][j] == '1'){\n dfs(i, j);\n numIslands++;\n }\n }\n }\n\n return numIslands;\n }\n};", "memory": "13906" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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(grid[i][j] == '1' && !vis[i][j]) {\n markIsland(grid, vis, i, j);\n num++;\n }\n }\n }\n return num;\n }\nprivate:\n void markIsland(vector<vector<char>>& grid, vector<vector<bool>>& vis, int i, int j) {\n vis[i][j] = true;\n if (i > 0 && grid[i - 1][j] == '1' && !vis[i - 1][j]) markIsland(grid, vis, i - 1, j);\n if (j > 0 && grid[i][j - 1] == '1' && !vis[i][j - 1]) markIsland(grid, vis, i, j - 1);\n if (i < grid.size() - 1 && grid[i + 1][j] == '1' && !vis[i + 1][j]) markIsland(grid, vis, i + 1, j);\n if (j < grid[i].size() - 1 && grid[i][j + 1] == '1' && !vis[i][j + 1]) markIsland(grid, vis, i, j + 1);\n }\n};", "memory": "14240" }
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 vertically. You may assume all four edges of the grid are all surrounded by water.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;] ] <strong>Output:</strong> 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> grid = [ [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;], [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;] ] <strong>Output:</strong> 3 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 300</code></li> <li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> </ul>
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] = toVisit.back();\n toVisit.pop_back();\n if (grid[row][col] != '1') continue;\n grid[row][col] = 'x';\n if (row > 0 && grid[row - 1][col] == '1') toVisit.emplace_back(row - 1, col);\n if (col > 0 && grid[row][col - 1] == '1') toVisit.emplace_back(row, col - 1);\n if (row < m - 1 && grid[row + 1][col] == '1') toVisit.emplace_back(row + 1, col);\n if (col < n - 1 && grid[row][col + 1] == '1') toVisit.emplace_back(row, col + 1);\n }\n }\n int numIslands(vector<vector<char>>& grid) {\n int num = 0;\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[i].size(); ++j) {\n if (grid[i][j] == '1') {\n ++num;\n visit(grid, i, j);\n }\n }\n }\n return num;\n }\n};", "memory": "16243" }