id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "void dfs(int node, vector<vector<vector<int>>> &alist, vector<bool> &visited, int *ans)\n{\n visited[node] = true;\n int i;\n for(i=0;i<alist[node].size();i++)\n {\n if(!visited[alist[node][i][0]])\n {\n if(alist[node][i][1] == 1)\n {\n dfs(alist[node][i][0], alist, visited, ans);\n }\n else\n {\n (*ans) = (*ans) + 1;\n alist[node][i][1] = 1;\n dfs(alist[node][i][0], alist, visited, ans);\n }\n }\n }\n}\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<vector<int>>> alist(n, vector<vector<int>>());\n int i;\n for(i=0;i<connections.size();i++)\n {\n alist[connections[i][0]].push_back({connections[i][1], 0});\n alist[connections[i][1]].push_back({connections[i][0], 1});\n }\n\n int ans = 0;\n vector<bool> visited(n, false);\n dfs(0, alist, visited, &ans);\n return ans;\n }\n};",
"memory": "146606"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "void dfs(int node, vector<vector<vector<int>>> &alist, vector<bool> &visited, int *ans)\n{\n visited[node] = true;\n int i;\n for(i=0;i<alist[node].size();i++)\n {\n if(!visited[alist[node][i][0]])\n {\n if(alist[node][i][1] == 1)\n {\n dfs(alist[node][i][0], alist, visited, ans);\n }\n else\n {\n (*ans) = (*ans) + 1;\n alist[node][i][1] = 1;\n dfs(alist[node][i][0], alist, visited, ans);\n }\n }\n }\n}\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<vector<int>>> alist(n, vector<vector<int>>());\n int i;\n for(i=0;i<connections.size();i++)\n {\n alist[connections[i][0]].push_back({connections[i][1], 0});\n alist[connections[i][1]].push_back({connections[i][0], 1});\n }\n\n int ans = 0;\n vector<bool> visited(n, false);\n dfs(0, alist, visited, &ans);\n return ans;\n }\n};",
"memory": "146606"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int i, vector<vector<int>>&graph,vector<int>&vis,int &cnt,set<pair<int,int>>&st){\n vis[i]=1;\n for(auto it:graph[i]){\n if(!vis[it]){\n if(st.find({i,it})!=st.end()) cnt++;\n dfs(it,graph,vis,cnt,st);\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n \n set<pair<int,int>>st;\n for(auto it:connections){\n st.insert({it[0],it[1]});\n }\n vector<vector<int>>graph(n);\n vector<int>vis(n,0);\n for(auto it:connections){\n graph[it[0]].push_back(it[1]);\n graph[it[1]].push_back(it[0]);\n }\n int cnt=0;\n dfs(0,graph,vis,cnt,st);\n \n return cnt;\n }\n};",
"memory": "148060"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n std::unordered_map<int, std::vector<int>> undirected;\n for(const auto& vec: connections) {\n undirected[vec[0]].push_back(vec[1]);\n undirected[vec[1]].push_back(vec[0]);\n }\n\n std::vector<std::vector<int>> final_edges;\n std::vector<int> visited(n, 0);\n std::queue<int> q;\n\n visited[0] = 1;\n q.push(0);\n while(!q.empty()) {\n int node = q.front();\n q.pop();\n for (int next: undirected[node]) {\n if (visited[next] == 0) {\n visited[next] = 1;\n q.push(next);\n final_edges.push_back({next, node});\n }\n }\n }\n\n auto comparator = [](const auto& lhs, const auto& rhs) {\n int lhs_min = std::min(lhs[0], lhs[1]);\n int lhs_max = std::max(lhs[0], lhs[1]);\n\n int rhs_min = std::min(rhs[0], rhs[1]);\n int rhs_max = std::max(rhs[0], rhs[1]);\n\n if (lhs_min != rhs_min) {\n return lhs_min < rhs_min;\n } else {\n return lhs_max < rhs_max;\n }\n };\n\n std::sort(connections.begin(), connections.end(), comparator);\n std::sort(final_edges.begin(), final_edges.end(), comparator);\n\n int ans = 0;\n for(int i = 0; i < connections.size(); ++i) {\n if (connections[i][0] != final_edges[i][0]) {\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "148060"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adjListUndir;\n unordered_set<string> connectionsSet;\n vector<bool> visited;\n int ans = 0;\n\n int minReorder(int n, vector<vector<int>>& connections) {\n adjListUndir.resize(n);\n visited.resize(n, false);\n\n // create adj list of size n assuming undirected roads between nodes\n for (const vector<int>& conn : connections) {\n int node1 = conn[0], node2 = conn[1];\n adjListUndir[node1].push_back(node2);\n adjListUndir[node2].push_back(node1);\n string key = to_string(node1) + ',' + to_string(node2);\n connectionsSet.insert(key);\n }\n\n // call dfs for each node if not already visited\n for (int i = 0; i < n; i++) {\n if (!visited[i]) {\n dfs(i);\n }\n }\n\n return ans;\n }\n\n void dfs(int i) {\n visited[i] = true;\n for (int j : adjListUndir[i]) {\n if (!visited[j]) {\n string key = to_string(i) + ',' + to_string(j);\n if (connectionsSet.contains(key)) ans++;\n dfs(j);\n }\n }\n }\n};",
"memory": "149514"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adjListUndir;\n unordered_set<string> connectionsSet;\n vector<bool> visited;\n int ans = 0;\n\n int minReorder(int n, vector<vector<int>>& connections) {\n adjListUndir.resize(n);\n visited.resize(n, false);\n\n // create adj list of size n assuming undirected roads between nodes\n for (const vector<int>& conn : connections) {\n int node1 = conn[0], node2 = conn[1];\n adjListUndir[node1].push_back(node2);\n adjListUndir[node2].push_back(node1);\n string key = to_string(node1) + ',' + to_string(node2);\n connectionsSet.insert(key);\n }\n\n // call dfs for each node if not already visited\n for (int i = 0; i < n; i++) {\n if (!visited[i]) {\n dfs(i);\n }\n }\n\n return ans;\n }\n\n void dfs(int i) {\n visited[i] = true;\n for (int j : adjListUndir[i]) {\n if (!visited[j]) {\n string key = to_string(i) + ',' + to_string(j);\n if (connectionsSet.contains(key)) ans++;\n dfs(j);\n }\n }\n }\n};",
"memory": "149514"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n set<pair<int,int>>s;\n for(auto it : connections){\n s.insert({it[0],it[1]});\n }\n vector<vector<int>>adj(n);\n for(auto it : connections){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int>vis(n,0);\n vis[0] = 1;\n queue<int>q;\n q.push(0);\n int ans = 0;\n while(!q.empty()){\n int curr = q.front();\n q.pop();\n for(int i=0;i<adj[curr].size();i++){\n if(vis[adj[curr][i]] == 1){\n continue;\n }\n if(s.count({adj[curr][i],curr})==0){\n ans++;\n s.insert({adj[curr][i],curr});\n }\n vis[adj[curr][i]] = 1;\n q.push(adj[curr][i]);\n }\n }\n return ans;\n }\n};",
"memory": "150968"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n unordered_map<int, vector<int>> graph;\n vector<bool> seen;\n set<pair<int, int>> roads;\n\npublic:\n int dfs(int node) {\n int rev = 0;\n for (int neighbor : graph[node]) {\n if (!seen[neighbor]) {\n // finding roads away from zero and if found rev will increment\n // indicating this road's direction should be reversed\n if (roads.find({node, neighbor}) != roads.end()) {\n rev++;\n }\n seen[neighbor] = true;\n rev += dfs(neighbor);\n }\n }\n return rev;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n // build the graph\n seen = vector(n, false);\n int e = connections.size();\n for (int i = 0; i < e; i++) {\n graph[connections[i][0]].push_back(connections[i][1]);\n graph[connections[i][1]].push_back(connections[i][0]);\n // add each edge to road as a pair in the direction of away from\n // zero\n roads.insert({connections[i][0], connections[i][1]});\n }\n seen[0] = true;\n return dfs(0);\n }\n};",
"memory": "150968"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n map<int, map<int, int>> mp;\n int dfs(int node, int par) {\n int ret = 0;\n for (auto &[v, w]: mp[node]) {\n if (par == v) continue;\n ret += w + dfs(v, node);\n }\n return ret;\n }\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n // dag\n for (auto &v: connections) {\n mp[v[0]][v[1]] = 1;\n mp[v[1]][v[0]] = 0;\n }\n \n return dfs(0, -1);\n }\n};",
"memory": "152421"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans = 0;\n\n int minReorder(int n, vector<vector<int>>& connections) {\n map<int, set<pair<int,int>>> mp;\n \n\n for(const auto& con : connections)\n {\n mp[con[0]].insert({con[1],1});\n mp[con[1]].insert({con[0],0});\n }\n\n\n search(mp, -1, 0);\n \n return ans;\n }\n\n void search(map<int, set<pair<int,int>>>& mp, int root, int child)\n {\n for(const auto rev : mp[child])\n {\n if(rev.first == root) continue;\n\n ans += rev.second;\n search(mp, child, rev.first);\n }\n }\n};",
"memory": "152421"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<vector<int>> curEdges;\n bool vis[50005];\n vector<int> adj[50005];\n\n int minReorder(int n, vector<vector<int>>& connections) {\n for (vector<int> vt : connections){\n curEdges.insert({vt[1], vt[0]});\n adj[vt[0]].push_back(vt[1]);\n adj[vt[1]].push_back(vt[0]);\n }\n int cnt = 0;\n queue<int> q;\n q.push(0);\n vis[0] = true;\n while (!q.empty()){\n int cur = q.front(); q.pop();\n for (int nxt : adj[cur]){\n if (vis[nxt]) continue;\n if (curEdges.find({cur, nxt}) == curEdges.end()){\n cnt++;\n }\n q.push(nxt); \n vis[nxt] = true;\n\n }\n }\n return cnt;\n }\n};",
"memory": "153875"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n \n //Create adj matrix\n vector<vector<int>> adj[n];\n for(int i=0;i<connections.size();i++)\n {\n int u = connections[i][0];\n int v = connections[i][1];\n\n adj[u].push_back({v,1});\n adj[v].push_back({u,0});\n }\n\n //Do BFS\n vector<bool> visited(n,false);\n queue<int> q;\n q.push(0);\n visited[0] = true;\n int cnt = 0;\n while(!q.empty())\n {\n int node = q.front();\n q.pop();\n cout<<node<<\" \";\n for(auto x:adj[node])\n {\n int adjNode = x[0];\n int edgeWt = x[1];\n if(!visited[adjNode] && edgeWt==true)\n cnt++;\n if(!visited[adjNode])\n {\n visited[adjNode] = true;\n q.push(adjNode);\n }\n }\n }\n return cnt;\n }\n};",
"memory": "153875"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n/*\nVVIMP- WE WILL USE ORIGINAL AND ARTIFICIAL CONCEPT HERE\nhttps://www.youtube.com/watch?v=42Z0eaopoZ8\n*/\n \n void dfs(int src,unordered_map<int,bool> &visited,unordered_map<int,vector<pair<int,int>>> &mp,int &cnt){\n visited[src]=true;\n for(auto p:mp[src]){\n auto v1=p.first;\n auto v2=p.second;\n \n if(!visited[v2]){\n if(v1==1)\n cnt+=1;\n dfs(v2,visited,mp,cnt);\n }\n \n \n }\n\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n \n unordered_map<int,vector<pair<int,int>>> mp;\n unordered_map<int,bool> visited;\n for(auto node:connections){\n int src=node[0];\n int des=node[1];\n mp[src].push_back({1,des});\n mp[des].push_back({0,src});\n }\n int cnt=0;\n dfs(0,visited,mp,cnt);\n return cnt;\n\n }\n\n};",
"memory": "155329"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n/*\nVVIMP- WE WILL USE ORIGINAL AND ARTIFICIAL CONCEPT HERE\nhttps://www.youtube.com/watch?v=42Z0eaopoZ8\n*/\n \n void dfs(int src,unordered_map<int,bool> &visited,unordered_map<int,vector<pair<int,int>>> &mp,int &cnt){\n visited[src]=true;\n for(auto p:mp[src]){\n auto v1=p.first;\n auto v2=p.second;\n if(!visited[v2]){\n if(v1==1)\n cnt+=1;\n dfs(v2,visited,mp,cnt);\n }\n \n \n }\n\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n \n unordered_map<int,vector<pair<int,int>>> mp;\n unordered_map<int,bool> visited;\n for(auto node:connections){\n int src=node[0];\n int des=node[1];\n mp[src].push_back({1,des});\n mp[des].push_back({0,src});\n }\n int cnt=0;\n dfs(0,visited,mp,cnt);\n return cnt;\n\n }\n\n};",
"memory": "155329"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\n struct Edge {\n \n int next;\n bool myEdge;\n \n Edge(int next, bool myEdge) :next(next), myEdge(myEdge) {};\n };\n\npublic:\n int count = 0;\n\n void dfs (unordered_map<int, vector<Edge>>&graph, unordered_set<int>& visited, int node) {\n visited.insert(node);\n for(auto neighbor:graph[node]) {\n\n if (visited.find(neighbor.next) == visited.end()) {\n count+=int(neighbor.myEdge);\n dfs(graph, visited,neighbor.next);\n }\n \n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n \n unordered_set<int>visited;\n unordered_map<int, vector<Edge>>graph;\n for (auto pair : connections) {\n graph[pair[0]].push_back({pair[1], true}); // This edge is directed from pair[0] to pair[1], so it might need to be reordered\ngraph[pair[1]].push_back({pair[0], false}); // This edge is the reverse direction, no reordering needed\n\n }\n dfs(graph, visited, 0);\n return count;\n\n }\n};",
"memory": "156783"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans = 0;\n void dfs(int i,map<vector<int>,int>&mp,vector<int>&dp,vector<int>dir[]){\n dp[i]=1;\n for(auto it:dir[i]){\n if(!dp[it]){\n if(mp.count({i,it})){\n ans++;\n }\n dfs(it,mp,dp,dir),dir;\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int>adj[n],dir[n];\n map<vector<int>,int>mp;\n for(int i=0;i<connections.size();i++){\n mp[connections[i]]++;\n dir[connections[i][0]].push_back(connections[i][1]);\n dir[connections[i][1]].push_back(connections[i][0]);\n } \n vector<int>dp(n,0);\n dfs(0,mp,dp,dir);\n return ans;\n }\n};",
"memory": "156783"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\ntypedef vector<vector<int>> GRAPH;\nint counter=0;\t\nvoid dfs(GRAPH& G1,int node,vector<bool>&visited,set<vector<int>>&S1){\n\tvisited[node]=true;\n\tfor(int i=0; i<G1[node].size(); i++){\n\t\tif(!visited[G1[node][i]]){\n\t\t\tif(S1.count({node,G1[node][i]}))\n\t\t\tcounter++;\n\t\t\tdfs(G1,G1[node][i],visited,S1);\n\t\t}\t\n\t}\n}\nint minReorder(int n, vector<vector<int>>& connections) {\n\tset<vector<int>>S1;\n\tS1.insert(connections.begin(),connections.end());\n vector<bool>visited(n);\n GRAPH G1(n);\n for(int i=0; i<connections.size(); i++){\n G1[connections[i][0]].push_back(connections[i][1]);\n G1[connections[i][1]].push_back(connections[i][0]);\n }\n \n \tdfs(G1,0,visited,S1);\n\t\treturn counter;\n }\n};",
"memory": "162598"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\ntypedef vector<vector<int>> GRAPH;\nint counter=0;\t\nvoid dfs(GRAPH& G1,int node,vector<bool>&visited,set<vector<int>>&S1){\n\tvisited[node]=true;\n\tfor(int i=0; i<G1[node].size(); i++){\n\t\tif(!visited[G1[node][i]]){\n\t\t\tif(S1.count({node,G1[node][i]}))\n\t\t\tcounter++;\n\t\t\tdfs(G1,G1[node][i],visited,S1);\n\t\t}\t\n\t}\n}\nint minReorder(int n, vector<vector<int>>& connections) {\n\tset<vector<int>>S1;\n\tS1.insert(connections.begin(),connections.end());\n vector<bool>visited(n);\n GRAPH G1(n);\n for(int i=0; i<connections.size(); i++){\n G1[connections[i][0]].push_back(connections[i][1]);\n G1[connections[i][1]].push_back(connections[i][0]);\n }\n \n for(int i=0 ; i<n; i++){\n \tif(!visited[i])\n \tdfs(G1,i,visited,S1);\n\t\t}\n\t\treturn counter;\n }\n};",
"memory": "162598"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n void dfsWithInversions(vector<vector<int>> &undirectedAdjList, vector<unordered_set<int>> &adjList, int prevNode, int currNode, int &numInverts)\n {\n for (int i = 0; i < undirectedAdjList[currNode].size(); i++)\n {\n int nextNode = undirectedAdjList[currNode][i];\n if (nextNode == prevNode)\n continue;\n \n if (adjList[currNode].find(nextNode) != adjList[currNode].end())\n {\n // edge found in original adj list\n numInverts++;\n }\n\n dfsWithInversions(undirectedAdjList, adjList, currNode, nextNode, numInverts);\n }\n }\n\n\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<unordered_set<int>> adjList(n);\n vector<vector<int>> undirectedAdjList(n);\n for (int i = 0; i < connections.size(); i++)\n {\n int a = connections[i][0];\n int b = connections[i][1];\n adjList[a].insert(b);\n undirectedAdjList[a].push_back(b);\n undirectedAdjList[b].push_back(a);\n }\n\n int numInverts = 0;\n dfsWithInversions(undirectedAdjList, adjList, -1, 0, numInverts);\n return numInverts;\n }\n};",
"memory": "164051"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n public:\n\n int minReorder(int n, vector<vector<int>>& connections) {\n\n unordered_map<int, set<int>> adjList;\n set<pair<int, int>> directed;\n\n for (const auto& conn : connections) {\n adjList[conn[0]].insert(conn[1]);\n adjList[conn[1]].insert(conn[0]);\n directed.insert({conn[0], conn[1]});\n }\n\n vector<bool> visited(n, false);\n queue<int> q;\n q.push(0);\n visited[0] = true;\n int cnt = 0;\n\n while (!q.empty()) {\n int city = q.front();\n q.pop();\n\n for (const auto& neighbor : adjList[city]) {\n if (!visited[neighbor]) {\n visited[neighbor] = true;\n\n if (directed.find({city, neighbor}) != directed.end()) {\n cnt++;\n }\n q.push(neighbor);\n }\n }\n }\n\n return cnt;\n}\n\n\n};",
"memory": "164051"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int changes = 0;\n void dfs(vector<int> g[],set<vector<int>> &s,int u,vector<int> &vis){\n for(auto it = g[u].begin();it!=g[u].end();it++){\n if(vis[*it])\n continue;\n if(s.find({*it,u}) == s.end()){\n changes++;\n }\n vis[*it] = true;\n dfs(g,s,*it,vis);\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<int> vis(n, false);\n vis[0] = true;\n vector<int> g[n]; set<vector<int>> s;\n for(vector<int> c:connections){\n g[c[0]].push_back(c[1]);\n g[c[1]].push_back(c[0]);\n s.insert({c[0],c[1]});\n }\n \n dfs(g,s,0,vis);\n return changes;\n }\n};",
"memory": "165505"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<bool> occupy(n);\n vector<unordered_map<int, bool>> link(n);\n for (auto const& connection: connections) {\n link[connection[0]][connection[1]] = true;\n link[connection[1]][connection[0]] = false;\n }\n int wrong_dir = 0;\n count(link, occupy, 0, wrong_dir);\n return wrong_dir;\n }\nprivate:\n void count(vector<unordered_map<int, bool>> const& link, vector<bool>& occupy, int idx, int& wrong_dir) {\n occupy[idx] = true;\n for (auto const& next: link[idx]) {\n if (!occupy[next.first]) {\n if (next.second) {\n wrong_dir++;\n }\n count(link, occupy, next.first, wrong_dir);\n }\n }\n occupy[idx] = false;\n }\n};",
"memory": "165505"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n map<int, vector<int>>mp;\n set<pair<int,int>>con;\n set<int>vis;\n int ans;\n\n void dfs(int ver){\n cout<<ver<<endl;\n \n vis.insert(ver);\n\n for(int &ch: mp[ver]){\n \n if(vis.find(ch)!=vis.end()){\n continue;\n }\n \n if(con.find( {ch, ver} )==con.end()){\n ans++;\n }\n\n dfs(ch);\n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n \n mp.clear();\n con.clear();\n vis.clear();\n \n ans = 0;\n\n for(vector<int>&i: connections){\n mp[i[0]].push_back(i[1]);\n mp[i[1]].push_back(i[0]);\n\n con.insert( {i[0], i[1]} );\n }\n\n dfs(0);\n\n return ans;\n }\n};",
"memory": "166959"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n map<int, vector<int>> m;\n map<int, vector<int>> rm;\n\n for (auto &connection :connections) {\n m[connection[0]].push_back(connection[1]);\n rm[connection[1]].push_back(connection[0]);\n }\n\n queue<int> q;\n q.push(0);\n int result = 0;\n set<int> st;\n while (!q.empty()) {\n int tmp = q.front();\n st.insert(tmp);\n q.pop();\n for (auto &next :m[tmp]) {\n if (!st.count(next)) {\n q.push(next);\n result += 1;\n }\n }\n\n for (auto &next :rm[tmp]) {\n if (!st.count(next))\n q.push(next);\n }\n }\n return result;\n }\n};",
"memory": "166959"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int recurDFS(int i, vector<vector<int>>& adj, vector<set<int>>& route, set<int>& seen) {\n if (seen.find(i) != seen.end()) return 0;\n seen.insert(i);\n int count = 0;\n for (int j : adj[i]) {\n if (seen.find(j) == seen.end() && route[j].find(i) == route[j].end()) count+=1;\n count += recurDFS(j, adj, route, seen);\n }\n return count;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj(n);\n vector<set<int>> route(n);\n set<int> seen;\n for (vector<int> r : connections) {\n adj[r[0]].push_back(r[1]);\n adj[r[1]].push_back(r[0]);\n route[r[0]].insert(r[1]);\n }\n return recurDFS(0, adj, route, seen);\n }\n};",
"memory": "168413"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int recurDFS(int i, vector<vector<int>>& adj, vector<set<int>>& route, set<int>& seen) {\n if (seen.find(i) != seen.end()) return 0;\n seen.insert(i);\n int count = 0;\n for (int j : adj[i]) {\n if (seen.find(j) == seen.end() && route[j].find(i) == route[j].end()) count+=1;\n count += recurDFS(j, adj, route, seen);\n }\n return count;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj(n);\n vector<set<int>> route(n);\n set<int> seen;\n for (vector<int> r : connections) {\n adj[r[0]].push_back(r[1]);\n adj[r[1]].push_back(r[0]);\n route[r[0]].insert(r[1]);\n }\n return recurDFS(0, adj, route, seen);\n }\n};",
"memory": "168413"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(int node, unordered_map<int, vector<int>>& adj,\n unordered_map<int, vector<int>>& edge, vector<bool>& visited) {\n visited[node] = true;\n int count = 0;\n for (int neighbour : adj[node]) {\n if (!visited[neighbour]) {\n if (edge[node].size() &&\n find(edge[node].begin(), edge[node].end(), neighbour) !=\n edge[node].end()){\n count++;\n }\n count+=dfs(neighbour,adj,edge,visited);\n }\n }\n return count;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<int>> adj;\n unordered_map<int, vector<int>> edge;\n vector<bool> visited(n, false);\n for (vector<int> connection : connections) {\n adj[connection[0]].push_back(connection[1]);\n adj[connection[1]].push_back(connection[0]);\n edge[connection[0]].push_back(connection[1]);\n }\n return dfs(0, adj, edge, visited);\n }\n};",
"memory": "169866"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int, vector<int>> &adj, map<pair<int, int>, bool> &edges, vector<bool> &visited, int curr){\n visited[curr] = true;\n int ans = 0;\n for(auto neighbor: adj[curr]){\n if(!visited[neighbor]){\n pair<int, int> p(curr, neighbor);\n if(edges[p]){\n ans++;\n }\n ans+= dfs(adj, edges, visited, neighbor);\n }\n }\n return ans;\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<int>> adj;\n map<pair<int, int>, bool> edges;\n for(auto edge: connections){\n pair<int, int> p(edge[0], edge[1]);\n edges[p] = true;\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n vector<bool> visited(n, false);\n \n return dfs(adj, edges, visited, 0); \n }\n};",
"memory": "169866"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int,list<int>> graph,rev_graph;\n\n for(int i=0;i<connections.size();i++){\n int u=connections[i][0];\n int v=connections[i][1];\n\n graph[u].push_back(v);\n rev_graph[v].push_back(u);\n }\n\n queue<int> q;\n q.push(0);\n unordered_map<int,bool> visited;\n int ans=0;\n visited[0]=true;\n while(!q.empty()){\n int front=q.front();\n q.pop();\n for(auto i:rev_graph[front]){\n if(!visited[i]){\n q.push(i);\n visited[i]=true;\n }\n }\n\n for(auto i:graph[front]){\n if(!visited[i]){\n ans++;\n q.push(i);\n visited[i]=true;\n }\n }\n }\n return ans;\n }\n};",
"memory": "171320"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& edges) {\n set<vector<int>> real(edges.begin(), edges.end());\n vector<vector<int>> graph(n);\n for(auto edge : edges){\n auto a = edge[0], b = edge[1];\n graph[a].push_back(b);\n graph[b].push_back(a);\n }\n int count = 0;\n function <void(int, int)>dfs = [&](int node, int prev)\n {\n for(auto neigh : graph[node])\n {\n if(neigh == prev)\n continue;\n\n if(real.find({node, neigh}) != real.end())\n count++;\n\n dfs(neigh, node);\n }\n };\n dfs(0, -1);\n return count;\n // return 0;\n }\n};",
"memory": "171320"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n // int nn=connections.size();\n vector<vector<int>> ug(n);\n set<vector<int>> st;\n for(auto x:connections){\n ug[x[0]].push_back(x[1]);\n ug[x[1]].push_back(x[0]);\n st.insert(x);\n }\n int ans=0;\n vector<bool> visited(n,false);\n dfs(ug,visited,0,st,ans);\n return ans;\n }\n void dfs(vector<vector<int>>& ug,vector<bool>& visited,int i,set<vector<int>>& st,int& ans){\n visited[i]=true;\n for(auto x:ug[i]){\n if(not visited[x]){\n if(st.find({x,i})==st.end())\n ans++;\n dfs(ug,visited,x,st,ans);\n }\n }\n }\n};",
"memory": "172774"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <queue>\nusing namespace std;\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj(n);\n unordered_map<int, unordered_set<int>> directedEdges;\n \n for (const auto& it : connections) {\n int u = it[0];\n int v = it[1];\n directedEdges[u].insert(v);\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n int cnt = 0;\n queue<int> q;\n q.push(0);\n vector<bool> vis(n, false);\n vis[0] = true;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n for (int neighbor : adj[node]) {\n if (!vis[neighbor]) {\n vis[neighbor] = true;\n if (directedEdges[node].find(neighbor) != directedEdges[node].end()) {\n cnt++;\n }\n q.push(neighbor);\n }\n }\n }\n return cnt;\n }\n};\n",
"memory": "172774"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n int l = connections.size();\n map<int, set<int>> graph;\n vector<vector<int>> tree(n, vector<int>(0));\n for (int i = 0; i < l; i++) {\n tree[connections[i][0]].push_back(connections[i][1]);\n tree[connections[i][1]].push_back(connections[i][0]);\n graph[connections[i][0]].insert(connections[i][1]);\n }\n\n queue<int> nodes;\n vector<int> visited(n, 0);\n nodes.push(0);\n int flips = 0;\n\n while(!nodes.empty()) {\n int curr = nodes.front();\n nodes.pop();\n visited[curr] = 1;\n set<int> currSet = graph[curr];\n for (int i = 0; i < tree[curr].size(); i++) {\n if (visited[tree[curr][i]] == 0) {\n if (currSet.find(tree[curr][i]) != currSet.end()) {\n flips++;\n }\n nodes.push(tree[curr][i]);\n visited[tree[curr][i]] = 1;\n }\n }\n }\n\n return flips;\n }\n};",
"memory": "174228"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adj(n, vector<int>());\n vector<unordered_set<int>> next(n, unordered_set<int>());\n for(const auto &connection : connections){\n int from = connection[0], to = connection[1];\n adj[from].push_back(to);\n adj[to].push_back(from);\n next[from].insert(to);\n }\n int count = 0;\n unordered_set<int> visited;\n visited.insert(0);\n queue<int> q;\n q.push(0);\n while(!q.empty()){\n int n = q.size();\n while(n--){\n int curr = q.front();\n q.pop();\n for(auto nei : adj[curr]){\n if(visited.find(nei) == visited.end()){\n if(next[curr].find(nei) != next[curr].end()){\n cout << curr << \"->\" << nei << endl;\n count++;\n }\n q.push(nei);\n visited.insert(nei);\n }\n }\n }\n }\n\n return count;\n }\n};",
"memory": "174228"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(unordered_map<int,vector<int>>&mapo,unordered_map<int,vector<int>>&c,vector<int>&vis,int node){\n if(vis[node]){\n return 0;\n }\n vis[node]=1;\n int ans=0;\n for(auto it:mapo[node]){\n if(!vis[it] && find(c[node].begin(),c[node].end(),it)!=c[node].end()){\n ans+= 1+dfs(mapo,c,vis,it);\n }\n ans+= dfs(mapo,c,vis,it);\n }\n return ans;\n }\n int minReorder(int n, vector<vector<int>>& co) {\n int ans=0;\n unordered_map<int,vector<int>>mapo;\n unordered_map<int,vector<int>>c;\n for(auto it:co ){\n mapo[it[0]].push_back(it[1]);\n mapo[it[1]].push_back(it[0]);\n c[it[0]].push_back(it[1]);\n }\n vector<int>vis(n+1,0);\n return dfs(mapo,c,vis,0);\n \n }\n};",
"memory": "175681"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& c) {\n unordered_map<int,set<int>> adjorg;\n unordered_map<int,vector<int>> unadj;\n for(auto it:c){\n int u=it[0];\n int v=it[1];\n adjorg[u].insert(v);\n unadj[u].push_back(v);\n unadj[v].push_back(u);\n }\n vector<int> vis(n);\n int cnt=0;\n queue<int> q;\n q.push(0);\n while(!q.empty()){\n int u=q.front();\n q.pop();\n vis[u]=1;\n for(auto v:unadj[u]){\n if(!vis[v]){\n q.push(v);\n if(adjorg[u].find(v) != adjorg[u].end()){\n cnt++;\n }\n }\n }\n }\n return cnt;\n }\n};",
"memory": "175681"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n int min = 0;\n vector<vector<int>> undirected(n);\n vector<unordered_set<int>> orig(n);\n vector<bool> visited(n, false);\n \n // The goal is to create an undirected graph and then dfs from node 0\n // Compare to original \n for (auto connection : connections) {\n undirected[connection[0]].push_back(connection[1]);\n undirected[connection[1]].push_back(connection[0]);\n orig[connection[0]].insert(connection[1]);\n }\n\n function<void(int node)> dfs = [&](int node) {\n\n for (int next : undirected[node]) {\n if (!visited[next]) {\n visited[next] = true;\n if (!orig[next].count(node)) {\n ++min;\n }\n dfs(next);\n }\n }\n };\n\n visited[0] = true;\n dfs(0);\n\n return min;\n }\n};",
"memory": "177135"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint cnt=0;\n\nvoid dfs(int node,vector<vector<vector<int>>>&v,vector<int>&vis ){\n\nvis[node]=1;\n for(auto it:v[node]){\n if(vis[it[0]])continue;\n if(it[1]){\n cnt++;\n dfs(it[0],v,vis);\n }\n else{\n dfs(it[0],v,vis);\n }\n }\n\n\n}\n int minReorder(int n, vector<vector<int>>& edges) {\n\n vector<vector<vector<int>>>v(n);\n vector<int>vis(n,0);\n for(auto it:edges){\n v[it[0]].push_back({it[1],1});\n v[it[1]].push_back({it[0],0});\n\n } \n\n dfs(0,v,vis);\n\n return cnt;\n }\n};",
"memory": "177135"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& c){\n vector<list<int>>graph(n,list<int>());\n vector<unordered_set<int>>check(n);\n for(auto x:c){\n graph[x[0]].push_back(x[1]);\n graph[x[1]].push_back(x[0]);\n check[x[0]].insert(x[1]);\n } \n vector<int>visited(n,-1);\n queue<int>q;\n int cnt=0;\n q.push(0);\n while(q.size()){\n int src = q.front();\n q.pop();\n visited[src]=1;\n for(auto x:graph[src]){\n if(visited[x]!=-1) continue;\n if(check[x].find(src)==check[x].end()){\n cout<<src<<\" \"<<x<<endl;\n cnt++;\n }\n q.push(x);\n } \n }\n return cnt;\n }\n};",
"memory": "178589"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint swaps = 0;\n void dfs(unordered_map<int, vector<int>> outgoing, unordered_map<int, vector<int>> incoming, vector<bool> visited, int curr){\n stack<int> q;\n q.push(curr);\n \n while (!q.empty()){\n int top = q.top();\n q.pop();\n visited[top] = true;\n cout << top << endl;\n for (int out : outgoing[top]){\n if (!visited[out]){\n swaps++;\n q.push(out);\n }\n }\n for (int in : incoming[top]){\n if (!visited[in]){\n q.push(in);\n }\n }\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, vector<int>> outgoing(n);\n unordered_map<int, vector<int>> incoming(n);\n\n for (int i = 0; i < connections.size(); i++){\n outgoing[connections[i][0]].push_back(connections[i][1]);\n incoming[connections[i][1]].push_back(connections[i][0]);\n }\n vector<bool> visited(n, false);\n dfs(outgoing, incoming, visited, 0);\n return swaps;\n }\n};",
"memory": "178589"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int count=0;\n void dfs(vector<vector<int>> &adj, set<vector<int>>& st, vector<int>& vis, int s ){\n vis[s] = 1;\n\n for(auto x: adj[s])\n { \n if(!vis[x]){\n if(st.find({s, x}) != st.end()){\n count++;\n }\n dfs(adj, st, vis, x);\n }\n }\n }\n \n int minReorder(int n, vector<vector<int>>& connections) {\n set<vector<int>> st;\n vector<int> vis(n, 0);\n\n for(auto x: connections){\n st.insert(x);\n }\n\n vector<vector<int>> adj(n);\n for(auto x: connections){\n int u = x[0], v = x[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n for(int i=0; i<n; i++){\n if(!vis[i]){\n dfs(adj, st, vis, i);\n }\n }\n return count;\n }\n};",
"memory": "180043"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n int m=0;\n int x;\n unordered_map <int,vector<int>> mp,mp2;\n unordered_map<int,bool>vis;\n void trav(int i)\n {\n vis[i]=1;\n if(mp[i].size())\n {\n for(int j=0;j<mp[i].size();j++)\n if(vis[mp[i][j]]==0)\n m++,trav(mp[i][j]);\n }\n\n for(int j=0;j<mp2[i].size();j++)\n if(!vis[mp2[i][j]])\n {\n trav(mp2[i][j]);\n }\n }\npublic:\n int minReorder(int n, vector<vector<int>>& c) {\n x=n;\n for(int i=0;i<n-1;i++)\n {\n mp[c[i][0]].push_back(c[i][1]);\n mp2[c[i][1]].push_back(c[i][0]);\n vis[i]=0;\n }\n // for(int i=0;i<n;i++)\n // {\n // out<<i<<\" \";\n // for(auto a:mp2[i])\n // out<<a<<\" \";\n // out<<endl;\n // }\n vis[n-1]=0;\n trav(0);\n return m;\n }\n};",
"memory": "180043"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int,set<int>> graph ;\n unordered_map<int,vector<int>> unDirGraph;\n for(auto it: connections){\n unDirGraph[it[0]].push_back(it[1]);\n unDirGraph[it[1]].push_back(it[0]);\n\n graph[it[0]].insert(it[1]);\n }\n\n vector<bool> visi(n,0);\n queue<int> q;\n q.push(0);\n visi[0]=1;\n\n int cnt = 0;\n while(!q.empty()){\n int curr = q.front();\n q.pop();\n\n for(auto it: unDirGraph[curr]){\n if(!visi[it]){\n visi[it]=1;\n if(graph[it].count(curr)==0) cnt++;\n q.push(it);\n }\n }\n }\n\n return cnt;\n\n }\n};",
"memory": "181496"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void solve(vector<vector<vector<int>>>& adj,int node,vector<bool>& visited,int& swap){\n // int swap=0;\n visited[node]=1;\n for(auto neigh:adj[node]){\n if(visited[neigh[0]])continue;\n if(neigh[1]==0) swap++;\n visited[neigh[0]]=1;\n solve(adj,neigh[0],visited,swap);\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<vector<int>>> adj(n,vector<vector<int>>({}));\n for(auto i:connections){\n adj[i[0]].push_back({i[1],0});//0 means -> out fromi[0]\n adj[i[1]].push_back({i[0],1});\n }\n int ans=0;\n vector<bool> visited(n,0);\n solve(adj,0,visited,ans);\n return ans;\n }\n};",
"memory": "181496"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n unordered_map<int, vector<int>> graph;\n vector<bool> seen;\n set<vector<int>> roads;\n\npublic:\n int dfs(int node) {\n int rev = 0;\n for (int neighbor : graph[node]) {\n if (!seen[neighbor]) {\n // finding roads away from zero and if found rev will increment\n // indicating this road's direction should be reversed\n if (roads.find({node, neighbor}) != roads.end()) {\n rev++;\n }\n seen[neighbor] = true;\n rev += dfs(neighbor);\n }\n }\n return rev;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n // build the graph\n seen = vector(n, false);\n int e = connections.size();\n for (int i = 0; i < e; i++) {\n graph[connections[i][0]].push_back(connections[i][1]);\n graph[connections[i][1]].push_back(connections[i][0]);\n // add each edge to road as a pair in the direction of away from\n // zero\n roads.insert({connections[i][0], connections[i][1]});\n }\n seen[0] = true;\n return dfs(0);\n }\n};",
"memory": "182950"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\n unordered_map<int, vector<int>> graph;\n vector<bool> seen;\n set<vector<int>> roads;\n\npublic:\n int dfs(int node) {\n int rev = 0;\n for (int neighbor : graph[node]) {\n if (!seen[neighbor]) {\n // finding roads away from zero and if found rev will increment\n // indicating this road's direction should be reversed\n if (roads.find({node, neighbor}) != roads.end()) {\n rev++;\n }\n seen[neighbor] = true;\n rev += dfs(neighbor);\n }\n }\n return rev;\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n // build the graph\n seen = vector(n, false);\n int e = connections.size();\n for (int i = 0; i < e; i++) {\n graph[connections[i][0]].push_back(connections[i][1]);\n graph[connections[i][1]].push_back(connections[i][0]);\n // add each edge to road as a pair in the direction of away from\n // zero\n roads.insert({connections[i][0], connections[i][1]});\n }\n seen[0] = true;\n return dfs(0);\n }\n};",
"memory": "182950"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,vector<int>> graph; //assume undirected\n unordered_set<string> existing; //edges existing in original graph\n vector<bool> visited;\n int answer;\n\n void dfs(int node)\n {\n vector<int> neighbours = graph[node];\n for(int neighbour:neighbours)\n {\n if(!visited[neighbour])\n {\n visited[neighbour] = true;\n string key = to_string(node);\n key += \",\";\n key += to_string(neighbour);\n if(existing.find(key)!=existing.end())\n {\n //node-->neighbour exists; so neighbour-->node \n //does not exist. Hence flip node-->neighbour\n answer++;\n }\n dfs(neighbour);\n }\n }\n }\n\n int minReorder(int n, vector<vector<int>>& connections) {\n // create undirected graph from connections\n // and record existing connections\n for(auto connection:connections)\n {\n int start = connection[0];\n int end = connection[1];\n graph[start].push_back(end);\n graph[end].push_back(start);\n string key = to_string(start);\n key += \",\";\n key += to_string(end);\n existing.insert(key);\n }\n\n answer=0;\n visited = vector<bool>(n,false);\n visited[0] = true;\n dfs(0); \n return answer;\n }\n};",
"memory": "184404"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<unordered_set<int>> graph(n); // Original graph\n vector<unordered_set<int>> reverseGraph(n); // Reversed graph\n\n // Build the original and reversed graphs\n for (const auto& conn : connections) {\n graph[conn[0]].insert(conn[1]); // Add edge to original graph\n reverseGraph[conn[1]].insert(conn[0]); // Add reverse edge to reversed graph\n }\n\n int changes = 0; // Counter for the number of changes needed\n\n queue<int> q; // Queue for BFS\n vector<bool> visited(n, false); // Visited nodes tracker\n q.push(0); // Start BFS from node 0\n visited[0] = true;\n\n while (!q.empty()) {\n int city = q.front();\n q.pop();\n\n // Check roads in the original graph\n for (int neighbor : graph[city]) {\n if (!visited[neighbor]) {\n changes++; // Road needs reorientation\n q.push(neighbor); // Add to queue\n visited[neighbor] = true; // Mark as visited\n }\n }\n\n // Check roads in the reversed graph\n for (int neighbor : reverseGraph[city]) {\n if (!visited[neighbor]) {\n q.push(neighbor); // Add to queue\n visited[neighbor] = true; // Mark as visited\n }\n }\n }\n\n return changes; // Return the total number of changes needed\n }\n};",
"memory": "188765"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> adjList(n, vector<int>());\n unordered_map<int, unordered_set<int>> diEdge;\n \n for(auto it: connections) {\n adjList[it[0]].emplace_back(it[1]);\n adjList[it[1]].emplace_back(it[0]);\n diEdge[it[0]].insert(it[1]);\n }\n\n int cnt = 0;\n dfs(0, adjList, diEdge, -1, cnt);\n return cnt;\n }\n\nprivate:\n void dfs(int curr, vector<vector<int>> &adjList, unordered_map<int, unordered_set<int>> &diEdge, int par, int &cnt) {\n for(int i: adjList[curr]) {\n if(i == par) continue;\n if(diEdge[curr].find(i) != diEdge[curr].end()) cnt++;\n\n dfs(i, adjList, diEdge, curr, cnt);\n }\n }\n};",
"memory": "188765"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void dfs(int source, set<vector<int>>& connSet, vector<vector<int>>& ways, set<int>& visited, int& res){\n visited.insert(source);\n for(auto next : ways[source]){\n if(visited.count(next)) continue;\n if(!connSet.count({next, source})) res++;\n dfs(next, connSet, ways, visited, res);\n }\n }\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> ways(n);\n set<vector<int>> connSet(connections.begin(), connections.end());\n for(auto i : connections){\n ways[i[0]].push_back(i[1]);\n ways[i[1]].push_back(i[0]);\n }\n int res = 0;\n set<int> visited;\n dfs(0, connSet, ways, visited, res);\n return res;\n }\n};",
"memory": "190219"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "/*\nDo a Topo Sort to get the order of the graph.\nSince all nodes want to point to 0 therefore, 0 must be the last\nelement of the ordering == Number of swaps needed in Topo Sort.\n\nOR\n\nStart bfs from 0 and then check the nbrs if they point correctly\nelse reverse the direction. Only count Outgoing Edges at every node.\n*/\n/*\n edges:\n nbrs: \n 0 > 1, 4\n 1 > 3\n 2 > 3\n 3 > 1, 2\n 4 > 0, 5\n 5 > 4\n\n CONDITON OF FLIPPING IS nbr[node] = parent then don't flip\n */\n/*\nData Structure BS:\nFor every Node we need to know if the Node can reach the city.\nSo we need to store the edge (from_i, to_i) for every node in a map.\n\nAlso, we need to get the neighbours for every node (un directionally)\nso that we can check if the nbrs have edges towards or away from the\ncity 0 and thus we traverse.\n*/\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, unordered_set<int>> edges;\n unordered_map<int, vector<int>> nbrs;\n vector<bool> visited(n, false);\n int ans = 0;\n\n // Create a set of directed edges\n for (const auto& connection : connections) {\n edges[connection[0]].insert(connection[1]);\n }\n\n // Create adjacency list for both directions\n for (const auto& connection : connections) {\n int from = connection[0];\n int to = connection[1];\n nbrs[from].push_back(to);\n nbrs[to].push_back(from);\n }\n\n // BFS to traverse the graph starting from node 0\n queue<int> q;\n q.push(0);\n visited[0] = true;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n\n for (int neighbor : nbrs[node]) {\n if (!visited[neighbor]) {\n if (edges[node].count(neighbor)) {\n ans++;\n }\n q.push(neighbor);\n visited[neighbor] = true;\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "190219"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "/*\nDo a Topo Sort to get the order of the graph.\nSince all nodes want to point to 0 therefore, 0 must be the last\nelement of the ordering == Number of swaps needed in Topo Sort.\n\nOR\n\nStart bfs from 0 and then check the nbrs if they point correctly\nelse reverse the direction. Only count Outgoing Edges at every node.\n*/\n/*\n edges:\n nbrs: \n 0 > 1, 4\n 1 > 3\n 2 > 3\n 3 > 1, 2\n 4 > 0, 5\n 5 > 4\n\n CONDITON OF FLIPPING IS nbr[node] = parent then don't flip\n */\n/*\nData Structure BS:\nFor every Node we need to know if the Node can reach the city.\nSo we need to store the edge (from_i, to_i) for every node in a map.\n\nAlso, we need to get the neighbours for every node (un directionally)\nso that we can check if the nbrs have edges towards or away from the\ncity 0 and thus we traverse.\n*/\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n unordered_map<int, unordered_set<int>> edges;\n unordered_map<int, vector<int>> nbrs;\n vector<bool> visited(n, false);\n int ans = 0;\n\n // Create a set of directed edges\n for (const auto& connection : connections) {\n edges[connection[0]].insert(connection[1]);\n }\n\n // Create adjacency list for both directions\n for (const auto& connection : connections) {\n int from = connection[0];\n int to = connection[1];\n nbrs[from].push_back(to);\n nbrs[to].push_back(from);\n }\n\n // BFS to traverse the graph starting from node 0\n queue<int> q;\n q.push(0);\n visited[0] = true;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n\n for (int neighbor : nbrs[node]) {\n if (!visited[neighbor]) {\n if (edges[node].count(neighbor)) {\n ans++;\n }\n q.push(neighbor);\n visited[neighbor] = true;\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "191673"
} |
1,576 | <p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.</p>
<p>Roads are represented by <code>connections</code> where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a road from city <code>a<sub>i</sub></code> to city <code>b<sub>i</sub></code>.</p>
<p>This year, there will be a big event in the capital (city <code>0</code>), and many people want to travel to this city.</p>
<p>Your task consists of reorienting some roads such that each city can visit the city <code>0</code>. Return the <strong>minimum</strong> number of edges changed.</p>
<p>It's <strong>guaranteed</strong> that each city can reach city <code>0</code> after reorder.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_1_1819.png" style="width: 311px; height: 189px;" />
<pre>
<strong>Input:</strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/05/13/sample_2_1819.png" style="width: 509px; height: 79px;" />
<pre>
<strong>Input:</strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3, connections = [[1,0],[2,0]]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= n <= 5 * 10<sup>4</sup></code></li>
<li><code>connections.length == n - 1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>
| 3 | {
"code": "/*\nDo a Topo Sort to get the order of the graph.\nSince all nodes want to point to 0 therefore, 0 must be the last\nelement of the ordering == Number of swaps needed in Topo Sort.\n\nOR\n\nStart bfs from 0 and then check the nbrs if they point correctly\nelse reverse the direction. Only count Outgoing Edges at every node.\n*/\n/*\n edges:\n nbrs: \n 0 > 1, 4\n 1 > 3\n 2 > 3\n 3 > 1, 2\n 4 > 0, 5\n 5 > 4\n\n CONDITON OF FLIPPING IS edges[nbr] = parent then don't flip\n */\n/*\nData Structure BS:\nFor every Node we need to know if the Node can reach the city.\nSo we need to store the edge (from_i, to_i) for every node in a map.\n\nAlso, we need to get the neighbours for every node (un directionally)\nso that we can check if the nbrs have edges towards or away from the\ncity 0 and thus we traverse.\n*/\n\n/*\nTo ENSURE edges[node] equals neighbor. This requires ensuring that\neach node can only have one direct edge in the map, which might \nnot be practical for multiple outgoing edges. So, if we need to flip\nboth edges of a node then edges[node] would give only one node which\nis incorrect. So, use a set or find a simpler solution\n*/\n\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n // edges stores the directed edges\n unordered_map<int, unordered_set<int>> edges;\n // nbrs stores the undirected edges for BFS traversal\n unordered_map<int, vector<int>> nbrs;\n vector<bool> visited(n, false);\n int ans = 0;\n\n // Populate the edges map with directed edges\n for (const auto& connection : connections) {\n edges[connection[0]].insert(connection[1]);\n }\n\n // Populate the adjacency list for undirected graph\n for (const auto& connection : connections) {\n int from = connection[0];\n int to = connection[1];\n nbrs[from].push_back(to);\n nbrs[to].push_back(from);\n }\n\n // BFS to traverse the graph starting from node 0\n queue<int> q;\n q.push(0);\n visited[0] = true;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n\n for (int neighbor : nbrs[node]) {\n if (!visited[neighbor]) {\n // If the directed edge exists from node to neighbor, it needs to be reordered\n if (edges[node].count(neighbor)) {\n ans++;\n }\n q.push(neighbor);\n visited[neighbor] = true;\n }\n }\n }\n\n return ans;\n }\n};\n",
"memory": "191673"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> ans(2*n,-1);\n int p1 = 0, p2 = n;\n for(int i = 0; i < nums.size(); i=i+2){\n ans[i] = nums[p1++];\n ans[i+1] = nums[p2++];\n }\n return ans;\n }\n};",
"memory": "11900"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n std::vector<int> shuffle(vector<int>& nums, int n) {\n std::vector<int> shuffledNums;\n shuffledNums.reserve(nums.size());\n\n for (int i{}, j{ n }; j < nums.size(); ++i, ++j) {\n shuffledNums.emplace_back(nums[i]);\n shuffledNums.emplace_back(nums[j]);\n }\n\n return shuffledNums;\n }\n};",
"memory": "12000"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> res(n * 2);\n for (int i = 0; i < n; i++) {\n res[i * 2] = nums[i];\n res[i * 2 + 1] = nums[n + i];\n }\n return res;\n }\n};",
"memory": "12100"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> array(nums.size());\n int i=0,j=0;\n while(j!=n)\n {\n array[i+1]=nums[j+n];\n array[i]=nums[j++];\n i=i+2;\n }\n return array;\n }\n};",
"memory": "12100"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> res(2*n);\n for(int i=0;i<n;i++){\n res[2*i]=nums[i];\n res[2*i+1] = nums[i+n];\n }\n return res;\n }\n};",
"memory": "12200"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int>v;\n for(int i=0;i<=(n-1);i++){\n v.push_back(nums[i]);\n v.push_back(nums[i+n]);\n }\n return v; \n }\n};",
"memory": "12200"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> output;\n for(int i=0; i<n; i++)\n {\n output.push_back(nums[i]);\n output.push_back(nums[n+i]);\n }\n return output;\n }\n};",
"memory": "12300"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 0 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n int i=0;\n int j=n;\n int k=0;\n int ma=1e3+1;\n while(i<n||j<2*n){\n if(k%2){\n nums[k]=(nums[j]%ma)*ma+nums[k];\n j++;\n }\n else{\n nums[k]=(nums[i]%ma)*ma+nums[k];\n i++;\n }\n k++;\n }\n k=0;\n while(k<2*n){\n nums[k]=nums[k]/ma;\n k++;\n }\n return nums;\n }\n};",
"memory": "12300"
} |
1,580 | <p>Given the array <code>nums</code> consisting of <code>2n</code> elements in the form <code>[x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>,y<sub>1</sub>,y<sub>2</sub>,...,y<sub>n</sub>]</code>.</p>
<p><em>Return the array in the form</em> <code>[x<sub>1</sub>,y<sub>1</sub>,x<sub>2</sub>,y<sub>2</sub>,...,x<sub>n</sub>,y<sub>n</sub>]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,1,3,4,7], n = 3
<strong>Output:</strong> [2,3,5,4,1,7]
<strong>Explanation:</strong> Since x<sub>1</sub>=2, x<sub>2</sub>=5, x<sub>3</sub>=1, y<sub>1</sub>=3, y<sub>2</sub>=4, y<sub>3</sub>=7 then the answer is [2,3,5,4,1,7].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,4,3,2,1], n = 4
<strong>Output:</strong> [1,4,2,3,3,2,4,1]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,2,2], n = 2
<strong>Output:</strong> [1,2,1,2]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 500</code></li>
<li><code>nums.length == 2n</code></li>
<li><code>1 <= nums[i] <= 10^3</code></li>
</ul> | 1 | {
"code": "class Solution {\npublic:\n vector<int> shuffle(vector<int>& nums, int n) {\n vector<int> ans(2*n);\n int j=0;\n for(int i=0;i<2*n;i++){\n ans[i]=nums[j];\n ans[++i]=nums[j+n];\n j++;\n }\n return ans;\n }\n};",
"memory": "12400"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minCost(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target) {\n const int INF = 0x3f3f3f3f;\n int f[target][n + 3];\n memset(f, 0x3f, sizeof f);\n memset(f[0], 0, sizeof f[0]);\n for (int i = 0; i < m; i++) {\n for (int section = min(i, target - 1); section >= 0; section--) {\n f[section][n + 1] = INF;\n f[section][n + 2] = INF;\n for (int color = 0; color < n; color++) {\n if (houses[i] != 0 && color != houses[i] - 1) {\n //房子已经有颜色,则不能再刷其他颜色了,跳过无意义的color\n f[section][color] = INF;\n continue;\n }\n f[section][color] = min(f[section][color],\n section == 0 ? INF : color == f[section - 1][n] ? f[section - 1][n + 2] : f[\n section - 1][n + 1]) + (houses[i] == 0 ? cost[i][color] : 0);\n\n //更新f[i][n], f[i][n+1], f[i][n+2]\n if (f[section][color] < f[section][n + 1]) {\n f[section][n] = color;\n f[section][n + 2] = f[section][n + 1];\n f[section][n + 1] = f[section][color];\n } else if (f[section][color] < f[section][n + 2]) {\n f[section][n + 2] = f[section][color];\n }\n }\n }\n }\n return f[target - 1][n + 1] < INF ? f[target - 1][n + 1] : -1;\n\n }\n};",
"memory": "12593"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n int dp[n+1][target+1];\n\n // for (int i=0; i<m; i++){\n for (int j=0; j<=n; j++){\n for (int k=0; k<=target; k++){\n dp[j][k]=INT_MAX/2;\n }\n }\n // }\n \n if (houses[0]==0){\n for (int i=1; i<=n; i++)dp[i][1]=cost[0][i-1];\n }else{\n dp[houses[0]][1]=0;\n }\n for (int i=1; i<m; i++){\n int dtemp[n+1][target+1];\n for (int j=0; j<=n; j++){\n for (int k=0; k<=target; k++){\n dtemp[j][k]=dp[j][k];\n dp[j][k]=INT_MAX/2;\n }\n }\n for (int k=1; k<=target; k++){\n int temp[n+1], m=INT_MAX/2;\n for (int j=1; j<=n; j++){\n temp[j]=m;\n m=min(m, dtemp[j][k-1]);\n }\n m=INT_MAX/2;\n for (int j=n; j>=1; j--){\n temp[j]=min(temp[j], m);\n m=min(m, dtemp[j][k-1]);\n }\n for (int j=1; j<=n; j++){\n if (houses[i]==0){\n dp[j][k]=dtemp[j][k]+cost[i][j-1];\n dp[j][k]=min(dp[j][k], temp[j]+cost[i][j-1]);\n }else{\n if (j==houses[i]){\n dp[j][k]=dtemp[j][k];\n dp[j][k]=min(dp[j][k], temp[j]);\n }\n }\n cout<<dp[j][k]<<\" \";\n }\n cout<<endl;\n }\n cout<<endl;\n }\n int ans=INT_MAX/2;\n for (int j=1; j<=n; j++){\n ans=min(ans, dp[j][target]);\n }\n if (ans==INT_MAX/2)return -1;\n return ans;\n }\n};",
"memory": "12593"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"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(t + 1, vector<int>(n + 3, M));\n fill(v[1].begin(), v[1].end(), 0);\n for(int i = 0; i < m; ++i)\n for(int k = min(t, i + 1); k > 0; --k)\n {\n v[k][n + 1] = v[k][n + 2] = M;\n for(int c = 0; c < n; ++c)\n if(A[i] && A[i] - 1 != c)\n v[k][c] = M;\n else\n {\n int p = (A[i] ? 0 : B[i][c]) + \n min(v[k][c], v[k - 1][n + 1 + (v[k - 1][n] == c)]);\n if(p <= v[k][n + 1])\n {\n v[k][n + 2] = v[k][n + 1];\n v[k][n + 1] = p;\n v[k][n] = c;\n }\n else if(p < v[k][n + 2])\n v[k][n + 2] = p;\n v[k][c] = p;\n }\n }\n return v[t][n + 1] < M ? v[t][n + 1] : -1;\n }\n};",
"memory": "12779"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "const auto __ = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n const auto& house_cnt = m;\n const auto& color_cnt = n;\n vector<vector<int32_t>> v(target + 1, vector<int32_t>(color_cnt + 3, kOutOfBounds));\n fill(v[1].begin(), v[1].end(), 0);\n for (int32_t i = 0; i < house_cnt; ++i) {\n // Check groups from painting all different to painting all with the same color\n for (int32_t grp = min(target, i + 1); grp > 0; --grp) {\n v[grp][color_cnt + 1] = v[grp][color_cnt + 2] = kOutOfBounds;\n for (int32_t color = 0; color < color_cnt; ++color) {\n if (houses[i] != kUnpainted && houses[i] - 1 != color) {\n v[grp][color] = kOutOfBounds;\n } else {\n const int32_t costs =\n (houses[i] != kUnpainted ? 0 : cost[i][color]) +\n min(v[grp][color], v[grp - 1][color_cnt + 1 + (v[grp - 1][color_cnt] == color ? 1 : 0)]);\n if (costs <= v[grp][color_cnt + 1]) {\n v[grp][color_cnt + 2] = v[grp][color_cnt + 1];\n v[grp][color_cnt + 1] = costs;\n v[grp][color_cnt] = color;\n } else if (costs < v[grp][color_cnt + 2]) {\n v[grp][color_cnt + 2] = costs;\n }\n v[grp][color] = costs;\n }\n }\n }\n }\n return v[target][color_cnt + 1] < kOutOfBounds ? v[target][color_cnt + 1] : -1;\n }\n\n private:\n static constexpr int32_t kUnpainted = 0;\n static constexpr int32_t kOutOfBounds = 1e7; \n};",
"memory": "12965"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"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(t + 1, vector<int>(n + 3, M));\n fill(v[1].begin(), v[1].end(), 0);\n for(int i = 0; i < m; ++i)\n for(int k = min(t, i + 1); k > 0; --k)\n {\n v[k][n + 1] = v[k][n + 2] = M;\n for(int c = 0; c < n; ++c)\n if(A[i] && A[i] - 1 != c)\n v[k][c] = M;\n else\n {\n int p = (A[i] ? 0 : B[i][c]) + \n min(v[k][c], v[k - 1][n + 1 + (v[k - 1][n] == c)]);\n if(p <= v[k][n + 1])\n {\n v[k][n + 2] = v[k][n + 1];\n v[k][n + 1] = p;\n v[k][n] = c;\n }\n else if(p < v[k][n + 2])\n v[k][n + 2] = p;\n v[k][c] = p;\n }\n }\n return v[t][n + 1] < M ? v[t][n + 1] : -1;\n }\n};",
"memory": "13151"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n int dp[101][21][101];\n \n int solve(int i,int l,int t,vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target){\n \n if(t>target){\n return INT_MAX/2;\n }\n \n if(i == m){\n return (t==target)?0:INT_MAX/2;\n }\n \n if(dp[i][l][t] != -1){\n return dp[i][l][t];\n }else{\n int ans = INT_MAX/2;\n \n if(houses[i] == 0){\n \n for(int j=0;j<n;j++){\n ans = min(ans,cost[i][j]+solve(i+1,j+1,(l==j+1)?t:t+1,houses,cost,m,n,target));\n }\n \n }else{\n ans = min(ans,solve(i+1,houses[i],(houses[i] == l)?t:t+1,houses,cost,m,n,target));\n }\n return dp[i][l][t] = ans;\n }\n }\n \n \n \n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n \n memset(dp,-1,sizeof dp);\n \n int ans = solve(0,0,0,houses,cost,m,n,target);\n return ans==INT_MAX/2?-1:ans;\n \n }\n};",
"memory": "13151"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint dp[101][21][101];\nint rec(int level,int colour,int nei,vector<int>& houses, vector<vector<int>>& cost,int target){\n if(nei>target)return INT_MAX;\n if(level==houses.size()){\n if(nei==target)\n return 0;\n else return INT_MAX;}\n if(dp[level][colour+1][nei]!=-1)return dp[level][colour+1][nei];\n if(houses[level]!=0){\n int nneig;\n colour==houses[level]-1?nneig=nei:nneig=nei + 1;\n return dp[level][colour+1][nei]=rec(level+1,houses[level]-1,nneig,houses,cost,target);\n }\n int ans=INT_MAX;\n int n=cost[0].size();\n for(int i=0;i<n;i++){\n int nneig;\n colour==i?nneig=nei:nneig=nei+1;\n int temp=rec(level+1,i,nneig,houses,cost,target);\n if(temp!=INT_MAX)temp+=cost[level][i];\n ans=min(ans,temp);\n }\n return dp[level][colour+1][nei]=ans;\n\n\n}\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n memset(dp,-1,sizeof(dp));\n int ans=rec(0,-1,0,houses,cost,target);\n return ans==INT_MAX?-1:ans;\n }\n};",
"memory": "13338"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // Assign the size as per maximum value for different params\n int memo[100][100][21];\n // Maximum cost possible plus 1\n const int MAX_COST = 1000001;\n \n int findMinCost(vector<int>& houses, vector<vector<int>>& cost, int targetCount, int currIndex,\n int neighborhoodCount, int prevHouseColor) {\n if (currIndex == houses.size()) {\n // If all houses are traversed, check if the neighbor count is as expected or not\n return neighborhoodCount == targetCount ? 0 : MAX_COST;\n }\n \n if (neighborhoodCount > targetCount) {\n // If the neighborhoods are more than the threshold, we can't have target neighborhoods\n return MAX_COST;\n }\n \n // We have already calculated the answer so no need to go into recursion\n if (memo[currIndex][neighborhoodCount][prevHouseColor] != -1) {\n return memo[currIndex][neighborhoodCount][prevHouseColor];\n }\n \n int minCost = MAX_COST;\n // If the house is already painted, update the values accordingly\n if (houses[currIndex]) {\n int newNeighborhoodCount = neighborhoodCount + (houses[currIndex] != prevHouseColor);\n minCost = \n findMinCost(houses, cost, targetCount, currIndex + 1, newNeighborhoodCount, houses[currIndex]);\n } else {\n int totalColors = cost[0].size();\n \n // If the house is not painted, try every possible color and store the minimum cost\n for (int color = 1; color <= totalColors; color++) {\n int newNeighborhoodCount = neighborhoodCount + (color != prevHouseColor);\n int currCost = cost[currIndex][color - 1]\n + findMinCost(houses, cost, targetCount, currIndex + 1, newNeighborhoodCount, color);\n minCost = min(minCost, currCost);\n }\n }\n \n // Return the minimum cost and also storing it for future reference (memoization)\n return memo[currIndex][neighborhoodCount][prevHouseColor] = minCost;\n }\n \n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // Marking all values to -1 to denote that we don't have the answer ready for these params yet\n memset(memo, -1, sizeof(memo));\n int answer = findMinCost(houses, cost, target, 0, 0, 0);\n \n // Return -1 if the answer is MAX_COST as it implies no answer possible\n return answer == MAX_COST ? -1 : answer;\n }\n};",
"memory": "13338"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n /*\n dp[i][j][k] = minimum cost with houses[0: i] and k group and the last group color\n is color[j]\n \n dp[i][j][k] = dp[i-1][j][k] + cost[i][j]\n dp[i-1][x][k-1] + cost[i][j], x!=j\n */\n int dp[m][n+1][target+1];\n for(int i=0; i<m; i++){\n for(int j=0; j<=n; j++){\n for(int k=0; k<=target; k++){\n dp[i][j][k] = INT_MAX/2;\n }\n }\n }\n for(int i=0; i<m; i++){\n int choose_color = houses[i];\n for(int neighbor=1; neighbor<=min(i+1, target); neighbor++){\n if(choose_color!=0){\n for(int color=1; color<=n; color++){\n if(color==choose_color) \n dp[i][choose_color][neighbor] = min(dp[i][choose_color][neighbor], \n (i>0 ?dp[i-1][color][neighbor] :0));\n else\n dp[i][choose_color][neighbor] = min(dp[i][choose_color][neighbor], \n (i>0 ?dp[i-1][color][neighbor-1] :0));\n }\n continue;\n }\n for(int choose_color=1; choose_color<=n; choose_color++){\n for(int color=1; color<=n; color++){\n if(color==choose_color){\n dp[i][choose_color][neighbor] = min(dp[i][choose_color][neighbor], \n (i>0 ?dp[i-1][color][neighbor] :0) + cost[i][choose_color-1]);\n }\n else{\n dp[i][choose_color][neighbor] = min(dp[i][choose_color][neighbor], \n (i>0 ?dp[i-1][color][neighbor-1] :0) + cost[i][choose_color-1]);\n }\n }\n }\n }\n }\n int min_cost = INT_MAX;\n for(int color=1; color<=n; color++) min_cost = min(min_cost, dp[m-1][color][target]);\n return min_cost==INT_MAX/2 ?-1 :min_cost;\n }\n};",
"memory": "13524"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // Assign the size as per maximum value for different params\n int memo[100][100][21];\n // Maximum cost possible plus 1\n const int MAX_COST = 1000001;\n \n int findMinCost(vector<int>& houses, vector<vector<int>>& cost, int targetCount, int currIndex,\n int neighborhoodCount, int prevHouseColor) {\n if (currIndex == houses.size()) {\n // If all houses are traversed, check if the neighbor count is as expected or not\n return neighborhoodCount == targetCount ? 0 : MAX_COST;\n }\n \n if (neighborhoodCount > targetCount) {\n // If the neighborhoods are more than the threshold, we can't have target neighborhoods\n return MAX_COST;\n }\n \n // We have already calculated the answer so no need to go into recursion\n if (memo[currIndex][neighborhoodCount][prevHouseColor] != -1) {\n return memo[currIndex][neighborhoodCount][prevHouseColor];\n }\n \n int minCost = MAX_COST;\n // If the house is already painted, update the values accordingly\n if (houses[currIndex]) {\n int newNeighborhoodCount = neighborhoodCount + (houses[currIndex] != prevHouseColor);\n minCost = \n findMinCost(houses, cost, targetCount, currIndex + 1, newNeighborhoodCount, houses[currIndex]);\n } else {\n int totalColors = cost[0].size();\n \n // If the house is not painted, try every possible color and store the minimum cost\n for (int color = 1; color <= totalColors; color++) {\n int newNeighborhoodCount = neighborhoodCount + (color != prevHouseColor);\n int currCost = cost[currIndex][color - 1]\n + findMinCost(houses, cost, targetCount, currIndex + 1, newNeighborhoodCount, color);\n minCost = min(minCost, currCost);\n }\n }\n \n // Return the minimum cost and also storing it for future reference (memoization)\n return memo[currIndex][neighborhoodCount][prevHouseColor] = minCost;\n }\n \n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // Marking all values to -1 to denote that we don't have the answer ready for these params yet\n memset(memo, -1, sizeof(memo));\n int answer = findMinCost(houses, cost, target, 0, 0, 0);\n \n // Return -1 if the answer is MAX_COST as it implies no answer possible\n return answer == MAX_COST ? -1 : answer;\n }\n};",
"memory": "13524"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[101][101][22];\n int solve(vector<int>& h, vector<vector<int>>& c, int n, int m, int t,int i,int p){\n if(i>=n && t==0)return 0;\n if(i>=n || t<0)return 1e9;\n \n if(dp[i][t][p+1]!=-1)return dp[i][t][p+1];\n\n int ans=INT_MAX;\n\n if(h[i]!=0){\n if(p==h[i]-1){\n ans=min(ans,solve(h,c,n,m,t,i+1,p));\n }\n else{\n ans=min(ans,solve(h,c,n,m,t-1,i+1,h[i]-1));\n } \n }\n else{\n for(int j=0;j<m;j++){\n if(p==j){\n ans=min(ans,c[i][j]+solve(h,c,n,m,t,i+1,j));\n }\n else{\n ans=min(ans,c[i][j]+solve(h,c,n,m,t-1,i+1,j));\n }\n }\n\n }\n return dp[i][t][p+1]=ans;\n\n }\n int minCost(vector<int>& h, vector<vector<int>>& c, int n, int m, int t) {\n memset(dp,-1,sizeof(dp));\n int ans=solve(h,c,n,m,t,0,-1);\n return (ans>=1e9)?-1:ans;\n }\n};",
"memory": "13710"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n #define vvi vector<vector<int>>\n #define vi vector<int>\n\n #define dbg(x) cout << #x << \" : \" << x << endl\n\n int dp[105][105][25];\n int f(int pos , int k , int last , int &target , vi &a , vvi &cost){\n int m = cost.size();\n int n = cost[0].size();\n assert(m == a.size());\n\n if(k > target){\n return 1e9;\n }\n\n if(pos == m){\n // have to end here!\n if(k == target)return 0;\n else return 1e9;\n }\n // dbg(pos);\n // dbg(k);\n // dbg(last);\n\n if(dp[pos][k][last] != -1)return dp[pos][k][last];\n\n // can start a new sequence from here (pos) or continue\n int ans = 1e9;\n if(a[pos] == 0){\n for(int i=1;i<=n;i++){\n int paint = cost[pos][i-1];\n if(a[pos] == i){\n paint = 0;\n }\n if(i == last){\n // continue\n ans = min(ans , paint + f(pos+1 , k , last , target , a , cost));\n }\n else{\n // new partition\n ans = min(ans , paint + f(pos+1 , k+1 , i , target , a , cost));\n }\n }\n }\n else{\n int k_ = k;\n if(a[pos] != last)k_++;\n ans = f(pos + 1 , k_ , a[pos] , target , a , cost);\n }\n \n return dp[pos][k][last] = ans;\n }\n int minCost(vector<int>& a, vector<vector<int>>& cost, int m, int n, int target) {\n memset(dp , -1 , sizeof(dp));\n int fans = 1e9;\n\n // CANT REPAINT A ALREADY PAINTED HOUSE!!\n // THAT'S WHY I GOT WA\n\n if(a[0] == 0){\n for(int i=1;i<=n;i++){\n int paint = cost[0][i-1];\n if(a[0] == i){\n paint = 0;\n }\n fans = min(fans , paint + f(1 , 1 , i , target , a , cost));\n }\n }\n else{\n fans = f(1 , 1 , a[0] , target , a , cost);\n }\n if(fans == 1e9)return -1;\n return fans;\n }\n};",
"memory": "13710"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n int dp[101][101][21];\n vector<vector<int>>cost;\n int target;\n vector<int>houses;\n int m,n;\n int solve(int i,int t,int prev){\n // cout<<i<<' '<<t<<' '<<prev<<endl;\n if(i==m && target==t)return 0;\n if(t>target)return (1e6+1);\n if(i==m)return (int)(1e6+1);\n if(dp[i][t][prev+1]!=-1)return dp[i][t][prev+1];\n int ans=1e6+1;\n if(houses[i]!=0){\n if(houses[i]==prev+1)return dp[i][t][prev+1]=solve(i+1,t,prev);\n else return dp[i][t][prev+1]=solve(i+1,t+1,houses[i]-1);\n }\n for(int c=0;c<n;c++){\n \n if(c==prev){\n ans=min(ans,cost[i][c]+solve(i+1,t,c));\n }\n else ans=min(ans,cost[i][c]+solve(i+1,t+1,c));\n }\n return dp[i][t][prev+1]=ans;\n\n }\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->cost=cost;\n this->m=m;\n this->n=n;\n this->target=target;\n this->houses=houses;\n memset(dp,-1,sizeof(dp));\n int ans=solve(0,0,-1);\n return ans==1e6+1?-1:ans;\n }\n};",
"memory": "13896"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n vector<int> houses;\n vector<vector<int>> cost;\n int m, n, t, dp[100][101][21];\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->houses = houses;\n this->cost = cost;\n this->m = m, this->n = n, this->t = target;\n\n memset(dp, -1, sizeof(dp));\n f(0, 0, 0);\n\n return dp[0][0][0] >= 1e8 ? -1 : dp[0][0][0];\n }\n\n int f(int i, int gr, int prv) {\n if(i >= m) {\n if(gr != t) return 1e8;\n else return 0;\n }\n if(dp[i][gr][prv] != -1) return dp[i][gr][prv];\n\n int miniCost = INT_MAX;\n if(!houses[i]) {\n for(int j = 0; j < n; ++j) {\n if(prv != j + 1) miniCost = min(miniCost, cost[i][j] + f(i + 1, gr + 1, j + 1));\n else miniCost = min(miniCost, cost[i][j] + f(i + 1, gr, prv));\n }\n } else {\n if(prv != houses[i]) {\n miniCost = min(miniCost, f(i + 1, gr + 1, houses[i]));\n } else {\n miniCost = min(miniCost, f(i + 1, gr, prv));\n }\n }\n\n return dp[i][gr][prv] = miniCost;\n }\n};",
"memory": "14083"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n vector<int> houses;\n vector<vector<int>> cost;\n int m, n, t, dp[100][101][21];\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->houses = houses;\n this->cost = cost;\n this->m = m, this->n = n, this->t = target;\n\n memset(dp, -1, sizeof(dp));\n f(0, 0, 0);\n\n return dp[0][0][0] >= 1e8 ? -1 : dp[0][0][0];\n }\n\n int f(int i, int gr, int prv) {\n if(i >= m) {\n if(gr != t) return 1e8;\n else return 0;\n }\n if(gr > t) return 1e8;\n if(dp[i][gr][prv] != -1) return dp[i][gr][prv];\n\n int miniCost = INT_MAX;\n if(!houses[i]) {\n for(int j = 0; j < n; ++j) {\n if(prv != j + 1) miniCost = min(miniCost, cost[i][j] + f(i + 1, gr + 1, j + 1));\n else miniCost = min(miniCost, cost[i][j] + f(i + 1, gr, prv));\n }\n } else {\n if(prv != houses[i]) {\n miniCost = min(miniCost, f(i + 1, gr + 1, houses[i]));\n } else {\n miniCost = min(miniCost, f(i + 1, gr, prv));\n }\n }\n\n return dp[i][gr][prv] = miniCost;\n }\n};",
"memory": "14083"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nvector<int> houses;\nvector<vector<int>> cost;\nint m,n;\nint dp[101][25][101];\nint solve(int index,int prevColor,int target){\n if(target<0)\n return 1e7;\n if(index==houses.size()){\n if(target==0)\n return 0;\n\n else\n return 1e7;\n }\n\n if(dp[index][prevColor+1][target]!=-1)\n return dp[index][prevColor+1][target];\n\n if(houses[index]!=0){\n if(houses[index]!=prevColor)\n return dp[index][prevColor+1][target]= solve(index+1,houses[index],target-1);\n else{\n return dp[index][prevColor+1][target]= solve(index+1,houses[index],target);\n } \n }\n else{\n int mini=INT_MAX;\n\n for(int i=1;i<=n;i++){\n if(i==prevColor){\n mini=min(mini,cost[index][i-1]+solve(index+1,i,target));\n }\nelse\n mini=min(cost[index][i-1]+solve(index+1,i,target-1),mini);\n }\n\n return dp[index][prevColor+1][target]= mini;\n }\n}\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->houses=houses;\n this->cost=cost;\n this->m=m;\n this->n=n;\n memset(dp,-1,sizeof(dp));\n //this->target=target;\n\n int ans= solve(0,-1,target);\n if(ans>=1e7)\n return -1;\n return ans;\n }\n};",
"memory": "14269"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n// m - no of houses\n// n - no of colors\n vector<int> house;\n vector<vector<int>> cost;\n int n, m;\n int dp[105][105][25];\n int find(int ind, int rem, int prev){\n if(ind == m){\n if(rem == 0) return 0;\n return 1e9;\n }\n if(rem < 0) return 1e9;\n if(dp[ind][rem][prev] != -1) return dp[ind][rem][prev];\n int ans = 1e9;\n if(house[ind] == 0){\n for(int i = 0; i < n; i++){\n int color = i + 1;\n int costtopaint = cost[ind][i];\n if(color == prev)\n ans = min(ans, costtopaint + find(ind + 1, rem, color));\n else\n ans = min(ans, costtopaint + find(ind + 1, rem - 1, color));\n }\n }\n else{\n int color = house[ind];\n if(color == prev)\n ans = min(ans, find(ind + 1, rem, color));\n else\n ans = min(ans, find(ind + 1, rem - 1, color));\n }\n return dp[ind][rem][prev] = ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->house = houses;\n this->cost = cost;\n this->n = n;\n this->m = m;\n for(int i = 0; i < 105; i++)\n for(int j = 0; j < 105; j++)\n for(int k = 0; k < 25; k++)\n dp[i][j][k] = -1;\n \n int ans = find(0, target, 0);\n if(ans >= 1e9)\n return -1;\n return ans;\n }\n};",
"memory": "14269"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n int dp[201][201][21];\n const int MAX = 1000000000;\n\n int dfs(vector<int>& houses, vector<vector<int>>& cost, int i, int target, int lastColor, int n) {\n if (target < 0) {\n return MAX; \n }\n\n if (i >= houses.size()) {\n return target == 0 ? 0 : MAX;\n }\n\n if (dp[i][target][lastColor] != -1) {\n return dp[i][target][lastColor];\n }\n\n if (houses[i] != 0) {\n int newTarget = target - (lastColor != houses[i] ? 1 : 0);\n return dp[i][target][lastColor] = dfs(houses, cost, i + 1, newTarget, houses[i], n);\n }\n\n int ans = MAX;\n for (int color = 1; color <= n; ++color) {\n int newTarget = target - (lastColor != color ? 1 : 0);\n if (newTarget >= 0) {\n ans = min(ans, cost[i][color - 1] + dfs(houses, cost, i + 1, newTarget, color, n));\n }\n }\n\n return dp[i][target][lastColor] = ans;\n }\n\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n memset(dp, -1, sizeof(dp)); // Initialize the dp array with -1\n\n int ans = dfs(houses, cost, 0, target, 0, n);\n\n return ans == MAX ? -1 : ans;\n }\n};\n",
"memory": "15200"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint M,N;\nint tar;\nint dp[101][101][101];\n int rec(int i,vector<vector<int>>& cost,vector<int>& house,int neighbours,int pre){\n if(i==M){\n if(neighbours==tar){\n return 0;\n }else{\n return 1e9;\n }\n }\n\n if(dp[i][neighbours][pre+10]!=-1) return dp[i][neighbours][pre+10];\n\n int ans=1e9;\n if(house[i]){\n if(house[i]!=pre){\n ans=min(ans,rec(i+1,cost,house,neighbours+1,house[i]));\n \n }else{\n ans=min(ans,rec(i+1,cost,house,neighbours,house[i]));\n }\n }else{\n for(int paint=0;paint<cost[i].size();paint++){\n if((paint+1)!=pre){\n ans=min(ans,cost[i][paint]+rec(i+1,cost,house,neighbours+1,paint+1));\n }else{\n ans=min(ans,cost[i][paint]+rec(i+1,cost,house,neighbours,paint+1));\n }\n }\n } \n\n return dp[i][neighbours][pre+10]=ans; \n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n M=m;\n N=n;\n tar=target;\n memset(dp,-1,sizeof(dp));\n int ans=rec(0,cost,houses,0,-1);\n if(ans==1e9){\n return -1;\n }\n\n return ans;\n\n\n }\n};",
"memory": "15386"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[101][101][102];\n int solve(vector<int>& h, vector<vector<int>>& c, int n, int m, int t,int i,int p){\n if(i>=n && t==0)return 0;\n if(i>=n || t<0)return 1e9;\n \n if(dp[i][t][p+1]!=-1)return dp[i][t][p+1];\n\n int ans=INT_MAX;\n\n if(h[i]!=0){\n if(p==h[i]-1){\n ans=min(ans,solve(h,c,n,m,t,i+1,p));\n }\n else{\n ans=min(ans,solve(h,c,n,m,t-1,i+1,h[i]-1));\n } \n }\n else{\n for(int j=0;j<m;j++){\n if(p==j){\n ans=min(ans,c[i][j]+solve(h,c,n,m,t,i+1,j));\n }\n else{\n ans=min(ans,c[i][j]+solve(h,c,n,m,t-1,i+1,j));\n }\n }\n\n }\n return dp[i][t][p+1]=ans;\n\n }\n int minCost(vector<int>& h, vector<vector<int>>& c, int n, int m, int t) {\n memset(dp,-1,sizeof(dp));\n int ans=solve(h,c,n,m,t,0,-1);\n return (ans>=1e9)?-1:ans;\n }\n};",
"memory": "15573"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[101][101][101];\n int sol(int i,int j,int n,vector<int>& houses,vector<vector<int>>& cost, int target){\n if(i>=houses.size()){\n if(target==0){\n return 0;\n }\n return 1e9;\n }\n if(target<0){\n return 1e9;\n }\n if(dp[i][j][target]!=-1){\n return dp[i][j][target];\n }\n int ans=1e9;\n if(houses[i]==0){\n //backtrack\n for(int color=1;color<=n;color++){\n int c=0;\n houses[i]=color;\n if(color!=j){\n c=sol(i+1,color,n,houses,cost,target-1);\n }\n else{\n c=sol(i+1,color,n,houses,cost,target);\n }\n houses[i]=0;\n ans=min(ans,c + cost[i][color-1]);\n }\n }\n else{\n int c=0;\n if(houses[i]!=j){\n c=sol(i+1,houses[i],n,houses,cost,target-1);\n }\n else{\n c=sol(i+1,j,n,houses,cost,target);\n }\n ans=min(ans,c);\n }\n\n return dp[i][j][target]=ans;\n\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n //base case\n int c=0;\n int prev=-1;\n int fl=0;\n for(int i=0;i<m;i++){\n if(houses[i]==0){\n fl=1;\n }\n if(prev==-1){\n c++;\n prev=houses[i];\n }\n else if(prev==houses[i]){\n\n }\n else{\n c++;\n prev=houses[i];\n }\n }\n if(!fl && c!=target){\n return -1;\n }\n memset(dp,-1,sizeof(dp));\n return sol(0,0,n,houses,cost,target)==1e9?-1:sol(0,0,n,houses,cost,target); \n // return 0;\n }\n};",
"memory": "15759"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n \n int target;\n vector<vector<int>> cost;\n vector<int> houses;\n int n, m;\n \n long long dp[109][109][39];\n \n long long solve(int idx, int nums, int prev) {\n \n if(nums > target)\n return INT_MAX;\n \n if(idx == m){\n return nums == target ? 0 : INT_MAX;\n }\n \n if(dp[idx][nums][prev] != -1)\n return dp[idx][nums][prev];\n \n ll ans = INT_MAX;\n int new_num = nums;\n \n if(houses[idx] != 0) {\n new_num = nums;\n if(prev != houses[idx]) new_num = nums + 1;\n ans = min(ans, solve(idx + 1, new_num, houses[idx])); \n } else {\n for(int i = 1; i <= n; i++) {\n new_num = nums;\n if(prev != i) new_num = nums + 1;\n ans = min(ans, solve(idx + 1, new_num, i) + cost[idx][i-1]);\n }\n }\n \n return dp[idx][nums][prev] = ans;\n \n }\n \n \n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n this->houses = houses;\n this->cost = cost;\n this->target = target;\n this->n = n; this->m = m;\n memset(dp, -1, sizeof(dp));\n ll ans = solve(0, 0, 29);\n return ans == INT_MAX ? -1 : ans;\n }\n};",
"memory": "15759"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[101][101][101];\n int help(int ind,int target,int prev,vector<int>& houses, vector<vector<int>>& cost, int m, int n){\n if(ind==m){\n if(target==0) return 0;\n return 1e9;\n }\n if(target<0) return 1e9;\n if(dp[ind][target][prev]!=-1) return dp[ind][target][prev];\n //case-1 if house is not coloured then we have to color\n if(houses[ind]==0){\n int mini=1e9;\n for(int color=0;color<n;color++){\n int p=(color+1==prev)?target:target-1;\n int ans=cost[ind][color]+help(ind+1,p,color+1,houses,cost,m,n);\n mini=min(mini,ans);\n }\n return dp[ind][target][prev]=mini;\n }\n else{\n int p=(houses[ind]==prev)?target:target-1;\n return dp[ind][target][prev]=help(ind+1,p,houses[ind],houses,cost,m,n);\n }\n return -1;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n memset(dp,-1,sizeof(dp));\n int result = help(0, target, 0, houses, cost, m, n);\n return result >= 1e9 ? -1 : result;\n }\n};",
"memory": "15945"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int dp[103][103][103];\n\n int help(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target, int i, int prev) {\n\n if (i == m) {\n if (!target) return 0;\n else return 1e9;\n }\n\n if (target < 0) return 1e9;\n\n if (dp[i][prev][target] != -1) return dp[i][prev][target];\n\n\n int mn = 1e9;\n\n // case 1:\n // no need to colorize\n if (houses[i]) {\n if (houses[i] != prev) // nbrs increase, hence target decreases\n return dp[i][prev][target] = help(houses, cost, m, n, target - 1, i + 1, houses[i]);\n\n else return dp[i][prev][target] = help(houses, cost, m, n, target, i + 1, houses[i]);\n }\n\n else {\n\n // case 2: color them\n\n // I need to paint with colors [1,n] such that the current color isn't the same as the prev\n // if they are the same, nbrs remain the same (no change in target)\n // else, nbrs increase, so target-1\n\n // colors: 1 to n\n // cost[i][j]: cost to paint ith building with color j+1\n\n\n for (int c = 1; c <= n; c++) {\n int cur = cost[i][c - 1];\n\n if (prev == c) cur += help(houses, cost, m , n, target, i + 1, c);\n else {\n\n // explore the rest (next rows)\n cur += help(houses, cost, m , n, target - 1, i + 1, c);\n }\n mn = min(mn, cur);\n }\n\n return dp[i][prev][target] = mn;\n }\n\n\n }\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n\n memset(dp, -1, sizeof(dp));\n int mm = help(houses, cost, m, n, target, 0, 0);\n return (mm >= 1e9) ? -1 : mm;\n }\n};",
"memory": "16131"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int dp[103][103][103];\n\n int help(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target, int i, int prev) {\n\n if (i == m) {\n if (!target) return 0;\n else return 1e9;\n }\n\n if (target < 0) return 1e9;\n\n if (dp[i][prev][target] != -1) return dp[i][prev][target];\n\n\n int mn = 1e9;\n\n // case 1:\n // no need to colorize\n if (houses[i]) {\n if (houses[i] != prev) // nbrs increase, hence target decreases\n return dp[i][prev][target] = help(houses, cost, m, n, target - 1, i + 1, houses[i]);\n\n else return dp[i][prev][target] = help(houses, cost, m, n, target, i + 1, houses[i]);\n }\n\n else {\n\n // case 2: color them\n\n // I need to paint with colors [1,n] such that the current color isn't the same as the prev\n // if they are the same, nbrs remain the same (no change in target)\n // else, nbrs increase, so target-1\n\n // colors: 1 to n\n // cost[i][j]: cost to paint ith building with color j+1\n\n\n for (int c = 1; c <= n; c++) {\n int cur = cost[i][c - 1];\n\n if (prev == c) cur += help(houses, cost, m , n, target, i + 1, c);\n else {\n\n // explore the rest (next rows)\n cur += help(houses, cost, m , n, target - 1, i + 1, c);\n }\n mn = min(mn, cur);\n }\n\n return dp[i][prev][target] = mn;\n }\n\n\n }\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n\n memset(dp, -1, sizeof(dp));\n int mm = help(houses, cost, m, n, target, 0, 0);\n return (mm >= 1e9) ? -1 : mm;\n }\n};",
"memory": "16318"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n #define vvi vector<vector<int>>\n #define vi vector<int>\n\n #define dbg(x) cout << #x << \" : \" << x << endl\n\n int dp[105][105][105];\n int f(int pos , int k , int last , int &target , vi &a , vvi &cost){\n int m = cost.size();\n int n = cost[0].size();\n assert(m == a.size());\n\n if(k > target){\n return 1e9;\n }\n\n if(pos == m){\n // have to end here!\n if(k == target)return 0;\n else return 1e9;\n }\n // dbg(pos);\n // dbg(k);\n // dbg(last);\n\n if(dp[pos][k][last] != -1)return dp[pos][k][last];\n\n // can start a new sequence from here (pos) or continue\n int ans = 1e9;\n if(a[pos] == 0){\n for(int i=1;i<=n;i++){\n int paint = cost[pos][i-1];\n if(a[pos] == i){\n paint = 0;\n }\n if(i == last){\n // continue\n ans = min(ans , paint + f(pos+1 , k , last , target , a , cost));\n }\n else{\n // new partition\n ans = min(ans , paint + f(pos+1 , k+1 , i , target , a , cost));\n }\n }\n }\n else{\n int k_ = k;\n if(a[pos] != last)k_++;\n ans = f(pos + 1 , k_ , a[pos] , target , a , cost);\n }\n \n return dp[pos][k][last] = ans;\n }\n int minCost(vector<int>& a, vector<vector<int>>& cost, int m, int n, int target) {\n memset(dp , -1 , sizeof(dp));\n int fans = 1e9;\n\n // CANT REPAINT A ALREADY PAINTED HOUSE!!\n // THAT'S WHY I GOT WA\n\n if(a[0] == 0){\n for(int i=1;i<=n;i++){\n int paint = cost[0][i-1];\n if(a[0] == i){\n paint = 0;\n }\n fans = min(fans , paint + f(1 , 1 , i , target , a , cost));\n }\n }\n else{\n fans = f(1 , 1 , a[0] , target , a , cost);\n }\n if(fans == 1e9)return -1;\n return fans;\n }\n};",
"memory": "16504"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic: \nunordered_map<int,int> notcol;\n\nint t[105][105][105];\n int solve(int ind,vector<int>& houses,vector<vector<int>>& cost,int m,int n,int tar,int prev,int reg){\n \n if(ind==m){\n if(reg==tar){\n return 0;\n }\n return INT_MAX;\n }\n\n if(reg>tar){\n return INT_MAX;\n }\nif(t[ind][reg][prev+1]!=-1){\n return t[ind][reg][prev+1];\n}\n\n int mini=INT_MAX;\n if(houses[ind]!=0){\n int new_reg = reg+(houses[ind]-1!=prev?1:0);\n return t[ind][reg][prev+1]= solve(ind+1,houses,cost,m,n,tar,houses[ind]-1,new_reg);\n }\n \nelse{\n for(int i=0;i<n;i++){\n int new_reg = reg+ (i!=prev?1:0);\n int costy = solve(ind+1,houses,cost,m,n,tar,i,new_reg);\n if(costy!=INT_MAX){\n costy+= cost[ind][i];\n }\n mini = min(mini,costy);\n }\n \n}\nreturn t[ind][reg][prev+1]= mini;\n}\n\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n \n \n \nmemset(t,-1,sizeof(t));\n int res = solve(0,houses,cost,m,n,target,-1,0);\n return (res==INT_MAX)?-1:res;\n }\n}; ",
"memory": "16690"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n int calcNumNeighborhoods(const vector<int>& houses) {\n int count = 0;\n int last_house = 0;\n for (int house : houses) {\n if (house > 0 && house != last_house) {\n count++;\n last_house = house;\n }\n }\n return count;\n }\n\n\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n int num_neighborhoods = calcNumNeighborhoods(houses);\n if (num_neighborhoods > target) {\n return -1;\n }\n\n const int max_cost = 1e6 + 1;\n\n // Initialize with max cost : 100* 1e4 ;\n vector<vector<vector<int>>> dp =\n vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(target, max_cost)));\n\n // At this point only one neighborhood;\n if (houses[0] == 0) {\n for (int j = 0; j < n; j++) {\n // Simple cost to paint.\n dp[0][j][0] = cost[0][j];\n }\n } else {\n // Cost 0 , as house is already painted\n dp[0][houses[0] - 1][0] = 0;\n }\n\n for (int i = 1; i < m; i++) {\n if (houses[i] == 0) {\n for (int j = 0; j < n; j++) {\n // Same color\n for (int k = 0; k < target; k++) {\n dp[i][j][k] = dp[i - 1][j][k] + cost[i][j];\n }\n // Different color (k is increased)\n for (int k = 0; k < target - 1; k++) {\n int min_cost = max_cost;\n for (int j1 = 0; j1 < n; j1++) {\n if (j1 != j) {\n min_cost = min(min_cost, dp[i - 1][j1][k] + cost[i][j]);\n }\n }\n dp[i][j][k + 1] = min(min_cost, dp[i][j][k + 1]);\n }\n }\n } else {\n const int j = houses[i] - 1;\n for (int k = 0; k < target; k++) {\n dp[i][j][k] = dp[i - 1][j][k];\n }\n for (int j1 = 0; j1 < n; j1++) {\n // Different color (k is increased)\n for (int k = 0; k < target - 1; k++) {\n int min_cost = max_cost;\n if (j1 != j) {\n min_cost = min(min_cost, dp[i - 1][j1][k]);\n }\n dp[i][j][k + 1] = min(min_cost, dp[i][j][k + 1]);\n }\n }\n }\n }\n\n int result = max_cost;\n for (int j = 0; j < n; j++) {\n result = min(result, dp[m - 1][j][target - 1]);\n }\n\n if (result == max_cost) {\n return -1;\n }\n return result;\n }\n};\n",
"memory": "16876"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "#include <vector>\n#include <algorithm>\n#include <climits>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n const int INF = 1e9 + 7;\n // DP table: dp[i][j][k] represents the minimum cost to paint up to the i-th house,\n // with the i-th house painted with color j, and k neighborhoods.\n vector<vector<vector<int>>> dp(m, vector<vector<int>>(n, vector<int>(target + 1, INF)));\n \n // Initialize the first house\n if (houses[0] == 0) {\n // If the first house is not painted, we can paint it with any color\n for (int j = 0; j < n; ++j) {\n dp[0][j][1] = cost[0][j]; // It will form 1 neighborhood\n }\n } else {\n // If the first house is already painted, we can only use that color\n dp[0][houses[0] - 1][1] = 0; // It forms 1 neighborhood and costs nothing\n }\n\n // Fill the DP table\n for (int i = 1; i < m; ++i) {\n for (int j = 0; j < n; ++j) {\n if (houses[i] != 0 && houses[i] - 1 != j) continue; // Skip if the house is already painted with a different color\n for (int k = 1; k <= target; ++k) {\n // Case 1: Continue the same neighborhood\n if (houses[i] == 0) {\n dp[i][j][k] = dp[i-1][j][k] + cost[i][j]; // Paint with color j\n } else {\n dp[i][j][k] = dp[i-1][j][k]; // No painting cost if already painted\n }\n \n // Case 2: Form a new neighborhood\n for (int prev = 0; prev < n; ++prev) {\n if (prev == j) continue; // Must be a different color\n if (houses[i] == 0) {\n dp[i][j][k] = min(dp[i][j][k], dp[i-1][prev][k-1] + cost[i][j]); // Paint with color j\n } else {\n dp[i][j][k] = min(dp[i][j][k], dp[i-1][prev][k-1]); // Already painted, no cost\n }\n }\n }\n }\n }\n\n // Find the minimum cost with exactly target neighborhoods\n int result = INF;\n for (int j = 0; j < n; ++j) {\n result = min(result, dp[m-1][j][target]);\n }\n\n return result == INF ? -1 : result;\n }\n};\n",
"memory": "17063"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\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, vector<int>(target + 1, INT_MAX)));\n\n // Initialize dp for the first house\n for (int i = 0; i < n; i++) {\n if (houses[0] == 0) {\n dp[0][i][1] = cost[0][i];\n } else if (houses[0] == i + 1) {\n dp[0][i][1] = 0;\n }\n }\n\n // Fill dp for the rest of the houses\n for (int i = 1; i < m; i++) {\n for (int j = 0; j < n; j++) {\n if (houses[i] != 0 && houses[i] != j + 1) {\n continue;\n }\n\n for (int k = 0; k < n; k++) {\n for (int t = 1; t <= target; t++) {\n if (dp[i - 1][k][t] == INT_MAX) continue;\n\n int currCost = (houses[i] == 0) ? cost[i][j] : 0;\n if (j == k) {\n dp[i][j][t] = min(dp[i][j][t], dp[i - 1][k][t] + currCost);\n } else if (t + 1 <= target) {\n dp[i][j][t + 1] = min(dp[i][j][t + 1], dp[i - 1][k][t] + currCost);\n }\n }\n }\n }\n }\n\n int result = INT_MAX;\n for (int i = 0; i < n; i++) {\n result = min(result, dp[m - 1][i][target]);\n }\n\n return result == INT_MAX ? -1 : result;\n }\n};\n",
"memory": "17249"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n vector<vector<int>> dp(n, vector<int>(target, 2000000)); // color, group\n if(houses[0]==0) \n {\n for(int c=0; c<n; c++)\n {\n dp[c][0] = cost[0][c];\n }\n }else\n {\n dp[houses[0]-1][0] = 0;\n }\n for(int h=1; h<m; h++)\n {\n vector<vector<int>> curr(n, vector<int>(target, 2000000));\n if(houses[h]==0)\n {\n for(int c=0; c<n; c++)\n {\n for(int g=0; g<target; g++)\n {\n curr[c][g] = dp[c][g]+cost[h][c];\n for(int cc=0; cc<n; cc++)\n {\n if(cc==c) continue;\n if(g>0) curr[c][g] = min(curr[c][g], dp[cc][g-1]+cost[h][c]);\n }\n }\n }\n }else\n {\n for(int g=0; g<target; g++)\n {\n for(int cc=0; cc<n; cc++)\n {\n if(cc==houses[h]-1) continue;\n curr[cc][g] = 2000000;\n }\n }\n for(int g=0; g<target; g++)\n {\n curr[houses[h]-1][g] = dp[houses[h]-1][g];\n for(int cc=0; cc<n; cc++)\n {\n if(cc==houses[h]-1) continue;\n if(g>0) curr[houses[h]-1][g] = min(curr[houses[h]-1][g], dp[cc][g-1]);\n }\n }\n }\n dp =curr;\n }\n int res = 2000000;\n for(int c=0; c<n; c++)\n {\n res = min(res, dp[c][target-1]);\n }\n return res>=2000000? -1: res;\n \n }\n};",
"memory": "17249"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\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>>(n, vector<int>(target+1, -1)));\n int ans=INT_MAX;\n for(int i=0; i < n; i++)\n {\n ans = min(ans, dp_fun(dp, houses, cost, m-1, i, target));\n }\n\n if(ans >= 1e8) return -1;\n return ans;\n }\n\n int dp_fun(vector<vector<vector<int>>>&dp, vector<int>&house, vector<vector<int>>&cost, int h_ind, int col_ind, int rem)\n {\n if(h_ind < 0)\n {\n if(rem == 0) return 0;\n return 1e8;\n }\n if(rem <= 0) return 1e8;\n\n if(dp[h_ind][col_ind][rem] == -1)\n {\n int ans=INT_MAX, painting_cost = cost[h_ind][col_ind];\n if(house[h_ind] != 0)\n {\n if(house[h_ind] != col_ind+1)\n {\n return dp[h_ind][col_ind][rem] = 1e8;\n }\n else\n {\n painting_cost = 0;\n }\n }\n\n ans = dp_fun(dp, house, cost, h_ind-1, col_ind, rem) + painting_cost;\n if(cost[0].size() == 1) \n {\n ans = min(ans, dp_fun(dp, house, cost, h_ind-1, col_ind, rem-1) + painting_cost);\n }\n for(int i=0; i < cost[0].size(); i++)\n {\n if(i != col_ind)\n {\n ans = min(ans, dp_fun(dp, house, cost, h_ind-1, i, rem-1) + painting_cost);\n }\n }\n dp[h_ind][col_ind][rem]=ans;\n } \n return dp[h_ind][col_ind][rem];\n }\n};",
"memory": "17435"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n // Function: return min cost\n // State vars:\n // - h: houses\n // - b: neighbourhoods\n // - c: colour\n // Recurrence relationships:\n // - To keep the same number of neighbourhoods as previous iteration, paint the house the same colour as previous house\n // - If house has already been painted, then base recurrence on previous min. cost of the house with the same colour\n // - Number of neighbourhoods is controlled by the house painting so no need for a loop for that\n // - There will be many invalid states\n // Base case:\n // - 1 house, 1 neighbourhood, costs used if house is unpainted, 0 cost if it is painted\n\n // dp[# houses][colour][# neighbourhoods]\n const int badState = numeric_limits<int>::max();\n vector<vector<vector<int>>> dp(m + 1, vector<vector<int>>(n, vector<int>(target + 1, badState)));\n\n // Base case\n for(int c = 0; c < n; c++) {\n dp[0][c][0] = 0;\n }\n\n for(int b = 1; b <= target; b++) {\n for(int h = b; h <= m; h++) {\n // Find the minimum and 2nd minimum for the previous # neighbourhoods\n int mn = badState;\n int mn2 = badState;\n\n for(int c = 0; c < n; c++) {\n const int cs = dp[h - 1][c][b - 1];\n if(cs < mn) {\n mn2 = mn;\n mn = cs;\n } else if(cs < mn2) {\n mn2 = cs;\n }\n }\n if(n == 1)\n mn2 = mn;\n\n // Determine the min costs\n if(houses[h - 1] != 0) {\n // Already painted house.\n const int minp = ((dp[h - 1][houses[h - 1] - 1][b - 1] != mn) ? mn : mn2);\n dp[h][houses[h - 1] - 1][b] = min(dp[h - 1][houses[h - 1] - 1][b], minp);\n } else {\n for(int c = 0; c < n; c++) {\n int minp = ((dp[h - 1][c][b - 1] != mn) ? mn : mn2);\n int minc = min(dp[h - 1][c][b], minp);\n if(minc != badState)\n dp[h][c][b] = minc + cost[h - 1][c];\n }\n }\n }\n }\n\n // Find out the minimum based on all the computation\n int minCost = badState;\n for(int i = 0; i < n; i++) {\n minCost = min(minCost, dp[m][i][target]);\n }\n return (minCost != badState) ? minCost : -1;\n }\n};",
"memory": "17621"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int f(int i, int p, int c, vector<int>& h, vector<vector<int>>& cost, int m, int n, int t, vector<vector<vector<int>>>& dp) {\n if (i==m && c==t) return 0;\n if (i>=m || c>t) return 1e9;\n if (dp[i][p][c] != -1) return dp[i][p][c];\n\n int ans = 1e9;\n\n if (h[i]!=0) {\n if (h[i]!=p) {\n ans = f(i+1, h[i], c + 1, h, cost, m, n, t, dp);\n } else {\n ans = f(i + 1, h[i], c, h, cost, m, n, t, dp);\n }\n } else {\n for (int color = 0; color < n; color++) {\n int curCost;\n if (color + 1 == p) {\n curCost = f(i + 1, color + 1, c, h, cost, m, n, t, dp) + cost[i][color];\n } else {\n curCost = f(i + 1, color + 1, c + 1, h, cost, m, n, t, dp) + cost[i][color];\n }\n ans = min(ans, curCost);\n }\n }\n\n dp[i][p][c] = ans;\n return ans;\n }\n\n int minCost(vector<int>& h, vector<vector<int>>& cost, int m, int n, int t) {\n vector<vector<vector<int>>> dp(m, vector<vector<int>>(n + 1, vector<int>(t + 1, -1)));\n int res = f(0, 0, 0, h, cost, m, n, t, dp);\n\n if (res == 1e9) {\n return -1;\n } else {\n return res;\n }\n }\n};\n",
"memory": "17808"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n int mincost(int ind,int pre_col,int target,vector<vector<int>> &cost,vector<int> &houses,vector<vector<vector<int>>> &dp)\n {\n int m=cost.size(),n=cost[0].size();\n if(target<0) return 1e7;\n if(ind==m)\n {\n if(target==0) return 0;\n return 1e7;\n }\n int pre_ind=pre_col+1;\n if(dp[ind][pre_ind][target]!=-1) return dp[ind][pre_ind][target];\n\n dp[ind][pre_ind][target]=1e9;\n\n //if the house has been painted last summer\n if(houses[ind]!=0)\n {\n if(houses[ind]!=pre_ind) dp[ind][pre_ind][target]=mincost(ind+1,houses[ind]-1,target-1,cost,houses,dp);\n else dp[ind][pre_ind][target]=mincost(ind+1,pre_col,target,cost,houses,dp);\n }\n\n // if the house is not painted\n else\n {\n for(int i=0;i<n;i++)\n {\n\n if(i!=pre_col) //we are choosing a different color\n {\n dp[ind][pre_ind][target]=min(dp[ind][pre_ind][target],cost[ind][i]+mincost(ind+1,i,target-1,cost,houses,dp));\n }\n\n else //we are choosing the same color \n {\n dp[ind][pre_ind][target]=min(dp[ind][pre_ind][target],cost[ind][i]+mincost(ind+1,i,target,cost,houses,dp));\n }\n }\n }\n\n return dp[ind][pre_ind][target];\n }\npublic:\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n if(n==1&&target>1) return -1;\n vector<vector<vector<int>>> dp(m,vector<vector<int>>(n+1,vector<int>(target+1,-1)));\n int ans=mincost(0,-1,target,cost,houses,dp);\n if(ans>=1e7) return -1;\n return ans;\n }\n};",
"memory": "17994"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int rec(int idx, int prev, int cur, vector<int>&house, vector<vector<int>>&cost, int m, int n, int target, vector<vector<vector<int>>>&dp){\n if(idx == m && cur == target)\n {\n return 0;\n }\n if (idx >= m || cur > target)\n {\n return 1e9;\n }\n if (dp[idx][prev][cur] != -1)\n {\n return dp[idx][prev][cur];\n }\n int ans=1e9;\n if(house[idx]==0)\n {\n int curCost = 1e9;\n for (int i = 0;i<n;i++)\n {\n int curr_color = i+1;\n if (curr_color == prev)\n {\n curCost = rec(idx + 1,curr_color,cur,house,cost,m,n,target,dp) + cost[idx][i];\n }\n else\n {\n curCost = rec(idx + 1,curr_color,cur + 1,house,cost,m,n,target,dp) + cost[idx][i];\n }\n ans = min(ans,curCost);\n }\n }\n else\n {\n if (prev != house[idx])\n {\n ans = rec(idx + 1,house[idx],cur + 1,house,cost,m,n,target,dp);\n }\n else\n {\n ans = rec(idx + 1,house[idx],cur,house,cost,m,n,target,dp);\n }\n }\n return dp[idx][prev][cur] = 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>> (n + 1, vector<int> (target + 1, -1)));\n int ans = rec(0,0,0,houses,cost,m,n,target,dp);\n return ans == 1e9? -1 : ans;\n }\n};",
"memory": "18180"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "// Memoization\n// TC: O((n*m*target)*n) , SC: O(n*m*target)\n\nclass Solution {\npublic:\n \n int rec(int idx, int prev, int cur, vector<int>&house, vector<vector<int>>&cost, int m, int n, int target, vector<vector<vector<int>>>&dp){\n \n // We have painted all the houses and our neighbour count is equal to target value.\n if(idx==m && cur==target)return 0;\n \n // 1 - We have not painted all the houses and our neighbour count has exceeded the target value.\n // 2 - We have painted all the houses and our neighbour count is still less than target value.\n if(idx>=m || cur>target)return 1e9;\n \n \n if(dp[idx][prev][cur]!=-1)return dp[idx][prev][cur];\n \n \n int ans=1e9;\n \n //if house is not colored\n if(house[idx]==0){\n \n int curCost=1e9;\n for(int i=0;i<n;i++){\n \n //Here i+1, represent the current color , in which we are coloring the not colored houses.\n int curr_color = i+1;\n \n // Painting the house with the same colour as that of the previous one.\n if(curr_color==prev){\n curCost=rec(idx+1,curr_color,cur,house,cost,m,n,target,dp)+cost[idx][i];\n }\n \n // Painting the house with a different color and incrementing the neighbour count.\n else{\n curCost=rec(idx+1,curr_color,cur+1,house,cost,m,n,target,dp)+cost[idx][i];\n }\n \n ans=min(ans,curCost);\n }\n }\n \n //house[idx]!=0, i.e house is already painted\n else{\n \n // house had different colour from its previous one. So, incrementing the neighbour count.\n if(prev!=house[idx]){\n ans=rec(idx+1,house[idx],cur+1,house,cost,m,n,target,dp);\n }\n\n // house had the same colour as previous one.\n else{\n ans=rec(idx+1,house[idx],cur,house,cost,m,n,target,dp);\n }\n \n }\n \n return dp[idx][prev][cur]=ans;\n }\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>>(n+1,vector<int>(target+1,-1)));\n \n int ans= rec(0,0,0,houses,cost,m,n,target,dp);\n return ans==1e9?-1:ans;\n }\n};",
"memory": "18366"
} |
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> </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> </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 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n int INF = 1e9+7;\npublic:\n int recur_2(vector<int>& houses, vector<vector<int>>& cost, int target, int ind, int lastColor, int curTarget,vector<vector<vector<int>>>& dp) {\n if (curTarget > target) return INF;\n if (ind >= houses.size()) {\n if (curTarget == target) return 0;\n else return INF;\n }\n if (dp[ind][lastColor][curTarget] != -1) return dp[ind][lastColor][curTarget];\n if (houses[ind] != 0) {\n return dp[ind][lastColor][curTarget] = recur_2(houses, cost, target, ind+1, houses[ind], (lastColor == houses[ind]) ? curTarget : curTarget + 1, dp);\n }\n int ans = INF;\n for (int i = 0; i < cost[ind].size(); i++) {\n ans = min(ans, recur_2(houses, cost, target,ind+1,i+1, (lastColor == i+1) ? curTarget : curTarget + 1, dp) + cost[ind][i]);\n }\n return dp[ind][lastColor][curTarget] = ans;\n }\n int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {\n int ans = INF;\n vector<vector<vector<int>>>dp(m+1, vector<vector<int>>(n+1, vector<int>(target+1, -1)));\n ans = recur_2(houses, cost, target, 0, 0, 0, dp);\n return ans >= INF ? -1 : ans;\n }\n};\n\n// //1 0 0 2 3 -> 5\n// // 1 0 2 \n\n// 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// }\n// public:\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": "18553"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.