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 nar...
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(al...
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 nar...
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(al...
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 nar...
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 ...
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 nar...
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 ...
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 nar...
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 ...
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 nar...
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 ...
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 nar...
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_...
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 nar...
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...
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 nar...
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<vect...
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 nar...
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...
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 nar...
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]);\...
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 nar...
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 ...
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 nar...
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...
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 nar...
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...
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 nar...
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 v...
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 nar...
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,m...
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 nar...
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...
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 nar...
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...
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 nar...
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[curr...
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 nar...
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]);\...
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 nar...
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++;\...
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 nar...
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[connect...
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 nar...
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 ...
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 nar...
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(con...
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 nar...
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]...
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 nar...
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]...
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 nar...
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 ...
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 nar...
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> ...
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 nar...
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);...
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 nar...
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 ...
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 nar...
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...
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 nar...
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 a...
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 nar...
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(connec...
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 nar...
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 = con...
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 nar...
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[nod...
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 nar...
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].pus...
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 nar...
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 an...
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 nar...
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,v...
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 nar...
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]].ins...
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 nar...
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.po...
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 nar...
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 co...
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 nar...
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 ...
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 nar...
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_...
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 nar...
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[n...
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 nar...
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...
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 nar...
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...
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 nar...
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 neigh...
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 nar...
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...
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 nar...
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 ...
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 nar...
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...
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 nar...
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 Edg...
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 nar...
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 Edg...
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 nar...
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 Edg...
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</...
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};", ...
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</...
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.em...
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</...
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</...
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};"...
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</...
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</...
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</...
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</...
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 e...
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</...
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 ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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]=...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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&...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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==targ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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][c...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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] = d...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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].s...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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)...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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, t...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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, t...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 }\...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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;\...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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][...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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;\...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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;\...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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]....
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 m...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 pa...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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++) {...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 a...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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:...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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 = 1e...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
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;...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\npublic:\n \n int 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 ...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "// 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 neigh...
1,583
<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> <p>A neighborhood is a maximal group of continuous houses that ar...
2
{ "code": "class Solution {\n int 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 ...