id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n template<typename P>\n bool topoIter(int n, vector<vector<int>> out, P&& job) {\n vector<int> in(n);\n for(int i = 0; i < n; ++i) \n for(const auto& j : out[i])\n ++in[j];\n queue<int> q{};\n for(int i = 0; i < n; ++i) if(in[i] == 0) q.push(i);\n while(!q.empty()) {\n auto now = q.front(); q.pop();\n job(now);\n --n;\n for(const int& nxt : out[now]) \n if(--in[nxt] == 0)\n q.push(nxt);\n }\n return n == 0;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(auto& id : group) if(id == -1) id = m++;\n \n vector<int> sz_grp(m,0);\n vector<vector<int>> beforeGroups(m);\n for(int i = 0; i < n; ++i) {\n ++sz_grp[group[i]];\n beforeGroups[group[i]].reserve(beforeItems[i].size()/4);\n for(const int& j : beforeItems[i])\n if(group[i] != group[j]) \n beforeGroups[group[i]].push_back(group[j]);\n }\n \n vector<int> idx = std::move(sz_grp);\n if(!topoIter(m, std::move(beforeGroups),\n [&,acc=0](const int& g) mutable { acc += exchange(idx[g], acc); })) {\n return {};\n }\n\n vector<int> ans(n);\n if(!topoIter(n, std::move(beforeItems),\n [&](const int& i) { ans[idx[group[i]]++] = i; })) {\n return {};\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "37037" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n template<typename P>\n bool topoIter(int n, vector<vector<int>> out, P&& job) {\n // Calculate in order\n vector<int> in(n);\n for(int i = 0; i < n; ++i) \n for(const auto& j : out[i])\n ++in[j];\n // Topological sort\n queue<int> q{};\n for(int i = 0; i < n; ++i) if(in[i] == 0) q.push(i);\n while(!q.empty()) {\n auto now = q.front(); q.pop();\n job(now);\n --n;\n for(const int& nxt : out[now]) \n if(--in[nxt] == 0)\n q.push(nxt);\n }\n return n == 0;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n // Assign group ids to -1s\n for(auto& id : group) if(id == -1) id = m++;\n \n // Preprocess\n vector<int> sz_grp(m,0);\n vector<vector<int>> beforeGroups(m);\n for(int i = 0; i < n; ++i) {\n ++sz_grp[group[i]];\n beforeGroups[group[i]].reserve(beforeItems[i].size()/4);\n for(const int& j : beforeItems[i])\n if(group[i] != group[j]) // Skip self loops\n beforeGroups[group[i]].push_back(group[j]);\n }\n \n // Calculate where the groups starts in the answer\n vector<int> idx = std::move(sz_grp);\n if(!topoIter(m, std::move(beforeGroups),\n [&,acc=0](const int& g) mutable { acc += exchange(idx[g], acc); })) {\n return {};\n }\n\n // Fill the answer\n vector<int> ans(n);\n if(!topoIter(n, std::move(beforeItems),\n [&](const int& i) { ans[idx[group[i]]++] = i; })) {\n return {};\n }\n\n // Reverse it back\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};", "memory": "37037" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "#include <vector>\n\nusing namespace std;\n\nclass Solution {\n vector<vector<int>> beforeGroups;\n vector<vector<int>> groupItems;\n vector<int> result;\n vector<bool> groupVisited;\n vector<bool> groupVisitedCurrentStack;\n vector<bool> itemVisited;\n vector<bool> itemVisitedCurrentStack;\n bool flag = true;\n\n void dfsItem(const int item, const int groupNumber, const vector<int>& group, const vector<vector<int>>& beforeItems) {\n if(itemVisitedCurrentStack[item] == true) {\n flag = false;\n return;\n }\n if(flag == false or itemVisited[item]) return;\n itemVisitedCurrentStack[item] = true;\n itemVisited[item] = true;\n for(const int& beforeItem: beforeItems[item]) if(group[beforeItem] == groupNumber) dfsItem(beforeItem, groupNumber, group, beforeItems);\n result.push_back(item);\n itemVisitedCurrentStack[item] = false;\n }\n\n void dfsGroup(const int groupNumber, const vector<int>& group, const vector<vector<int>>& beforeItems) {\n if(groupVisitedCurrentStack[groupNumber]) {\n flag = false;\n return;\n }\n if(flag == false or groupVisited[groupNumber]) return;\n groupVisitedCurrentStack[groupNumber] = true;\n groupVisited[groupNumber] = true;\n\n for(const int& beforeGroup: beforeGroups[groupNumber]) dfsGroup(beforeGroup, group, beforeItems);\n groupVisitedCurrentStack[groupNumber] = false;\n for(const int& item: groupItems[groupNumber]) dfsItem(item, groupNumber, group, beforeItems);\n }\n\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int groupNumber = m;\n for(int i=0; i<n; i++) if(group[i] == -1) group[i] = groupNumber++;\n\n beforeGroups.assign(groupNumber, vector<int>());\n groupItems.assign(groupNumber, vector<int>());\n groupVisited.assign(groupNumber, false);\n groupVisitedCurrentStack.assign(groupNumber, false);\n itemVisited.assign(n, false);\n itemVisitedCurrentStack.assign(n, false);\n\n for(int i=0; i<n; i++) {\n groupItems[group[i]].push_back(i);\n for(const int& beforeItem: beforeItems[i]) if(group[i] != group[beforeItem]) beforeGroups[group[i]].push_back(group[beforeItem]);\n }\n \n for(int i=0; i<groupNumber; i++) {\n if(groupVisited[i] == false) dfsGroup(i, group, beforeItems);\n if(flag == false) return {};\n }\n\n return result;\n }\n};", "memory": "38312" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\n\npublic:\n vector<int> topologicalSort(vector<int>& inDegrees, vector<vector<int>>& graph) {\n stack<int> st;\n for (int i = 0; i < inDegrees.size(); i++) {\n if (inDegrees[i] == 0) {\n st.push(i);\n }\n }\n vector<int> order;\n while (!st.empty()) {\n int top = st.top();\n st.pop();\n order.push_back(top);\n for (int i = 0; i < graph[top].size(); i++) {\n int node = graph[top][i];\n inDegrees[node]--;\n if (inDegrees[node] == 0) {\n st.push(node);\n }\n }\n }\n return order;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n group[i] = m;\n m++;\n }\n }\n // if there's a cycle or any path that puts groups out of order then it is impossible\n\n // 2. consolidate edges per group\n // 3. topological sort groups\n vector<vector<int>> groupGraph(m);\n vector<int> groupInDegree(m);\n vector<vector<int>> itemGraph(n); // before -> after\n vector<int> itemInDegree(n); // can add after if indegree == 0\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int j = 0; j < beforeItems[i].size(); j++) {\n int after = i;\n int before = beforeItems[i][j];\n if (group[after] != group[before]) {\n groupGraph[group[before]].push_back(group[after]);\n groupInDegree[group[after]]++;\n } else {\n itemGraph[before].push_back(after);\n itemInDegree[after]++;\n }\n }\n }\n\n // we have already topological sorted all items and all groups\n // if we know that we can order the items within each group amongst themselves\n // and we know we can order the groups amongst themselves\n // then we know we can return an answer\n vector<int> groupSort = topologicalSort(groupInDegree, groupGraph);\n if (groupSort.size() != m) {\n return vector<int>();\n }\n vector<int> itemSort = topologicalSort(itemInDegree, itemGraph);\n if (itemSort.size() != n) {\n return vector<int>();\n }\n\n vector<int> groupSize(m);\n for (int i = 0; i < group.size(); i++) {\n groupSize[group[i]]++;\n }\n vector<int> startIndex(m);\n int soFar = 0;\n for (int i = 0; i < groupSort.size(); i++) {\n startIndex[groupSort[i]] = soFar;\n soFar += groupSize[groupSort[i]];\n }\n \n vector<int> solution(n);\n for (int i = 0; i < itemSort.size(); i++) {\n int grp = group[itemSort[i]];\n int idx = startIndex[grp];\n startIndex[grp]++;\n solution[idx] = itemSort[i];\n }\n\n \n return solution;\n }\n};", "memory": "39587" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n //create a graph for each group\n int indegree[n+m];\n memset(indegree,0,sizeof(indegree));\n vector<vector<int>>graph(n+m);\n for(int i = 0;i<n;i++){\n if(group[i]!=-1){\n graph[n+group[i]].push_back(i);\n indegree[i]++;\n }\n }\n for(int i = 0;i<n;i++){\n int gi = group[i]==-1?i:group[i]+n;\n for(int j:beforeItems[i]){\n int gj = group[j]==-1?j:group[j]+n;\n if(gi==gj){\n graph[j].push_back(i);\n indegree[i]++;\n }else{\n graph[gj].push_back(gi);\n indegree[gi]++;\n }\n }\n }\n vector<int>ans;\n for(int i =0;i<n+m;i++){\n if(indegree[i]==0)dfs(ans,graph,indegree,n,i);\n }\n return (ans.size()==n)?ans:vector<int>{};\n }\n\n void dfs(vector<int>&ans, vector<vector<int>>&graph, int*indegree, int n, int curr){\n if(curr<n)ans.push_back(curr);\n indegree[curr] = -1;\n for(int nex:graph[curr]){\n if(--indegree[nex]==0){\n dfs(ans,graph,indegree,n,nex);\n }\n }\n }\n};", "memory": "40862" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<vector<int>> graph(n+m,vector<int>{});\n vector<int> indegree(n+m,0);\n vector<int> ans;\n for(int i=0; i<group.size();i++)\n {\n if(group[i]==-1)\n {\n continue;\n }\n int group_node = n+group[i];\n graph[group_node].push_back(i); //each group node points to the nodes in its group\n indegree[i]++; //dont forget to increase indegree\n }\n for(int i=0;i<beforeItems.size();i++)\n {\n for (auto before_i:beforeItems[i])\n {\n int i_group_node;\n int before_i_group_node;\n if(group[i]==-1)\n {\n i_group_node = i;\n }\n else \n {\n i_group_node= group[i] + n; //not group[i]\n }\n if(group[before_i]==-1)\n {\n before_i_group_node=before_i;\n }\n else \n {\n before_i_group_node= group[before_i] + n;\n }\n if(i_group_node==before_i_group_node) //they are in same group\n {\n graph[before_i].push_back(i); //NOT i_group_node\n indegree[i]++;\n }\n else\n {\n graph[before_i_group_node].push_back(i_group_node);\n indegree[i_group_node]++;\n }\n \n }\n\n }\n for(int i=0;i<indegree.size();i++)\n {\n if(indegree[i]==0)\n dfs(ans, graph, indegree, n, i); //start topological sort from here\n }\n\n if(ans.size()==n) return ans;\n return vector<int>{};\n \n }\n void dfs(vector<int>& ans, vector<vector<int>>& graph, vector<int>& indegree, int n, int cur) {\n if(cur < n) ans.push_back(cur);\n indegree[cur] = -1; // mark it visited\n for(auto next : graph[cur]) {\n indegree[next]--;\n if(indegree[next] == 0)\n dfs(ans, graph, indegree, n, next);\n }\n }\n};", "memory": "42137" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<vector<int>> graph(n+m,vector<int>{});\n vector<int> indegree(n+m,0);\n vector<int> ans;\n for(int i=0; i<group.size();i++)\n {\n if(group[i]==-1)\n {\n continue;\n }\n int group_node = n+group[i];\n graph[group_node].push_back(i); //each group node points to the nodes in its group\n indegree[i]++; //dont forget to increase indegree\n }\n for(int i=0;i<beforeItems.size();i++)\n {\n for (auto before_i:beforeItems[i])\n {\n int i_group_node;\n int before_i_group_node;\n if(group[i]==-1)\n {\n i_group_node = i;\n }\n else \n {\n i_group_node= group[i] + n; //not group[i]\n }\n if(group[before_i]==-1)\n {\n before_i_group_node=before_i;\n }\n else \n {\n before_i_group_node= group[before_i] + n;\n }\n if(i_group_node==before_i_group_node) //they are in same group\n {\n graph[before_i].push_back(i); //NOT i_group_node\n indegree[i]++;\n }\n else\n {\n graph[before_i_group_node].push_back(i_group_node);\n indegree[i_group_node]++;\n }\n \n }\n\n }\n for(int i=0;i<indegree.size();i++)\n {\n if(indegree[i]==0)\n dfs(ans, graph, indegree, n, i); //start topological sort from here\n }\n\n if(ans.size()==n) return ans;\n return vector<int>{};\n \n }\n void dfs(vector<int>&ans, vector<vector<int>>&graph, vector<int>&indegree, int n, int cur)\n {\n if(cur<n) ans.push_back(cur); //dont push back group nodes!\n indegree[cur]=-1; //dont visit this one again\n //we have 'visited' cur\n //now visit curs neighbours \n for(auto node:graph[cur])\n {\n indegree[node]--;\n if(indegree[node]==0)\n {\n dfs(ans,graph,indegree,n,node);\n }\n }\n }\n};", "memory": "42137" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> grpIndegree(m);\n vector<vector<int>> graph(n);\n vector<vector<int>> groupGraph(m);\n queue<int> grpQueue;\n queue<int> itmQueue;\n queue<int> ungrpQueue;\n for(int itm = 0, grp; itm < n;itm++ ){\n grp = group[itm];\n if(grp != -1) groupGraph[grp].push_back(itm);\n else if(beforeItems[itm].size() == 0) ungrpQueue.push(itm);\n\n for(int prnt : beforeItems[itm]){\n if(grp != - 1 && group[prnt] != grp) grpIndegree[grp]++;\n graph[prnt].push_back(itm);\n }\n }\n for(int i = 0; i < m; i++){\n if(grpIndegree[i] == 0) grpQueue.push(i);\n }\n\n vector<int> ans;\n \n while(true){\n while(!ungrpQueue.empty()){\n int itmTop = ungrpQueue.front();\n ans.push_back(itmTop);\n ungrpQueue.pop();\n for(int child : graph[itmTop]){\n auto& chldBef = beforeItems[child];\n chldBef.pop_back();\n int chldDeg = chldBef.size();\n int chldGrp = group[child];\n if(chldGrp == -1){\n if(chldDeg == 0) ungrpQueue.push(child);\n }\n else{\n if(--grpIndegree[chldGrp] == 0) grpQueue.push(chldGrp);\n }\n }\n }\n\n while(!grpQueue.empty()){\n int grpTop = grpQueue.front();\n grpQueue.pop();\n for(int x : groupGraph[grpTop]){\n if (beforeItems[x].size() == 0) itmQueue.push(x);\n }\n while(!itmQueue.empty()){\n int itmTop = itmQueue.front();\n int itmGrp = group[itmTop];\n ans.push_back(itmTop);\n itmQueue.pop();\n for(int child : graph[itmTop]){\n auto& chldBef = beforeItems[child];\n chldBef.pop_back();\n int chldDeg = chldBef.size();\n int chldGrp = group[child];\n if(chldGrp == itmGrp){\n if(chldDeg == 0) itmQueue.push(child);\n }\n else if(chldGrp == -1){\n if(chldDeg == 0) ungrpQueue.push(child);\n }\n else if(--grpIndegree[chldGrp] == 0) grpQueue.push(chldGrp);\n\n }\n }\n }\n if(ungrpQueue.empty() && grpQueue.empty()) break;\n }\n\n if(ans.size() != n) return vector<int>();\n return ans;\n }\n};", "memory": "43412" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> grpIndegree(m);\n vector<vector<int>> graph(n);\n vector<vector<int>> groupGraph(m);\n for(int itm = 0, grp; itm < n;itm++ ){\n grp = group[itm];\n if(grp != -1) groupGraph[grp].push_back(itm);\n for(int prnt : beforeItems[itm]){\n if(grp != - 1 && group[prnt] != grp) grpIndegree[grp]++;\n graph[prnt].push_back(itm);\n }\n }\n // for(int i = 0; i < n; i++){\n // cout<<i<<\" : \";\n // for(int j : graph[i]) cout<<j<<\" \";\n // cout<<endl;\n // }\n // cout<<\"Group Graph : \"<<endl;\n // for(int i = 0; i < m; i++){\n // cout<<i<<\" : \";\n // for(int j : groupGraph[i]) cout<<j<<\" \";\n // cout<<endl << \"Indegree : \"<<grpIndegree[i]<<endl;\n // }\n queue<int> grpQueue;\n queue<int> itmQueue;\n queue<int> ungrpQueue;\n for(int i = 0; i < n; i++){\n if(i<m && grpIndegree[i] == 0) grpQueue.push(i);\n if(group[i] == -1 && beforeItems[i].size() == 0) ungrpQueue.push(i);\n } \n\n vector<int> ans;\n \n while(!ungrpQueue.empty() || !grpQueue.empty()){\n // cout<<\"Ungrouped : \";\n while(!ungrpQueue.empty()){\n int itmTop = ungrpQueue.front();\n // cout<<itmTop<<\" \";\n ans.push_back(itmTop);\n ungrpQueue.pop();\n for(int child : graph[itmTop]){\n auto& chldBef = beforeItems[child];\n chldBef.pop_back();\n int chldDeg = chldBef.size();\n int chldGrp = group[child];\n if(chldGrp == -1){\n if(chldDeg == 0) ungrpQueue.push(child);\n }\n else{\n if(--grpIndegree[chldGrp] == 0) grpQueue.push(chldGrp);\n }\n }\n }\n if(grpQueue.empty()) break;\n\n\n int grpTop = grpQueue.front();\n grpQueue.pop();\n for(int x : groupGraph[grpTop]){\n if (beforeItems[x].size() == 0) itmQueue.push(x);\n }\n // cout<<\"Grouped : \";\n while(!itmQueue.empty()){\n int itmTop = itmQueue.front();\n // cout<<itmTop<<\" \";\n int itmGrp = group[itmTop];\n ans.push_back(itmTop);\n itmQueue.pop();\n for(int child : graph[itmTop]){\n auto& chldBef = beforeItems[child];\n chldBef.pop_back();\n int chldDeg = chldBef.size();\n int chldGrp = group[child];\n if(chldGrp == itmGrp){\n if(chldDeg == 0) itmQueue.push(child);\n }\n else if(chldGrp == -1){\n if(chldDeg == 0) ungrpQueue.push(child);\n }\n else{\n if(--grpIndegree[chldGrp] == 0) grpQueue.push(chldGrp);\n }\n }\n }\n // cout<<endl;\n \n\n }\n // for(int x : ans) cout<<x<<\" \";\n // cout<<endl;\n if(ans.size() != n) return vector<int>();\n return ans;\n }\n};", "memory": "43412" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\n\npublic:\n vector<int> topologicalSort(vector<int>& inDegrees, vector<vector<int>>& graph) {\n stack<int> st;\n for (int i = 0; i < inDegrees.size(); i++) {\n if (inDegrees[i] == 0) {\n st.push(i);\n }\n }\n vector<int> order;\n while (!st.empty()) {\n int top = st.top();\n st.pop();\n order.push_back(top);\n for (int i = 0; i < graph[top].size(); i++) {\n int node = graph[top][i];\n inDegrees[node]--;\n if (inDegrees[node] == 0) {\n st.push(node);\n }\n }\n }\n return order;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n group[i] = m;\n m++;\n }\n }\n // if there's a cycle or any path that puts groups out of order then it is impossible\n\n // 2. consolidate edges per group\n // 3. topological sort groups\n vector<vector<int>> groupGraph(m);\n vector<int> groupInDegree(m);\n vector<vector<int>> itemGraph(n); // before -> after\n vector<int> itemInDegree(n); // can add after if indegree == 0\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int j = 0; j < beforeItems[i].size(); j++) {\n int after = i;\n int before = beforeItems[i][j];\n itemGraph[before].push_back(after);\n itemInDegree[after]++;\n if (group[after] != group[before]) {\n groupGraph[group[before]].push_back(group[after]);\n groupInDegree[group[after]]++;\n }\n }\n }\n\n // note that if we can sort by items and we can sort by groups then that covers all edges so the answer is possible\n vector<int> groupSort = topologicalSort(groupInDegree, groupGraph);\n if (groupSort.size() != m) {\n return vector<int>();\n }\n vector<int> itemSort = topologicalSort(itemInDegree, itemGraph);\n if (itemSort.size() != n) {\n return vector<int>();\n }\n\n vector<int> groupSize(m);\n for (int i = 0; i < group.size(); i++) {\n groupSize[group[i]]++;\n }\n vector<int> startIndex(m);\n int soFar = 0;\n for (int i = 0; i < groupSort.size(); i++) {\n startIndex[groupSort[i]] = soFar;\n soFar += groupSize[groupSort[i]];\n }\n \n vector<int> solution(n);\n for (int i = 0; i < itemSort.size(); i++) {\n int grp = group[itemSort[i]];\n int idx = startIndex[grp];\n startIndex[grp]++;\n solution[idx] = itemSort[i];\n }\n\n \n return solution;\n }\n};", "memory": "44687" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector <int> ans;\n vector <int> adj[(m << 1) + n];\n vector <int> indegree(n + (m << 1));\n for(int i = 0; i < group.size(); ++i) {\n if(group[i] != -1) {\n adj[n + group[i]].push_back(i);\n adj[i].push_back(n + m + group[i]);\n indegree[i]++;\n indegree[n + m + group[i]]++;\n }\n for(int j: beforeItems[i]) {\n if(group[i] == -1 && group[i] == group[j]) {\n adj[j].push_back(i);\n indegree[i]++;\n } else {\n int ig = group[i] == -1 ? i : n + group[i];\n int jg = group[j] == -1 ? j : n + m + group[j];\n // cout << i << \" \" << j << endl;\n // cout << ig << \" \" << jg << endl;\n // cout << group[i] << \" \" << group[j] << endl;\n if(group[i] == group[j]) {\n adj[j].push_back(i);\n indegree[i]++;\n } else { \n adj[jg].push_back(ig);\n indegree[ig]++;\n }\n }\n // cout << endl;\n }\n }\n // for(int i = 0; i < (n + (m << 1)); ++i) {\n // cout << i << \" -- \";\n // for(int child: adj[i]) {\n // cout << child << \" \";\n // }\n // cout << endl;\n // }\n for(int i = 0; i < n + (m << 1); ++i) {\n if(indegree[i] == 0)\n dfs(ans, adj, indegree, i, n);\n }\n if(ans.size() != n)\n return vector <int> {};\n return ans;\n }\n\n void dfs(vector <int>& ans, vector <int>* adj, vector <int>& indegree, int curr, int n) {\n if(curr < n) ans.push_back(curr);\n indegree[curr] = -1;\n for(int child: adj[curr]) {\n if(--indegree[child] == 0) {\n dfs(ans, adj, indegree, child, n);\n }\n }\n }\n\n};", "memory": "44687" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool topo(vector<int>adj[],int n,vector<int>&res)\n {\n queue<int>qu;\n vector<int>ind(n,0);\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<adj[i].size();j++) ind[adj[i][j]]++;\n }\n for(int i=0;i<n;i++) if(!ind[i]) qu.push(i);\n while(!qu.empty())\n {\n int x=qu.front();qu.pop();\n res.push_back(x);\n for(int i=0;i<adj[x].size();i++) \n {\n ind[adj[x][i]]--;\n if(ind[adj[x][i]]==0) qu.push(adj[x][i]);\n }\n }\n if(res.size()!=n) return false;\n return true;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int>adj[n];\n for(int i=0;i<group.size();i++) if(group[i]==-1) group[i]=m++;\n vector<int>grps[m];\n vector<int>adjg[m];\n for(int i=0;i<beforeItems.size();i++)\n {\n for(int j=0;j<beforeItems[i].size();j++)\n {\n adj[beforeItems[i][j]].push_back(i);\n if(group[i]==-1||group[beforeItems[i][j]]==-1) continue;\n auto it=find(adjg[group[beforeItems[i][j]]].begin(),adjg[group[beforeItems[i][j]]].end(),group[i]);\n if(it==adjg[group[beforeItems[i][j]]].end()&&group[beforeItems[i][j]]!=group[i])\n {\n int gp=group[beforeItems[i][j]];\n int gc=group[i];\n adjg[gp].push_back(gc);\n }\n }\n }\n vector<int>topg,topi;\n bool res1=topo(adj,n,topi);\n bool res2=topo(adjg,m,topg);\n if(!res1||!res2) return {};\n vector<int>ans;\n for(int i=0;i<topi.size();i++){\n int g=group[topi[i]];\n grps[g].push_back(topi[i]);\n }\n for(int i=0;i<topg.size();i++)\n {\n for(int j=0;j<grps[topg[i]].size();j++)\n ans.push_back(grps[topg[i]][j]);\n }\n return ans;\n }\n};", "memory": "45962" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool topo(vector<int>adj[],int n,vector<int>&res)\n {\n queue<int>qu;\n cout<<n<<endl;\n vector<int>ind(n,0);\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<adj[i].size();j++) ind[adj[i][j]]++;\n }\n for(int i=0;i<n;i++) if(!ind[i]) qu.push(i);\n while(!qu.empty())\n {\n int x=qu.front();qu.pop();\n res.push_back(x);\n for(int i=0;i<adj[x].size();i++) \n {\n ind[adj[x][i]]--;\n if(ind[adj[x][i]]==0) qu.push(adj[x][i]);\n }\n }\n if(res.size()!=n) return false;\n return true;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int>adj[n];\n for(int i=0;i<group.size();i++) if(group[i]==-1) group[i]=m++;\n vector<int>grps[m];\n vector<int>adjg[m];\n for(int i=0;i<beforeItems.size();i++)\n {\n for(int j=0;j<beforeItems[i].size();j++)\n {\n adj[beforeItems[i][j]].push_back(i);\n if(group[i]==-1||group[beforeItems[i][j]]==-1) continue;\n auto it=find(adjg[group[beforeItems[i][j]]].begin(),adjg[group[beforeItems[i][j]]].end(),group[i]);\n if(it==adjg[group[beforeItems[i][j]]].end()&&group[beforeItems[i][j]]!=group[i])\n {\n int gp=group[beforeItems[i][j]]==-1?m:group[beforeItems[i][j]];\n int gc=group[i]==-1?m:group[i];\n adjg[gp].push_back(gc);\n }\n }\n }\n vector<int>topg,topi;\n bool res1=topo(adj,n,topi);\n bool res2=topo(adjg,m,topg);\n if(!res1||!res2) return {};\n for(int i=0;i<topi.size();i++) cout<<topi[i]<<\" \";\n cout<<endl;\n for(int i=0;i<topg.size();i++) cout<<topg[i]<<\" \";\n cout<<endl;\n vector<int>ans;\n for(int i=0;i<topi.size();i++){\n int g=group[topi[i]]==-1?m:group[topi[i]];\n grps[g].push_back(topi[i]);\n }\n for(int i=0;i<topg.size();i++)\n {\n for(int j=0;j<grps[topg[i]].size();j++)\n ans.push_back(grps[topg[i]][j]);\n }\n return ans;\n }\n};", "memory": "45962" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& groups, vector<vector<int>>& beforeItems) {\n using i32 = int;\n vector <vector <i32>> A (n + m + m);\n vector <i32> deg (n + m + m);\n for (i32 i = 0; i < groups.size(); ++i) if (groups[i] != -1) {\n i32 L = groups[i] + n;\n i32 R = groups[i] + n + m;\n A[L].push_back(i);\n A[i].push_back(R);\n deg[i]++;\n deg[R]++;\n }\n for (i32 i = 0; i < n; ++i) {\n for (i32 x : beforeItems[i]) {\n if (groups[i] != -1 && groups[i] == groups[x]) {\n A[x].push_back(i);\n deg[i]++;\n }\n else {\n i32 R = groups[x] == -1 ? x : groups[x] + n + m;\n i32 L = groups[i] == -1 ? i : groups[i] + n;\n A[R].push_back(L);\n deg[L]++;\n }\n }\n }\n vector <i32> Ans;\n auto dfs = [&] (auto &&dfs, i32 u) -> void {\n if (u < n) Ans.push_back(u);\n for (i32 v : A[u]) if (deg[v] != -1) {\n deg[v]--;\n if (deg[v] == 0) {\n deg[v] = -1;\n dfs(dfs, v);\n }\n }\n };\n for (i32 i = 0; i < n + m + m; ++i) {\n if (deg[i] == 0 && deg[i] != -1) {\n dfs(dfs, i);\n }\n }\n return Ans.size() == n ? Ans : vector <i32> {};\n }\n};", "memory": "47237" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> toposort(vector<int> adj[], int k) {\n vector<int> indegree(k + 1);\n for (int i = 0; i < k; i++) {\n for (auto it : adj[i])\n indegree[it]++;\n }\n queue<int> q;\n vector<int> topo;\n for (int i = 0; i < k; i++) {\n if (indegree[i] == 0)\n q.push(i);\n }\n while (!q.empty()) {\n int top = q.front();\n topo.push_back(top);\n q.pop();\n for (auto it : adj[top]) {\n indegree[it]--;\n if (indegree[it] == 0)\n q.push(it);\n }\n }\n return (topo.size() == k) ? topo : vector<int>();\n }\n vector<int> sortItems(int n, int m, vector<int>& group,\n vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++) {\n if (group[i] == -1)\n group[i] = m++;\n }\n vector<int> item_graph[n];\n vector<int> group_graph[m];\n for (int i = 0; i < n; i++) {\n if (beforeItems[i].size() != 0) {\n for (auto it : beforeItems[i]) {\n item_graph[it].push_back(i);\n if (group[i] != group[it]) {\n int prevItemGroup = group[it];\n int currItemGroup = group[i];\n group_graph[prevItemGroup].push_back(currItemGroup);\n }\n }\n }\n }\n vector<int> item_order = toposort(item_graph, n);\n vector<int> group_order = toposort(group_graph, m);\n\n vector<int> groupToitemOrder[m];\n for (auto it : item_order) {\n groupToitemOrder[group[it]].push_back(it);\n }\n vector<int> result;\n\n for (auto group : group_order) {\n for(auto it :groupToitemOrder[group]){\n result.push_back(it);\n }\n }\n return result;\n }\n};", "memory": "47237" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> t, res(n), state(n + 2 * m);\n vector<vector<int>> g(n + 2 * m);\n for (int i = 0; i < n; ++i) {\n if (group[i] != -1) {\n g[n + group[i]].push_back(i);\n g[i].push_back(n + m + group[i]);\n }\n for (int j : beforeItems[i]) {\n if (group[i] != -1 && group[i] == group[j]) {\n g[j].push_back(i);\n } else {\n int p = group[i] == -1 ? i : n + group[i];\n int q = group[j] == -1 ? j : n + m + group[j];\n g[q].push_back(p);\n }\n }\n }\n for (int i = (int)g.size() - 1; i >= 0; --i) {\n if (!helper(g, i, state, t)) return {};\n }\n reverse(t.begin(), t.end());\n copy_if(t.begin(), t.end(), res.begin(), [&](int i) {return i < n;});\n return res;\n }\n bool helper(vector<vector<int>>& g, int i, vector<int>& state, vector<int>& res) {\n if (state[i] != 0) return state[i] == 2;\n state[i] = 1;\n for (int next : g[i]) {\n if (!helper(g, next, state, res)) return false;\n }\n state[i] = 2;\n res.push_back(i);\n return true;\n }\n};\n", "memory": "48512" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool CheckDAG(int u, vector<int> &color, const vector<vector<int>> &adj) { \n color[u] = 1; // Đang duyệt, đánh dấu màu là 1\n for(int v : adj[u]) {\n if(color[v] == 1) return true; // is DAG => res \n else if(color[v] == 0) {\n if(CheckDAG(v, color, adj)) {\n return true;\n }\n }\n }\n color[u] = 2; // Đã duyệt xong, đánh dấu màu là 2\n return false;\n }\n void dfs(int u, stack<int> &store, const vector<vector<int>> &adj, vector<bool> &visited) {\n visited[u] = true;\n for(int v : adj[u]) {\n if(!visited[v]) {\n dfs(v, store, adj, visited);\n }\n }\n store.push(u);\n }\n vector<int> Get_Topo(const vector<vector<int>> &adj) {\n vector<int> Topo;\n vector<int> color(adj.size(), 0);\n for(int u = 0; u < adj.size(); u++) {\n if(color[u] == 0) {\n if(CheckDAG(u, color, adj)) { // is DAG => COOK\n return {};\n }\n }\n }\n stack<int> store;\n vector<bool> visited(adj.size(), false);\n for(int u = 0; u < adj.size(); u++) {\n if(!visited[u]) {\n dfs(u, store, adj, visited);\n }\n }\n while(!store.empty()) {\n Topo.push_back(store.top());\n store.pop();\n }\n return Topo;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i = 0; i < group.size(); i++) {\n if(group[i] == -1) {\n group[i] = m++;\n }\n }\n vector<vector<int>> adj(n);\n vector<vector<int>> adj_group(m);\n for(int u = 0; u < n; u++) {\n for(int v : beforeItems[u]) {\n adj[v].push_back(u); \n //thằng v đứng trước thằng u\n if(group[v] != group[u]) {\n adj_group[group[v]].push_back(group[u]); \n // => v truoc u => nhóm thằng v đứng trước thằng u\n }\n }\n }\n vector<int> Topo_of_adj = Get_Topo(adj);\n if(Topo_of_adj.empty() == true) return {}; \n vector<int> Topo_of_adj_group = Get_Topo(adj_group);\n if(Topo_of_adj_group.empty() == true) return {}; \n vector<vector<int>> grouped_item(m);\n for(int u : Topo_of_adj) {\n grouped_item[group[u]].push_back(u);\n }\n vector<int> res;\n for(int g : Topo_of_adj_group) {\n for(int u : grouped_item[g]) {\n res.push_back(u);\n }\n }\n return res;\n }\n};\n", "memory": "48512" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> topoSort(int sz, vector<vector<int>> &adj){\n vector<int> in(sz);\n vector<int> ans;\n for(int i = 0 ; i< sz; i++){\n for(auto node : adj[i]){\n in[node]++;\n }\n }\n\n queue<int> q;\n for(int i = 0; i < sz; i ++){ \n if(in[i] == 0) q.push(i);\n }\n\n while(!q.empty()){\n int curr = q.front();\n ans.push_back(curr);\n q.pop();\n for(auto child : adj[curr]){\n in[child]--;\n if(in[child]==0){\n q.push(child);\n }\n }\n }\n return (ans.size()==sz) ? ans : vector<int>{} ;\n }\n \n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i = 0 ; i< n ; i++){\n if(group[i]==-1){\n group[i] = m;\n m++;\n }\n }\n\n vector<vector<int>> adjItems(n);\n vector<vector<int>> adjGroups(m);\n\n for(int i=0;i<n;i++){\n vector<int> node = beforeItems[i]; \n for(auto child : node){\n adjItems[child].push_back(i);\n if(group[child]!=group[i]){ \n adjGroups[group[child]].push_back(group[i]);\n }\n }\n }\n\n vector<int> sortedItems = topoSort(n, adjItems);\n vector<int> sortedGroups = topoSort(m, adjGroups);\n\n vector<vector<int>> sortedItemsGroup(m);\n for(auto item : sortedItems){\n sortedItemsGroup[group[item]].push_back(item);\n }\n\n vector<int> ans;\n for(auto grp : sortedGroups){\n for(auto node : sortedItemsGroup[grp]){\n ans.push_back(node);\n }\n }\n\n return ans;\n }\n};", "memory": "49787" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
0
{ "code": "class Solution {\npublic:\nvoid f(vector<int> adj[],vector<int> &in,vector<int> &v){\n queue<int> q;\n int n=in.size();\n for(int i=0;i<n;i++){\n if(in[i]==0){\n q.push(i);\n }\n }\n while(!q.empty()){\n int node=q.front();\n q.pop();\n v.push_back(node);\n for(auto it:adj[node]){\n in[it]--;\n if(in[it]==0){\n q.push(it);\n }\n }\n }\n}\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& bi) {\n vector<int> ans,gtopo,ntopo;\n // return ans;\n // vector<unordered_set<int>> v(m);\n vector<int> grpdeg(m+n,0),indegree(n,0),nadj[n],grpadj[m+n];\n for(int i=0;i<n;i++){\n if(group[i]==-1){\n group[i]=m++;\n }\n }\n for(int i=0;i<n;i++){\n for(int j=0;j<bi[i].size();j++){\n int dal=group[bi[i][j]];\n int node=bi[i][j];\n nadj[node].push_back(i);\n indegree[i]++;\n if(dal==group[i]){\n continue;\n }\n \n grpadj[dal].push_back(group[i]);\n grpdeg[group[i]]++;\n }\n }\n f(nadj,indegree,ntopo);\n f(grpadj,grpdeg,gtopo);\n if(ntopo.size()!=n ){\n return ans;\n }\n vector<int> sampl[m+n+1];\n for(auto it:ntopo){\n int dal=group[it];\n sampl[dal].push_back(it);\n }\n for(auto it:gtopo){\n for(auto itt:sampl[it]){\n ans.push_back(itt);\n }\n }\n if(ans.size()!=n){\n return {};\n }\n return ans; \n }\n};", "memory": "49787" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> topologicalSort(vector<vector<int>>& graph, vector<int>& inDegree) {\n queue<int> q;\n for (int i = 0; i < inDegree.size(); i++) {\n if (inDegree[i] == 0) {\n q.push(i);\n }\n }\n vector<int> order;\n while (!q.empty()) {\n int top = q.front();\n order.push_back(top);\n q.pop();\n for (int i = 0; i < graph[top].size(); i++) {\n int node = graph[top][i];\n inDegree[node]--;\n if (inDegree[node] == 0) {\n q.push(node);\n }\n }\n }\n return order;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int, int> groupSize;\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n group[i] = m;\n m++;\n }\n groupSize[group[i]]++;\n }\n vector<vector<int>> itemBeforeToAfter(n);\n vector<int> itemInDegree(n);\n vector<vector<int>> groupBeforeToAfter(m);\n vector<int> groupInDegree(m);\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int before : beforeItems[i]) {\n if (group[before] != group[i]) {\n groupBeforeToAfter[group[before]].push_back(group[i]);\n groupInDegree[group[i]]++;\n }\n itemBeforeToAfter[before].push_back(i);\n itemInDegree[i]++;\n }\n }\n vector<int> groupOrder = topologicalSort(groupBeforeToAfter, groupInDegree);\n if (groupOrder.size() != m) {\n return vector<int>();\n }\n vector<int> itemOrder = topologicalSort(itemBeforeToAfter, itemInDegree);\n if (itemOrder.size() != n) {\n return vector<int>();\n }\n vector<int> solution(n);\n unordered_map<int, int> groupIdx;\n for (int i = 1; i < m; i++) {\n groupIdx[groupOrder[i]] = groupIdx[groupOrder[i-1]] + groupSize[groupOrder[i-1]];\n }\n for (int i = 0; i < itemOrder.size(); i++) {\n int itemGroup = group[itemOrder[i]];\n solution[groupIdx[itemGroup]] = itemOrder[i];\n groupIdx[itemGroup]++;\n }\n return solution;\n }\n};", "memory": "51062" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n\n bool topo(vector<int> &ans,vector<int> mp[],vector<int> &in){\n queue<int> q;\n for(int i=0;i<in.size();i++){\n if(in[i]==0){\n q.push(i);\n }\n }\n\n while(!q.empty()){\n int u= q.front(); q.pop();\n ans.push_back(u);\n\n for(int v:mp[u]){\n in[v]--;\n if(in[v]==0) q.push(v);\n }\n }\n\n return (ans.size()==in.size());\n \n }\n vector<int> sortItems(int n, int m, vector<int>& grp, vector<vector<int>>& prev) {\n for(int i=0;i<n;i++){\n if(grp[i]==-1) grp[i]=m++;\n }\n\n vector<int> nd(n,0), gd(m,0);\n vector<int> mpn[n], mpg[m];\n\n for(int v=0;v<n;v++){\n for(int u:prev[v]){\n nd[v]++;\n mpn[u].push_back(v); // nodes ki directed list\n\n if(grp[u]!=grp[v]){\n gd[grp[v]]++; //grps ki directed list\n mpg[grp[u]].push_back(grp[v]);\n }\n }\n }\n vector<int> qitm, qgrp; // dono ki toposorted fixed sequence\n bool itm= topo(qitm, mpn,nd);\n bool grps= topo(qgrp,mpg,gd);\n\n if(!itm || !grps) return {};\n\n vector<int> ans;\n map<int,vector<int>> mp;\n for(int it:qitm){\n mp[grp[it]].push_back(it);\n }\n \n for(int x: qgrp){ \n for(auto y:mp[x]){ // jo grp pehle aaya ho uske elements bhar do jo ki pehle toposorted way me aaye hai \n ans.push_back(y);\n }\n }\n\n return ans;\n\n }\n};", "memory": "51062" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int>topo(vector<vector<int>>& graph, vector<int>& indegree){\n vector<int>sorted;\n queue<int>q;\n\n for(int i=0; i<indegree.size(); i++){\n if(indegree[i]==0){\n q.push(i);\n }\n }\n while(!q.empty()){\n int node=q.front();\n q.pop();\n sorted.push_back(node);\n\n for(auto it:graph[node]){\n indegree[it]--;\n if(indegree[it]==0){\n q.push(it);\n }\n }\n }\n if(sorted.size()==graph.size()){\n return sorted;\n }\n return {};\n\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int>ans;\n for(int i=0; i<n; i++){\n if(group[i]==-1){\n group[i]=m;\n m++;\n }\n }\n vector<vector<int>>adjlistitems(n, vector<int>());\n vector<vector<int>>adjlistgroups(m,vector<int>());\n vector<int>inditems(n,0);\n vector<int>indgrps(m,0);\n \n\n for(int i=0; i<n; i++){\n for(auto it:beforeItems[i]){\n adjlistitems[it].push_back(i);\n inditems[i]++;\n \n // if(group[i]!=group[it]){\n // adjlistgroups[group[it]].push_back(group[i]);\n // indgrps[group[i]]++;\n // }\n }\n }\n for(int i=0; i<n; i++){\n for(auto it:beforeItems[i]){\n if(group[i]!=group[it]){\n adjlistgroups[group[it]].push_back(group[i]);\n indgrps[group[i]]++;\n }\n }\n }\n vector<int>itemsorted=topo(adjlistitems, inditems);\n vector<int>groupsorted=topo(adjlistgroups, indgrps);\n\n if(itemsorted.size()==0 || groupsorted.size()==0){\n return {};\n }\n unordered_map<int,vector<int>>groupitems;\n\n for(auto it: itemsorted){\n groupitems[group[it]].push_back(it);\n }\n for(auto i: groupsorted){\n for(auto it: groupitems[i]){\n ans.push_back(it);\n }\n }\n // for(int i=0; i<groupsorted.size(); i++){\n // for(auto it: groupitems[i]){\n // ans.push_back(it);\n // }\n // }\n return ans;\n }\n};", "memory": "52337" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> topo_sort(vector<vector<int>>& adj ){\n // int count=0;\n // for(auto j:adj){\n // cout<<count++<<\" -> \";\n // for( auto i:j){\n // cout<<i<<\" \";\n // }\n // cout<<endl;\n // }\n // indegree;\n int n= adj.size();\n vector<int> indegree( n,0 );\n for(auto ad:adj){\n for( auto i:ad){\n indegree[i]++;\n }\n }\n vector<bool> visited(n,0);\n queue<int> qu;\n for( int i=0;i<n;i++){\n if(indegree[i]==0){\n indegree[i]--;\n visited[i]=1;\n qu.push(i);\n }\n }\n vector<int> ret;\n while(!qu.empty()){\n int tp = qu.front();\n qu.pop();\n ret.push_back(tp);\n for(auto j: adj[tp]){\n indegree[j]--;\n if(indegree[j]==0 && visited[j]==0){\n visited[j]=1;\n qu.push(j);\n }\n }\n }\n\n if(ret.size() != n) return {};\n return ret;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<vector<int>> graph_item(n);\n for(int i=0;i<n;i++){\n // create new group for -1\n if(group[i]==-1){\n group[i]=m++;\n }\n // create item to item adj list\n if(beforeItems[i].size()!= 0){\n for(auto j: beforeItems[i] ){\n graph_item[j].push_back(i);\n }\n }\n }\n\n vector<vector<int>> graph_grp(m);\n for(int i=0;i<n;i++){\n // create grp to grp adj list\n int self_grp = group[i];\n if(beforeItems[i].size()!= 0){\n for(auto j: beforeItems[i] ){\n int grp_bef = group[j];\n if( grp_bef != self_grp){\n graph_grp[grp_bef].push_back(self_grp);\n }\n }\n }\n }\n vector<int> topo_item = topo_sort(graph_item);\n if(topo_item.size()==0) return {};\n // for(auto i: topo_item){\n // cout<<i<<\" \";\n // }\n // cout<<endl;\n vector<int> topo_grp = topo_sort(graph_grp);\n if(topo_grp.size()==0) return {};\n // for(auto i: topo_grp){\n // cout<<i<<\" \";\n // }\n // cout<<endl;\n vector<vector<int>> ans(m);\n for(auto i: topo_item){\n ans[group[i]].push_back(i);\n }\n vector<int> ret;\n for(int i:topo_grp){\n for(auto j: ans[i]){\n ret.push_back(j);\n }\n }\n\n return ret;\n\n \n\n }\n};", "memory": "52337" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\n vector<int> topologicalSort(vector<vector<int>> &graph, vector<int> indegree){\n vector<int> visited,stack,empty;\n for(int i = 0;i<graph.size();i++){\n if(indegree[i] == 0)stack.push_back(i);\n }\n\n while(!stack.empty()){\n int cur = stack.back();\n visited.push_back(cur);\n stack.pop_back();\n for (auto &neigh :graph[cur]){\n indegree[neigh] -= 1;\n if(indegree[neigh] == 0)stack.push_back(neigh);\n }\n }\n\n if(visited.size() == graph.size())return visited;\n else return empty;\n }\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int gid= m;\n for(int g = 0;g<group.size();g++){\n if(group[g] == -1){\n group[g] = gid;\n gid += 1;\n }\n }\n\n vector<vector<int>> item_graph(n,vector<int>());\n vector<int> item_indegree(n,0);\n\n vector<vector<int>> group_graph(gid,vector<int>());\n vector<int> group_indegree(gid,0);\n\n for(int curr = 0;curr<n;curr++){\n for(auto &prev: beforeItems[curr]){\n item_graph[prev].push_back(curr);\n item_indegree[curr]+=1;\n\n if(group[curr] != group[prev]){\n group_graph[group[prev]].push_back(group[curr]);\n group_indegree[group[curr]] += 1;\n\n }\n }\n }\n\n vector<int> item_order = topologicalSort(item_graph,item_indegree);\n vector<int> group_order = topologicalSort(group_graph,group_indegree);\n\n unordered_map<int,vector<int>> mp;\n for(auto &item: item_order){\n // cout<<item<<endl;\n mp[group[item]].push_back(item);\n }\n\n vector<int> ans;\n // cout<<\"2\"<<endl;\n // for(auto &group: group_order){\n // cout<<group<<endl;\n // for(auto &v: mp[group])\n // cout<<v<<\" \";\n // cout<<endl;\n // }\n for(auto &group: group_order)ans.insert(ans.end(),mp[group].begin(),mp[group].end());\n return ans;\n }\n};", "memory": "53612" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n void dfs(int v,vector<bool>& vis,vector<int>& ans,vector<vector<int>>& g) {\n vis[v] = true;\n for (int u : g[v]) {\n if (!vis[u])\n dfs(u,vis,ans,g);\n }\n ans.push_back(v);\n }\n vector<int> topoSort(vector<vector<int>>& g,bool& f) {\n vector<bool> visited;\n vector<int> ans;\n visited.assign(g.size(), false);\n for (int i = 0; i < g.size(); i++) {\n if (!visited[i]) {\n dfs(i,visited,ans,g);\n }\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n vector<int> topoSort(vector<vector<int>>& g){\n int n=g.size();\n vector<int> inDegree(n,0);\n for(int i=0;i<n;i++){\n for(int j=0;j<g[i].size();j++){\n inDegree[g[i][j]]++;\n }\n }\n stack<int> st;\n for(int i=0;i<n;i++){\n if(inDegree[i]==0)st.push(i);\n }\n vector<int> ans;\n while(!st.empty()){\n int node=st.top();\n st.pop();\n ans.push_back(node);\n for(int i=0;i<g[node].size();i++){\n inDegree[g[node][i]]--;\n if(inDegree[g[node][i]]==0)st.push(g[node][i]);\n }\n }\n return ans;\n }\n void show(vector<int>& v){\n for(int i=0;i<v.size();i++){\n cout<<v[i]<<\" \";\n }\n cout<<endl;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int,int> gp;\n int freeGp=0;\n for(int i=0;i<group.size();i++){\n if(group[i]!=-1)gp[i]=group[i];\n else{\n gp[i]=m+freeGp;\n freeGp++;\n }\n }\n vector<vector<int>> gpDepend(m+freeGp);\n vector<vector<int>> graphWithoutInterDep(n);\n for(int i=0;i<beforeItems.size();i++){\n for(int j=0;j<beforeItems[i].size();j++){\n if(gp[i]!=gp[beforeItems[i][j]]){\n gpDepend[gp[beforeItems[i][j]]].push_back(gp[i]);\n }\n else{\n graphWithoutInterDep[beforeItems[i][j]].push_back(i);\n }\n }\n }\n bool f=true;\n vector<int> topoSortedGpDepend=topoSort(gpDepend);\n // if(!f)return {};\n vector<int> topoSortedInternalGraph=topoSort(graphWithoutInterDep);\n // if(!f)return {};\n show(topoSortedGpDepend);\n show(topoSortedInternalGraph);\n if(topoSortedGpDepend.size()!=m+freeGp)return {};\n if(topoSortedInternalGraph.size()!=n)return {};\n unordered_map<int,vector<int>> mp;\n vector<int> ans;\n for(int i=0;i<topoSortedInternalGraph.size();i++){\n mp[gp[topoSortedInternalGraph[i]]].push_back(topoSortedInternalGraph[i]);\n }\n for(int i=0;i<topoSortedGpDepend.size();i++){\n ans.insert(ans.end(),mp[topoSortedGpDepend[i]].begin(),mp[topoSortedGpDepend[i]].end());\n }\n return ans;\n }\n};", "memory": "53612" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i = 0; i < n; i++){\n if (group[i] == -1) group[i] = m++;\n }\n\n vector<unordered_set<int>> groupGraph(m, unordered_set<int>());\n vector<vector<int>> itemGraph(n, vector<int>());\n vector<int> itemIndegree(n, 0);\n vector<int> groupIndegree(m, 0);\n\n for(int item = 0; item < n; item++){\n for(auto& beforeItem : beforeItems[item]){\n itemGraph[beforeItem].push_back(item);\n int itemGroup = group[item];\n int beforeItemGroup = group[beforeItem];\n if (itemGroup != beforeItemGroup)\n groupGraph[beforeItemGroup].insert(itemGroup);\n }\n itemIndegree[item] += beforeItems[item].size();\n }\n\n for(int g = 0; g < m; g++){\n for(auto& ng : groupGraph[g]) groupIndegree[ng]++;\n }\n\n vector<int> groupTopo;\n queue<int> q;\n for(int g = 0; g < m; g++){\n if (groupIndegree[g] == 0) q.push(g);\n }\n\n while(!q.empty()){\n int g = q.front(); q.pop();\n groupTopo.push_back(g);\n for(auto& ng : groupGraph[g]){\n groupIndegree[ng]--;\n if (groupIndegree[ng] == 0){\n q.push(ng);\n }\n }\n }\n\n vector<vector<int>> groupToItems(m, vector<int>());\n\n for(int item = 0; item < n; item++){\n if (itemIndegree[item] == 0) q.push(item);\n }\n\n while(!q.empty()){\n int item = q.front(); q.pop();\n groupToItems[group[item]].push_back(item);\n for(auto& adjItem : itemGraph[item]){\n itemIndegree[adjItem]--;\n if (itemIndegree[adjItem] == 0){\n q.push(adjItem);\n }\n }\n }\n\n vector<int> ans;\n for(auto& g : groupTopo){\n for(auto& item : groupToItems[g]) ans.push_back(item);\n }\n\n if (ans.size() < n) return {};\n return ans;\n }\n};", "memory": "54887" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> toposort(int n,vector<int> adj[], vector<int> &indegree){\n vector<int> ans;\n queue<int> q;\n for(int i=0; i<n; i++){\n if(indegree[i]==0){\n q.push(i);\n }\n }\n while(!q.empty()){\n int node=q.front();\n q.pop();\n ans.push_back(node);\n for(auto it:adj[node]){\n indegree[it]--;\n if(indegree[it]==0){\n q.push(it);\n }\n }\n }\n return ans;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int tot_grp=m;\n for(int i=0; i<n; i++){\n if(group[i]==-1){\n group[i]=tot_grp;\n tot_grp++;\n }\n }\n vector<int> adj1[n];\n vector<int> adj2[tot_grp];\n vector<int> indegree1(n,0);\n vector<int> indegree2(tot_grp,0);\n for(int i=0; i<n; i++){\n for(auto j:beforeItems[i]){\n adj1[j].push_back(i);\n indegree1[i]++;\n if(group[j]!=group[i]){\n indegree2[group[i]]++;\n adj2[group[j]].push_back(group[i]);\n }\n }\n }\n vector<int> topo1=toposort(n,adj1,indegree1);\n vector<int> topo2=toposort(tot_grp,adj2,indegree2);\n if(topo1.size()!=n || topo2.size()!=tot_grp){\n return {};\n }\n vector<int> ans;\n vector<vector<int>> temp;\n map<int,vector<int>> mp;\n for(auto it:topo1){\n mp[group[it]].push_back(it);\n }\n for(auto it:topo2){\n temp.push_back(mp[it]);\n }\n for(auto it=temp.begin();it!=temp.end();it++){\n for(auto j:*it){\n ans.push_back(j);\n }\n }\n return ans;\n }\n};", "memory": "54887" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "#include <ranges>\n\ntemplate <typename T> concept Callback = invocable<T, int>;\n\nclass Solution {\npublic:\n template<ranges::range R, ranges::range R2, Callback F>\n bool toposort(R& g, vector<int>& deg, const vector<R2>& edge, F& op) {\n int count = 0, total = 0;\n vector<int> stack;\n\n for (auto x: g) {\n if (deg[x] == 0) stack.push_back(x);\n ++total;\n }\n\n while (stack.size()) {\n int x = stack.back();\n stack.pop_back();\n\n for (auto y: edge[x]) {\n if (--deg[y] == 0) {\n stack.push_back(y);\n }\n }\n if (!op(x)) return false;\n count++;\n }\n\n return count == total;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int old_m = m;\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m++;\n }\n vector<unordered_set<int>> outer_edge(m);\n vector<int> inner_deg(n), outer_deg(m);\n vector<unordered_set<int>> g(m);\n\n for (int i = 0; i < n; i++) g[group[i]].insert(i);\n\n for (int i = 0; i < n; i++) {\n int gi = group[i];\n for (auto j: beforeItems[i]) {\n int gj = group[j];\n if (gi == gj) {\n inner_deg[j]++;\n } else {\n auto [_, b] = outer_edge[gi].insert(gj);\n if (b) outer_deg[gj]++;\n }\n }\n\n if (gi >= old_m) continue;\n auto pred = [&group, gi](int x) {\n return group[x] == gi;\n };\n auto r = ranges::partition(beforeItems[i], pred);\n beforeItems[i].resize(r.begin() - beforeItems[i].begin());\n }\n\n auto nonempty_groups = views::iota(0, m) | views::filter([&](int x) { return g[x].size(); });\n vector<int> ans;\n auto op_inner = [&ans](int x) {\n ans.push_back(x);\n return true;\n };\n auto op = [&](int x) {\n if (g[x].size() == 1) {\n ans.push_back(*g[x].begin());\n return true;\n }\n return toposort(g[x], inner_deg, beforeItems, op_inner);\n };\n\n if (!toposort(nonempty_groups, outer_deg, outer_edge, op)) return {};\n ranges::reverse(ans);\n return ans;\n }\n};", "memory": "56162" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> topoSort(unordered_map<int,list<int>>& adjList,vector<int>& inDegree)\n {\n int n = inDegree.size();\n queue<int> q;\n for(int i = 0 ; i < n ; i++)\n if(inDegree[i] == 0)\n q.push(i);\n\n vector<int> ans;\n while(!q.empty())\n {\n int src = q.front();\n q.pop();\n ans.push_back(src);\n\n for(auto& neigh : adjList[src])\n {\n inDegree[neigh]--;\n if(inDegree[neigh] == 0)\n q.push(neigh);\n }\n }\n\n return (ans.size() != n) ? vector<int>{} : ans;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& grp, vector<vector<int>>& beforeItems) {\n unordered_map<int,list<int>> depGraph;\n for(int i = 0 ; i < n ; i++)\n { \n for(int x : beforeItems[i])\n depGraph[x].push_back(i);\n }\n\n vector<int> depGraphInDegree(n,0);\n for(auto& [src,neighList] : depGraph)\n {\n for(auto& neigh : neighList)\n depGraphInDegree[neigh]++;\n }\n\n vector<int> depGraphTopoSort = topoSort(depGraph,depGraphInDegree);\n if (depGraphTopoSort.empty()) \n return {};\n\n // Modify the group array to ensure independent nodes\n for(int& x : grp)\n {\n if(x == -1)\n x = m++;\n }\n\n unordered_map<int,list<int>> grpGraph;\n for(int i = 0 ; i < n ; i++)\n {\n for(int x : beforeItems[i])\n {\n if(grp[x] != grp[i])\n grpGraph[grp[x]].push_back(grp[i]);\n }\n }\n\n vector<int> grpGraphInDegree(m,0);\n for(auto& [src,neighList] : grpGraph)\n {\n for(auto& neigh : neighList)\n grpGraphInDegree[neigh]++;\n }\n\n vector<int> grpGraphTopoSort = topoSort(grpGraph,grpGraphInDegree);\n if (grpGraphTopoSort.empty()) \n return {};\n \n vector<vector<int>> combinedTopoSort(m);\n for(int& x : depGraphTopoSort)\n combinedTopoSort[grp[x]].push_back(x);\n \n vector<int> ans;\n for(int& x : grpGraphTopoSort)\n {\n for(int y : combinedTopoSort[x])\n ans.push_back(y);\n }\n\n return ans;\n }\n};", "memory": "59987" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool topoSort(vector<vector<int>> adj ,vector<int> &topo,vector<int> &deg){\n queue<int> q;\n for(int i=0;i<deg.size();i++){\n if(deg[i]==0)\n q.push(i);\n }\n while(!q.empty()){\n int curr=q.front();\n q.pop();\n topo.push_back(curr);\n for(int v : adj[curr]){\n if(--deg[v]==0)\n q.push(v);\n }\n }\n return topo.size()==deg.size();\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& bef) {\n for(auto &i: group) \n if(i == -1) i = m++;\n\n vector<int> ideg(n,0), gdeg(m,0);\n vector<vector<int>> iadj(n) , gadj(m);\n for(int i = 0; i< n ; i++){\n for(auto j : bef[i]){\n iadj[j].push_back(i);\n ideg[i]++;\n if(group[i]!=group[j])\n gadj[group[j]].push_back(group[i]),gdeg[group[i]]++;\n }\n }\n vector<int> grp, it;\n vector<int> ans;\n if(topoSort(iadj, it, ideg) && topoSort(gadj, grp, gdeg)){\n unordered_map<int,vector<int>> mp;\n for(int i : it){\n mp[group[i]].push_back(i);\n }\n vector<int> ans;\n for(int g : grp){\n vector<int> temp = mp[g];\n for(auto i : temp){\n ans.push_back(i);\n }\n }\n return ans;\n }\n return {};\n\n }\n};\n", "memory": "59987" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> toposort(unordered_map<int, vector<int>>& adj, vector<int>& indegree) {\n queue<int> q;\n for (int i = 0; i < indegree.size(); i++) {\n if (indegree[i] == 0) q.push(i);\n }\n vector<int> ans;\n while (!q.empty()) {\n int node = q.front(); q.pop();\n ans.push_back(node);\n for (auto it : adj[node]) {\n indegree[it]--;\n if (indegree[it] == 0) q.push(it);\n }\n }\n return ans;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m++;\n }\n\n unordered_map<int, vector<int>> adj1, adj2;\n vector<int> indegree1(n, 0), indegree2(m, 0);\n\n for (int v = 0; v < n; v++) {\n for (auto u : beforeItems[v]) {\n adj1[u].push_back(v);\n indegree1[v]++;\n\n if (group[v] != group[u]) {\n int prev = group[u];\n int curr = group[v];\n adj2[prev].push_back(curr);\n indegree2[curr]++;\n }\n }\n }\n\n vector<int> itemOrder = toposort(adj1, indegree1);\n vector<int> groupOrder = toposort(adj2, indegree2);\n\n if (itemOrder.size() != n || groupOrder.size() != m) return {};\n\n unordered_map<int, vector<int>> groupedItems;\n for (int v : itemOrder) {\n groupedItems[group[v]].push_back(v);\n }\n\n vector<int> result;\n for (int g : groupOrder) {\n for (int v : groupedItems[g]) {\n result.push_back(v);\n }\n }\n\n return result;\n }\n};\n", "memory": "61262" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int>toposort(unordered_map<int,vector<int>>& adj,vector<int>& indegree){\n queue<int>q;\n for(int i=0;i<indegree.size();i++){\n if(indegree[i]==0) q.push(i);\n }\n vector<int>ans;\n while(!q.empty()){\n int node=q.front(); q.pop();\n ans.push_back(node);\n for(auto it:adj[node]){\n indegree[it]--;\n if(indegree[it]==0) q.push(it);\n }\n }\n return ans;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i=0;i<n;i++){\n if(group[i]==-1) group[i]=m++;\n }\n unordered_map<int,vector<int>>adj1,adj2;\n vector<int>indegree1(n,0),indegree2(m,0);\n\n for(int i=0;i<n;i++){\n int v=i;\n for(auto u:beforeItems[v]){\n adj1[u].push_back(v);\n indegree1[v]++;\n \n if(group[v]!=group[u]){\n int prev=group[u];\n int curr=group[v];\n adj2[prev].push_back(curr);\n indegree2[curr]++;\n }\n }\n }\n vector<int>itemOrder=toposort(adj1,indegree1);\n vector<int>groupOrder=toposort(adj2,indegree2);\n for(auto it:groupOrder) cout<<it<<\" \";\n if(itemOrder.size()!=n || groupOrder.size()!=m) return {};\n\n unordered_map<int,vector<int>>groupedItems;\n for(auto it:itemOrder){\n groupedItems[group[it]].push_back(it);\n }\n vector<int>ans;\n for(auto it:groupOrder){\n for(auto num:groupedItems[it]){\n ans.push_back(num);\n }\n }\n return ans;\n }\n};", "memory": "61262" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> topSort(int n, map<int, vector<int>>& adj, vector<int>& indegree) {\n queue<int> q;\n vector<int> sorted;\n for (int i = 0; i < n; ++i) {\n if (indegree[i] == 0) {\n q.push(i);\n }\n }\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n sorted.push_back(node);\n \n for (int neighbor : adj[node]) {\n indegree[neighbor]--;\n if (indegree[neighbor] == 0) {\n q.push(neighbor);\n }\n }\n }\n if (sorted.size() == n) {\n return sorted;\n }\n return {};\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int newGroupId = m;\n for (int i = 0; i < n; ++i) {\n if (group[i] == -1) {\n group[i] = newGroupId++;\n }\n }\n map<int, vector<int>> itemAdj, groupAdj;\n vector<int> itemIndegree(n, 0), groupIndegree(newGroupId, 0);\n for (int i = 0; i < n; ++i) {\n for (int before : beforeItems[i]) {\n itemAdj[before].push_back(i); \n itemIndegree[i]++;\n if (group[i] != group[before]) {\n groupAdj[group[before]].push_back(group[i]); \n groupIndegree[group[i]]++;\n }\n }\n }\n vector<int> sortedGroups = topSort(newGroupId, groupAdj, groupIndegree);\n if (sortedGroups.empty()) {\n return {}; \n }\n vector<int> sortedItems = topSort(n, itemAdj, itemIndegree);\n if (sortedItems.empty()) {\n return {}; \n }\n map<int, vector<int>> groupToItems;\n for (int item : sortedItems) {\n groupToItems[group[item]].push_back(item);\n }\n\n vector<int> result;\n for (int g : sortedGroups) {\n for (int item : groupToItems[g]) {\n result.push_back(item);\n }\n }\n \n return result;\n }\n};\n", "memory": "62537" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> topoSort(vector<vector<int>>& adj, vector<int>& inDegree) {\n queue<int> q;\n for(int i = 0; i < inDegree.size(); i++) {\n if (inDegree[i] == 0) q.push(i);\n }\n vector<int> res;\n while(!q.empty()) {\n int node = q.front();\n q.pop();\n res.push_back(node);\n for(int neighbor: adj[node]) {\n inDegree[neighbor]--;\n if(inDegree[neighbor] == 0) q.push(neighbor);\n }\n }\n return res.size() == inDegree.size() ? res : vector<int>();\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int groupID = m;\n for(int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m;\n m++;\n }\n vector<vector<int>> itemAdj(n);\n vector<int> itemInd(n);\n vector<vector<int>> groupAdj(m);\n vector<int> groupInd(m);\n for(int i = 0; i < n; i++) {\n for(int prev: beforeItems[i]) {\n itemAdj[prev].push_back(i);\n itemInd[i]++;\n if(group[i] != group[prev]) {\n groupAdj[group[prev]].push_back(group[i]);\n groupInd[group[i]]++;\n }\n }\n }\n vector<int> itemOrder = topoSort(itemAdj, itemInd);\n vector<int> groupOrder = topoSort(groupAdj, groupInd);\n\n if(itemOrder.empty() || groupOrder.empty()) return vector<int>();\n unordered_map<int,vector<int>> ordered_groups;\n for(int item: itemOrder) {\n ordered_groups[group[item]].push_back(item);\n }\n vector<int> res;\n for(int group: groupOrder) {\n res.insert( res.end(), ordered_groups[group].begin(), ordered_groups[group].end() );\n }\n return res;\n }\n};", "memory": "62537" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "#include <ranges>\n\nclass Solution {\npublic:\n template<ranges::range R, ranges::range R2>\n bool toposort(R& g, vector<int>& deg, vector<R2>& edge, function<bool(int)> op) {\n int count = 0, total = 0;\n vector<int> stack;\n\n for (auto x: g) {\n if (deg[x] == 0) stack.push_back(x);\n ++total;\n }\n\n while (stack.size()) {\n int x = stack.back();\n stack.pop_back();\n\n for (auto y: edge[x]) {\n deg[y]--;\n if (deg[y] == 0) {\n stack.push_back(y);\n }\n }\n if (!op(x)) break;\n count++;\n }\n\n return count == total;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m++;\n }\n vector<unordered_set<int>> outer_edge(m);\n vector<vector<int>> inner_edge(n);\n vector<int> inner_deg(n), outer_deg(m);\n vector<unordered_set<int>> g(m);\n\n for (int i = 0; i < n; i++) g[group[i]].insert(i);\n\n for (int i = 0; i < n; i++) {\n int gi = group[i];\n for (auto j: beforeItems[i]) {\n int gj = group[j];\n if (gi == gj) {\n inner_edge[j].push_back(i);\n inner_deg[i]++;\n } else {\n auto [_, b] = outer_edge[gj].insert(gi);\n if (b) outer_deg[gi]++;\n }\n }\n }\n\n auto nonempty_groups = views::iota(0, m) | views::filter([&](int x) { return g[x].size(); });\n vector<int> ans;\n auto op_inner = [&](int x) {\n ans.push_back(x);\n return true;\n };\n auto op = [&](int x) {\n return toposort(g[x], inner_deg, inner_edge, op_inner);\n };\n toposort(nonempty_groups, outer_deg, outer_edge, op);\n\n if (ans.size() != n) return {};\n return ans;\n }\n};", "memory": "63812" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "#include <ranges>\n\nclass Solution {\npublic:\n template<ranges::range R, ranges::range R2, typename T>\n bool toposort(R& g, vector<int>& deg, const vector<R2>& edge, T op) {\n int count = 0, total = 0;\n vector<int> stack;\n\n for (auto x: g) {\n if (deg[x] == 0) stack.push_back(x);\n ++total;\n }\n\n while (stack.size()) {\n int x = stack.back();\n stack.pop_back();\n\n for (auto y: edge[x]) {\n deg[y]--;\n if (deg[y] == 0) {\n stack.push_back(y);\n }\n }\n if (!op(x)) return false;\n count++;\n }\n\n return count == total;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m++;\n }\n vector<unordered_set<int>> outer_edge(m);\n vector<vector<int>> inner_edge(n);\n vector<int> inner_deg(n), outer_deg(m);\n vector<unordered_set<int>> g(m);\n\n for (int i = 0; i < n; i++) g[group[i]].insert(i);\n\n for (int i = 0; i < n; i++) {\n int gi = group[i];\n for (auto j: beforeItems[i]) {\n int gj = group[j];\n if (gi == gj) {\n inner_edge[j].push_back(i);\n inner_deg[i]++;\n } else {\n auto [_, b] = outer_edge[gj].insert(gi);\n if (b) outer_deg[gi]++;\n }\n }\n }\n\n auto nonempty_groups = views::iota(0, m) | views::filter([&](int x) { return g[x].size(); });\n vector<int> ans;\n auto op_inner = [&](int x) {\n ans.push_back(x);\n return true;\n };\n auto op = [&](int x) {\n return toposort(g[x], inner_deg, inner_edge, op_inner);\n };\n toposort(nonempty_groups, outer_deg, outer_edge, op);\n\n if (ans.size() != n) return {};\n return ans;\n }\n};", "memory": "63812" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n vector<int> topoSort(unordered_map<int, vector<int>>& neighbourMap, vector<int>& inDegree){\n queue<int> q; // contains nodes whose dependency have been resolved, aka indeg = 0\n for(pair<int, vector<int>> p : neighbourMap){\n if(inDegree[p.first] == 0){\n q.push(p.first);\n }\n }\n\n vector<int> order;\n while(!q.empty()){\n int front = q.front();\n q.pop();\n order.push_back(front);\n\n for(int neighbour : neighbourMap[front]){\n inDegree[neighbour]--;\n if(inDegree[neighbour] == 0){\n q.push(neighbour);\n }\n }\n }\n\n return order.size() == neighbourMap.size() ? order : vector<int>(); \n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n // first assign groups to the items that do not belong to any group\n for(int i = 0; i < n; i++){\n if(group[i] == -1){\n group[i] = m++;\n }\n }\n\n // initialize the itemGraph, iteminDegree\n unordered_map<int, vector<int>> itemGraph;\n vector<int> iteminDegree(n, 0);\n for(int i = 0; i < n; i++){\n itemGraph[i] = vector<int>();\n }\n\n // initialize the groupGraph, groupinDegree\n unordered_map<int, vector<int>> groupGraph;\n vector<int> groupinDegree(m, 0);\n for(int i = 0; i < m; i++){\n groupGraph[i] = vector<int>();\n }\n\n // step 1 : build the itemGraph and groupGraph by the help of before list\n for(int item = 0; item < n; item++){\n\n for(int prev : beforeItems[item]){\n // itemGraph\n itemGraph[prev].push_back(item); // prev --> item\n iteminDegree[item]++;\n\n // groupGraph\n if(group[item] != group[prev]){\n int itemGroup = group[item];\n int prevGroup = group[prev];\n\n groupGraph[prevGroup].push_back(itemGroup); // prevGroup -> itemGroup\n groupinDegree[itemGroup]++;\n }\n }\n\n }\n\n // step 2 : get the topological order of itemGraph and groupGraph\n vector<int> itemOrder = topoSort(itemGraph, iteminDegree);\n vector<int> groupOrder = topoSort(groupGraph, groupinDegree);\n\n if(itemOrder.empty() || groupOrder.empty()) {\n return {}; // Return an empty vector if there's a cycle\n }\n\n // step 3 : arrange the ordered into final answer (you can use a hashMap)\n unordered_map<int, vector<int>> hashMap;\n for(int& item : itemOrder){\n int itemGroup = group[item];\n hashMap[itemGroup].push_back(item);\n }\n\n vector<int> ans;\n\n for(int& group : groupOrder){\n ans.insert(ans.end(), hashMap[group].begin(), hashMap[group].end());\n }\n\n return ans;\n }\n};\n\n/*\n\n first job is to make sure that every item has a group. not -1. so assign a valid group to each item.\n\n => you first get the idea that it requires topological sort. which is easy to get after observing carefully.\n => for that you need a graph\n => you build a itemgraph (graph with items as nodes) using beforeList\n => you find the topo sort of this itemGraph.\n => you get an ans. eg {0, 6, 3, 7, 5, 1, 4, 2}\n => but it is incorrect as 6,3,4 are from the same group. but they are seperated in this ans.\n => this is because you never cared about the second condition.\n => now there are two ways to proceed\n 1st : you have the items sort. just traverse at them and save their groups in a uSet (to avoid duplicacy)\n and now you have the group order and also the item order.\n 2nd : while building the itemGraph. build a groupGraph also. and do the topological sort of the group\n with the dag of group.\n => either way you have the orders of both. then building the final answer from there is EASY. \n*/", "memory": "65087" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int>topo(unordered_map<int,list<int>>&adj,vector<int>&indegree){\n queue<int>q;\n for(int i=0;i<indegree.size();i++){\n if(indegree[i]==0)q.push(i);\n }\n vector<int>ans;\n while(!q.empty()){\n int front=q.front();\n ans.push_back(front);\n q.pop();\n for(auto nbr: adj[front]){\n indegree[nbr]--;\n if(indegree[nbr]==0)q.push(nbr);\n\n }\n }\n if (ans.size() != indegree.size()) return {}; // return empty if cycle exists\n \n return ans;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int,list<int>>listadj;\n unordered_map<int,list<int>>groupadj;\n\n\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) group[i] = m++;\n }\n\n vector<int>degreelist(n,0);\n vector<int>degreegroup(m,0);\n for(int i=0;i<group.size();i++){ \n for(auto v: beforeItems[i]){\n listadj[v].push_back(i);\n degreelist[i]++;\n\n if (group[v] != group[i]) { // Only add group dependency if they are different\n groupadj[group[v]].push_back(group[i]);\n degreegroup[group[i]]++;\n }\n }\n\n }\n vector<int>topolist=topo(listadj,degreelist);\n vector<int>topogroup=topo(groupadj,degreegroup);\n unordered_map<int,list<int>>tempans;\n for(auto v: topolist){\n tempans[group[v]].push_back(v);\n }\n\n vector<int>ans;\n for(auto v: topogroup){\n ans.insert(ans.end(),tempans[v].begin(),tempans[v].end());\n }\n\n return ans;\n }\n};", "memory": "65087" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i=0;i<group.size();i++){\n if(group[i]==-1)group[i]=m++;\n }\n map<int,vector<int>> mp;\n for(int i=0;i<group.size();i++)group[i]++;\n vector<int> adj[n+1];\n vector<int> adj1[m+1];\n vector<int> ind(n+1,0),in(m+1,0);\n\n map<pair<int,int>,int> grpedge;\n for(int i=0;i<beforeItems.size();i++){\n if(beforeItems[i].empty())continue;\n for(auto it:beforeItems[i]){\n adj[it].push_back(i);\n ind[i]++;\n \n if(group[i]!=group[it]){\n if(grpedge.find({group[it],group[i]})!=grpedge.end())continue;\n adj1[group[it]].push_back(group[i]);\n in[group[i]]++;\n grpedge[{group[it],group[i]}]++;\n }\n }\n }\n for(int i=0;i<group.size();i++){\n mp[group[i]].push_back(i);\n }\n int val=0;\n queue<int> q;\n map<int,int> item,grp;\n for(int i=0;i<n;i++){\n if(ind[i]==0)q.push(i);\n }\n while(!q.empty()){\n int sz=q.size();\n for(int i=0;i<sz;i++){\n auto tp=q.front();\n item[tp]=val++;\n q.pop();\n for(auto it:adj[tp]){\n if(--ind[it]!=0)continue;\n q.push(it);\n }\n }\n }\n if(val!=n)return {};\n val=0;\n for(int i=0;i<=m;i++){\n if(in[i]==0)q.push(i);\n }\n while(!q.empty()){\n int sz=q.size();\n for(int i=0;i<sz;i++){\n auto tp=q.front();\n grp[tp]=val++;\n q.pop();\n for(auto it:adj1[tp]){\n if(--in[it]!=0)continue;\n q.push(it);\n }\n }\n }\n // cout<<val<<\" \"<<m;\n if(val!=(m+1))return {};\n vector<pair<int,int>> x;\n for(auto it:grp){\n x.push_back({it.second,it.first});\n }\n vector<int> ans;\n sort(x.begin(),x.end());\n for(auto it:x){\n vector<pair<int,int>> y;\n for(auto ti:mp[it.second]){\n y.push_back({item[ti],ti});\n }\n sort(y.begin(),y.end());\n for(auto ti:y)\n ans.push_back(ti.second);\n }\n if(ans.size()!=n)return {};\n return ans;\n\n }\n};", "memory": "66362" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> bfs(queue<int> &q, unordered_map<int, vector<int>>& adj, unordered_map<int, int>& indegree){\n vector<int> ans;\n while(!q.empty()){\n int top = q.front();\n q.pop();\n\n for(auto nbr: adj[top]){\n indegree[nbr]--;\n if(indegree[nbr] == 0){\n q.push(nbr);\n }\n }\n ans.push_back(top);\n }\n return ans;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) {\n group[i] = m++;\n }\n }\n \n unordered_map<int, vector<int>> itemsAdj;\n unordered_map<int, int> itemsIn;\n\n for(int i = 0; i < n; i++){\n for(int j = 0; j < beforeItems[i].size(); j++){\n itemsAdj[beforeItems[i][j]].push_back(i);\n }\n itemsIn[i] = beforeItems[i].size();\n } \n\n queue<int> q1;\n \n for(auto it: itemsIn){\n if(it.second == 0){\n q1.push(it.first);\n }\n }\n\n vector<int> itemsOrder = bfs(q1, itemsAdj, itemsIn);\n if(itemsOrder.empty() || itemsOrder.size() != n){\n return {};\n }\n \n cout<<\"itemsOrder: \"<<endl;\n for(auto it: itemsOrder){\n cout<<it<<\", \";\n }\n cout<<endl;\n\n unordered_map<int, vector<int>> groupsMap;\n for(auto i: itemsOrder){\n int groupOfEl = group[i];\n groupsMap[groupOfEl].push_back(i);\n }\n\n unordered_map<int, vector<int>> groupsAdj;\n unordered_map<int, int> groupsIn;\n for(int i = 0; i < n; i++){\n for(int j = 0; j < beforeItems[i].size(); j++){\n cout<<\"i: \"<<i<<\" size\"<<beforeItems[i].size()<<endl;\n if(group[beforeItems[i][j]] != group[i]){\n groupsAdj[group[beforeItems[i][j]]].push_back(group[i]);\n groupsIn[group[i]]++;\n }\n }\n }\n\n cout<<\"groupsAdj: \"<<endl;\n for(auto it: groupsAdj){\n cout<<it.first<<\"--> \";\n for(auto i: it.second){\n cout<<i<<\", \";\n }\n cout<<endl;\n }\n cout<<endl;\n for(auto it: groupsMap){\n if(!groupsIn.count(it.first)){\n groupsIn[it.first] = 0;\n }\n }\n cout<<\"groupsIn: \"<<endl;\n for(auto it: groupsIn){\n cout<<it.first<<\"-> \"<<it.second<<endl;\n }\n cout<<endl;\n\n \n\n queue<int> q2;\n \n for(auto it: groupsIn){\n if(it.second == 0){\n q2.push(it.first);\n }\n }\n \n vector<int> groupsOrder = bfs(q2, groupsAdj, groupsIn);\n if(groupsOrder.empty() || groupsOrder.size() != groupsMap.size()){\n return {};\n }\n cout<<\"groupsOrder: \"<<endl;\n for(auto it: groupsOrder){\n cout<<it<<\", \";\n }\n cout<<endl;\n\n vector<int> res;\n\n for(auto it: groupsOrder){\n for(auto i: groupsMap[it]){\n res.push_back(i);\n }\n }\n\n return res;\n }\n};", "memory": "66362" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int, int> groupOf;\n int totalGroups = m;\n for(int i = 0; i < n; i++){\n if (group[i] == -1) {\n groupOf[i] = totalGroups++;\n }\n else{\n groupOf[i] = group[i];\n }\n \n }\n\n unordered_map<int, unordered_set<int>> groupAdj;\n vector<vector<int>> itemAdj(n, vector<int>());\n vector<int> groupIndegree(totalGroups, 0);\n vector<int> itemIndegree(n, 0);\n\n for(int i = 0; i < n; i++){\n for(auto& item : beforeItems[i]){\n // item -> i\n int iG = groupOf[i];\n int itemG = groupOf[item];\n if (iG != itemG)\n groupAdj[itemG].insert(iG);\n itemAdj[item].push_back(i);\n itemIndegree[i]++;\n }\n }\n\n for(int i = 0; i < totalGroups; i++){\n for(auto& adjGroup : groupAdj[i]){\n groupIndegree[adjGroup]++;\n }\n }\n\n\n queue<int> q;\n\n vector<int> groupTopo;\n for(int i = 0; i < totalGroups; i++){\n if (groupIndegree[i] == 0){\n q.push(i);\n }\n cout << \"-------------------------------------\" << endl;\n cout << i << endl;\n cout << \"ADJ BOYS \" << endl;\n for(auto& g : groupAdj[i]) cout << g << endl;\n cout << \"-------------------------------------\" << endl;\n }\n\n while(!q.empty()){\n int g = q.front(); q.pop();\n groupTopo.push_back(g);\n for(auto& adjNode : groupAdj[g]){\n groupIndegree[adjNode]--;\n if (groupIndegree[adjNode] == 0) q.push(adjNode);\n }\n }\n\n for(int i = 0; i < n; i++){\n if (itemIndegree[i] == 0){\n q.push(i);\n }\n }\n\n vector<int> ansTopo;\n\n unordered_map<int, vector<int>> groupItem;\n\n while(!q.empty()){\n int item = q.front(); q.pop();\n groupItem[groupOf[item]].push_back(item);\n for(auto& adjItem : itemAdj[item]){\n itemIndegree[adjItem]--;\n if (itemIndegree[adjItem] == 0) q.push(adjItem);\n }\n }\n\n for(auto& g : groupTopo){\n for(auto& item : groupItem[g]) ansTopo.push_back(item);\n }\n\n if (ansTopo.size() < n) return {};\n return ansTopo;\n }\n};", "memory": "67637" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i = 0; i < n; ++i)\n if(group[i] == -1)\n group[i] = m++;\n\n vector<unordered_set<int>> graphGroup(m), graphItem(n);\n vector<int> inGroup(m), inItem(n);\n for(int i = 0; i < n; i++) {\n int to_group = group[i];\n for(int from : beforeItems[i]) {\n int from_group = group[from];\n if(from_group != to_group && !graphGroup[from_group].count(to_group)) {\n graphGroup[from_group].insert(to_group);\n inGroup[to_group]++;\n }\n if(!graphItem[from].count(i)) {\n graphItem[from].insert(i);\n inItem[i]++;\n }\n }\n }\n\n vector<int> group_sorted = topoSort(graphGroup, inGroup);\n vector<int> item_sorted = topoSort(graphItem, inItem);\n if(group_sorted.empty() || item_sorted.empty()) return {};\n\n vector<vector<int>> group2item(m);\n for(auto item : item_sorted){\n group2item[group[item]].push_back(item);\n }\n \n vector<int> ans;\n for(int group_id : group_sorted) {\n for(int item : group2item[group_id]) {\n ans.push_back(item);\n }\n }\n \n return ans;\n }\n \n vector<int> topoSort(vector<unordered_set<int>>& graph, vector<int>& indegree) {\n vector<int> ans;\n queue<int> q;\n for(int i = 0; i <indegree.size(); i++) {\n if(indegree[i] == 0) q.push(i);\n }\n \n while(!q.empty()) {\n int t = q.front();\n q.pop();\n \n ans.push_back(t);\n \n for(int next : graph[t]) {\n --indegree[next];\n if(indegree[next] == 0) {\n q.push(next);\n }\n }\n }\n \n for(int i = 0; i < indegree.size(); i++) {\n if(indegree[i] > 0) return {};\n }\n \n return ans;\n }\n};", "memory": "67637" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n //vector<vector<int>> member(m, vector<int>());\n unordered_set<int> member[m];\n for (int i = 0; i < n; i++) {\n if (group[i] > -1) {\n member[group[i]].insert(i);\n }\n }\n\n //vector<vector<int>> beforeGroup(m, vector<int>());\n unordered_set<int> beforeGroup[m];\n for (int i = 0; i < m; i++) {\n for (auto j : member[i]) {\n for (auto k : beforeItems[j]) {\n if (member[i].count(k) == 0) {\n beforeGroup[i].insert(k);\n }\n }\n }\n\n for (auto j : member[i]) {\n unordered_set<int> s(beforeItems[j].begin(), beforeItems[j].end());\n for (auto k : beforeGroup[i]) {\n if (s.count(k) == 0) {\n beforeItems[j].push_back(k);\n }\n }\n }\n }\n\n queue<int> q;\n vector<vector<int>> out(n, vector<int>());\n vector<int> in(n, 0);\n queue<int> g;\n for (int i = 0; i < n; i++) {\n in[i] = beforeItems[i].size();\n for (auto k : beforeItems[i]) {\n out[k].push_back(i);\n }\n if (in[i] == 0) {\n q.push(i);\n }\n }\n\n vector<int> v, ret;\n while (!q.empty()) {\n v.push_back(q.front());\n for (auto k : out[q.front()]) {\n in[k]--;\n if (in[k] == 0) {\n q.push(k);\n }\n }\n q.pop();\n }\n \n if (v.size() != n) {\n return {};\n }\n\n vector<vector<int>> groupOrder(m, vector<int>());\n for (auto k : v) {\n if (group[k] != -1) {\n groupOrder[group[k]].push_back(k);\n }\n }\n\n vector<bool> useGroup(m, false);\n for (auto k : v) {\n if (group[k] == -1) {\n ret.push_back(k);\n } else {\n if (useGroup[group[k]]) continue;\n useGroup[group[k]] = true;\n for (auto l : groupOrder[group[k]]) {\n ret.push_back(l);\n }\n }\n }\n\n return ret;\n }\n};", "memory": "68912" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> khansalgo(unordered_map<int,vector<int>>m,vector<int>count){\n vector<int>result;\n queue<int>q;\n for(int i=0;i<count.size();i++){\n if(count[i]==0){\n q.push(i);\n }\n }\n while(!q.empty()){\n int node=q.front();\n q.pop();\n result.push_back(node);\n for(auto it:m[node]){\n count[it]--;\n if(count[it]==0){\n q.push(it);\n }\n }\n }\n if(result.size()!=count.size()) return {};\n return result;\n\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n \n for(int i=0;i<n;i++){\n if(group[i]==-1){\n \n group[i]=m++;\n }\n }\n unordered_map<int,vector<int>>elements;\n vector<int>count1(n);\n vector<int>count2(m);\n unordered_map<int,vector<int>>groups;\n for(int i=0;i<n;i++){\n int cgroup=group[i];\n for(auto it:beforeItems[i]){\n int pgroup=group[it];\n elements[it].push_back(i);\n count1[i]++;\n if(cgroup!=pgroup){\n groups[pgroup].push_back(cgroup);\n count2[cgroup]++;\n }\n\n }\n\n }\n vector<int>result1=khansalgo(elements,count1);\n vector<int>result2=khansalgo(groups,count2);\n if(result1.empty() || result2.empty()){\n return {};\n }\n vector<int>result;\n unordered_map<int,vector<int>>f;\n for(int i=0;i<result1.size();i++){\n f[group[result1[i]]].push_back(result1[i]);\n }\n // for(int i=0;i<result2.size();i++){\n // for(auto it:f[i]){\n // result.push_back(it);\n // }\n // }\n for (int i = 0; i < result2.size(); i++) {\n int currentGroup = result2[i];\n if (f.find(currentGroup) != f.end()) { // Ensure the group exists in the map\n for (auto item : f[currentGroup]) {\n result.push_back(item);\n }\n }\n }\n return result;\n\n \n \n }\n};\n", "memory": "70187" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool cmp(vector<int>&a,vector<int>&b)\n {\n if(a[0]!=b[0])return a[0]<b[0];\n else return a[1]<b[1];\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int>graph[n];\n vector<int>ind(n);\n int groupId = m;\n for(int i=0;i<n;i++)\n {\n if(group[i]==-1)group[i] = groupId++;\n }\n for(int i=0;i<n;i++)\n {\n for(auto val:beforeItems[i])\n {\n graph[val].push_back(i);\n ind[i]++;\n }\n \n }\n queue<int>q;\n for(int i=0;i<n;i++)if(ind[i]==0)q.push(i);\n vector<int>elements;\n while(!q.empty())\n {\n auto f=q.front();\n q.pop();\n elements.push_back(f);\n for(auto val:graph[f])\n {\n ind[val]--;\n if(ind[val]==0)q.push(val);\n }\n }\n if(elements.size()<n)return {};\n\n\n map<int,vector<int>>groups;\n vector<int>in(groupId);\n set<pair<int,int>>st;\n for(int i=0;i<n;i++)\n {\n int x=group[i];\n for(auto val:beforeItems[i])\n {\n if(st.find({group[val],x})==st.end() && x!=group[val])\n {groups[group[val]].push_back(x);\n in[x]++;\n st.insert({group[val],x});\n }\n }\n \n }\n queue<int>pq;\n for(int i=0;i<groupId;i++)if(in[i]==0)pq.push(i);\n vector<int>grp;\n while(!pq.empty())\n {\n auto f=pq.front();\n pq.pop();\n grp.push_back(f);\n for(auto val:groups[f])\n {\n in[val]--;\n if(in[val]==0)\n {\n pq.push(val);\n }\n }\n }\n if(grp.size()<groupId)return {};\n unordered_map<int,int>index;\n for(int i=0;i<grp.size();i++)\n {\n index[grp[i]]=i;\n }\n vector<vector<int>>ans;\n for(int i=0;i<elements.size();i++)\n {\n vector<int>temp;\n temp.push_back(index[group[elements[i]]]);\n temp.push_back(i);\n temp.push_back(elements[i]);\n ans.push_back(temp);\n }\n sort(ans.begin(),ans.end(),cmp);\n vector<int>sol;\n for(auto val:ans)\n {\n sol.push_back(val[2]);\n }\n return sol;\n }\n};", "memory": "70187" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\n vector<int> topological_sort(const vector<vector<int>>& graph) {\n vector<int> vec;\n vector<int> visited(graph.size());\n int cnt = 1;\n\n auto dfs = [&](int v, int cnt, auto Dfs) -> bool {\n visited[v] = -cnt;\n for (int u : graph[v]) {\n if (visited[u] == -cnt) {\n return false;\n }\n if (!visited[u]) {\n if (!Dfs(u, cnt, Dfs)) {\n return false;\n }\n }\n }\n visited[v] = cnt;\n vec.emplace_back(v);\n return true;\n };\n\n for (int i = 0; i < graph.size(); i++) {\n if (!visited[i]) {\n if (!dfs(i, cnt, dfs)) {\n return {};\n }\n }\n cnt++;\n }\n\n reverse(vec.begin(), vec.end());\n return vec;\n }\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n group[i] = m++;\n }\n }\n\n vector<vector<int>> groups(m);\n for (int i = 0; i < n; i++) {\n groups[group[i]].emplace_back(i);\n }\n\n vector<unordered_set<int>> group_graph_st(m);\n vector<vector<int>> inner_graph(n);\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int element : beforeItems[i]) {\n if (group[element] != group[i]) {\n group_graph_st[group[element]].insert(group[i]);\n } else {\n inner_graph[element].emplace_back(i);\n }\n }\n }\n\n vector<vector<int>> group_graph;\n for (int i = 0; i < m; i++) {\n group_graph.emplace_back(group_graph_st[i].begin(), group_graph_st[i].end());\n }\n\n vector<int> group_topo = topological_sort(group_graph);\n cout << group_topo.size() << '\\n';\n if (group_topo.empty()) {\n return {};\n }\n\n auto build_graph = [&](int group) {\n unordered_map<int, int> idx_map;\n for (int i = 0; i < groups[group].size(); i++) {\n idx_map[groups[group][i]] = i;\n }\n\n vector<vector<int>> graph(groups[group].size());\n for (int i = 0; i < groups[group].size(); i++) {\n for (int v : inner_graph[groups[group][i]]) {\n graph[i].emplace_back(idx_map[v]);\n }\n }\n return graph;\n };\n\n vector<int> answer;\n for (int group : group_topo) {\n if (groups[group].empty()) {\n continue;\n }\n vector<vector<int>> temp_graph = build_graph(group);\n vector<int> inner_topo = topological_sort(temp_graph);\n if (inner_topo.empty()) {\n return {};\n }\n for (int v : inner_topo) {\n answer.emplace_back(groups[group][v]);\n }\n }\n\n return answer;\n }\n};", "memory": "71462" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\n vector<int> topological_sort(const vector<vector<int>>& graph) {\n vector<int> vec;\n vector<int> visited(graph.size());\n int cnt = 1;\n\n auto dfs = [&](int v, int cnt, auto Dfs) -> bool {\n visited[v] = -cnt;\n for (int u : graph[v]) {\n if (visited[u] == -cnt) {\n return false;\n }\n if (!visited[u]) {\n if (!Dfs(u, cnt, Dfs)) {\n return false;\n }\n }\n }\n visited[v] = cnt;\n vec.emplace_back(v);\n return true;\n };\n\n for (int i = 0; i < graph.size(); i++) {\n if (!visited[i]) {\n if (!dfs(i, cnt, dfs)) {\n return {};\n }\n }\n cnt++;\n }\n\n reverse(vec.begin(), vec.end());\n return vec;\n }\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n group[i] = m++;\n }\n }\n\n vector<vector<int>> groups(m);\n for (int i = 0; i < n; i++) {\n groups[group[i]].emplace_back(i);\n }\n\n vector<unordered_set<int>> group_graph_st(m);\n vector<vector<int>> inner_graph(n);\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int element : beforeItems[i]) {\n if (group[element] != group[i]) {\n group_graph_st[group[element]].insert(group[i]);\n } else {\n inner_graph[element].emplace_back(i);\n }\n }\n }\n\n vector<vector<int>> group_graph;\n for (int i = 0; i < m; i++) {\n group_graph.emplace_back(group_graph_st[i].begin(), group_graph_st[i].end());\n }\n\n vector<int> group_topo = topological_sort(group_graph);\n if (group_topo.empty()) {\n return {};\n }\n\n auto build_graph = [&](int group) {\n unordered_map<int, int> idx_map;\n for (int i = 0; i < groups[group].size(); i++) {\n idx_map[groups[group][i]] = i;\n }\n\n vector<vector<int>> graph(groups[group].size());\n for (int i = 0; i < groups[group].size(); i++) {\n for (int v : inner_graph[groups[group][i]]) {\n graph[i].emplace_back(idx_map[v]);\n }\n }\n return graph;\n };\n\n vector<int> answer;\n for (int group : group_topo) {\n if (groups[group].empty()) {\n continue;\n }\n vector<vector<int>> temp_graph = build_graph(group);\n vector<int> inner_topo = topological_sort(temp_graph);\n if (inner_topo.empty()) {\n return {};\n }\n for (int v : inner_topo) {\n answer.emplace_back(groups[group][v]);\n }\n }\n\n return answer;\n }\n};", "memory": "71462" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int, vector<int>> groups;\n int id = 30001;\n for (int i = 0; i < n; ++i) {\n if (group[i] == -1) {\n groups[id].push_back(i);\n group[i] = id;\n ++id;\n } else {\n groups[group[i]].push_back(i);\n }\n }\n\n // build graph and degrees\n vector<int> deg(n, 0);\n unordered_map<int, int> gdeg;\n vector<vector<int>> graph(n);\n for (int i = 0; i < n; ++i) {\n for (auto x: beforeItems[i]) {\n graph[x].push_back(i);\n ++deg[i];\n\n if (group[i] != group[x]) {\n ++gdeg[group[i]];\n }\n }\n }\n\n // top sort\n queue<int> gq;\n vector<int> ans;\n vector<int> seen(n, 0);\n\n // order group first\n for (auto &[k, v]: groups) {\n if (gdeg[k] == 0) {\n gq.push(k);\n }\n }\n\n while (!gq.empty()) {\n int g = gq.front();\n gq.pop();\n\n // order item in group\n queue<int> q;\n \n for (auto u: groups[g]) {\n if (deg[u] == 0) {\n q.push(u);\n }\n }\n\n int count = 0;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n seen[node] = 1;\n\n ans.push_back(node);\n ++count;\n \n for (auto &u: graph[node]) {\n --deg[u];\n\n if (group[u] != g) {\n --gdeg[group[u]];\n \n if (gdeg[group[u]] == 0) {\n gq.push(group[u]);\n }\n } else {\n if (seen[u] == 1) {\n return {};\n }\n\n if (deg[u] == 0) {\n q.push(u);\n }\n }\n }\n }\n\n if (count != groups[g].size()) {\n return {};\n }\n }\n\n if (ans.size() != n) {\n return {};\n }\n\n return ans;\n }\n};", "memory": "72737" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int, vector<int>> groups;\n int id = 30001;\n for (int i = 0; i < n; ++i) {\n if (group[i] == -1) {\n groups[id].push_back(i);\n group[i] = id;\n ++id;\n } else {\n groups[group[i]].push_back(i);\n }\n }\n\n // build graph and degrees\n vector<int> deg(n, 0);\n unordered_map<int, int> gdeg;\n vector<vector<int>> graph(n);\n for (int i = 0; i < n; ++i) {\n for (auto x: beforeItems[i]) {\n graph[x].push_back(i);\n ++deg[i];\n\n if (group[i] != group[x]) {\n ++gdeg[group[i]];\n }\n }\n }\n\n // top sort\n queue<int> gq;\n vector<int> ans;\n vector<int> seen(n, 0);\n\n // order group first\n for (auto &[k, v]: groups) {\n if (gdeg[k] == 0) {\n gq.push(k);\n }\n }\n\n while (!gq.empty()) {\n int g = gq.front();\n gq.pop();\n\n // order item in group\n queue<int> q;\n \n for (auto u: groups[g]) {\n if (deg[u] == 0) {\n q.push(u);\n }\n }\n\n int count = 0;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n seen[node] = 1;\n\n ans.push_back(node);\n ++count;\n \n for (auto &u: graph[node]) {\n --deg[u];\n\n if (group[u] != g) {\n --gdeg[group[u]];\n \n if (gdeg[group[u]] == 0) {\n gq.push(group[u]);\n }\n } else {\n if (seen[u] == 1) {\n return {};\n }\n\n if (deg[u] == 0) {\n q.push(u);\n }\n }\n }\n }\n\n if (count != groups[g].size()) {\n return {};\n }\n }\n\n if (ans.size() != n) {\n return {};\n }\n\n return ans;\n }\n};", "memory": "72737" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<int>topoSort(unordered_map<int,vector<int>>adj,vector<int>& indegree)\n{\nqueue<int>que;\nfor(int i=0;i<adj.size();i++)\n{\n if(indegree[i]==0)\n {\n que.push(i);\n }\n}\n vector<int>result;\n while(!que.empty())\n {\n int u=que.front();\n que.pop();\n result.push_back(u);\n for(int v:adj[u])\n {\n indegree[v]--;\n if(indegree[v]==0)\n {\n que.push(v);\n }\n }\n }\n return result.size()==adj.size()?result:vector<int>();\n}\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i=0;i<n;i++)\n {\n if(group[i]==-1)\n {\n group[i]=m++;\n }\n }\n unordered_map<int,vector<int>>itemGraph;\n vector<int>itemIndegree(n,0);\n for(int i=0;i<n;i++)\n {\n itemGraph[i]=vector<int>();\n }\n unordered_map<int,vector<int>>groupGraph;\n vector<int>groupIndegree(m,0);\n for(int i=0;i<m;i++)\n {\n groupGraph[i]=vector<int>();\n }\n\n for(int i=0;i<n;i++)\n {\n for(int prev:beforeItems[i])\n {\n itemGraph[prev].push_back(i);\n itemIndegree[i]++;\n if(group[prev]!=group[i])\n {\n int prevGroup=group[prev];\n int currGroup=group[i];\n groupGraph[prevGroup].push_back(currGroup);\n groupIndegree[currGroup]++;\n }\n }\n }\n vector<int>itemOrder=topoSort(itemGraph,itemIndegree);\n vector<int>GraphOrder=topoSort(groupGraph,groupIndegree);\n unordered_map<int,vector<int>>groupsToItemOrder;\n for(int& item:itemOrder)\n {\n int itemGroup=group[item];\n groupsToItemOrder[itemGroup].push_back(item);\n }\n vector<int>result;\n for(int& group:GraphOrder)\n {\n result.insert(result.end(),groupsToItemOrder[group].begin(),groupsToItemOrder[group].end());\n }\n return result;\n }\n};", "memory": "74012" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n bool topoSortUtil(vector<int>& topoOrder, unordered_map<int,int>& indeg, unordered_map<int,vector<int>>& graph){\n priority_queue<int,vector<int>, greater<int>> minHeap;\n for(auto &i: indeg) if(!i.second) minHeap.push(i.first);\n int cnt=0;\n while(!minHeap.empty()){\n int tp=minHeap.top(); minHeap.pop();\n topoOrder.push_back(tp);\n cnt++;\n for(auto &u: graph[tp]){\n if(!--indeg[u]) minHeap.push(u);\n }\n }\n return cnt == graph.size();\n }\n\n bool topoSort(vector<int>& topoOrder, unordered_map<int,vector<int>>& graph){\n int n=graph.size();\n unordered_map<int,int> indeg;\n for(auto &i: graph){\n if(!indeg.count(i.first)) indeg[i.first]=0;\n for(auto &j: i.second) indeg[j]++;\n }\n return topoSortUtil(topoOrder,indeg,graph);\n }\n\n vector<int> sortItems(int n, int m, vector<int>& g, vector<vector<int>>& dep) {\n unordered_map<int,unordered_map<int,vector<int>>> groupAndItsGraph;\n for(int i=0;i<n;++i){\n if(g[i]==-1) g[i]=m++;\n groupAndItsGraph[g[i]][i]={};\n }\n unordered_map<int,vector<int>> groupGraph;\n for(int i=0;i<m;++i) groupGraph[i]={};\n for(int i=0;i<n;++i){\n for(auto &j: dep[i]){\n if(g[i]==g[j]) groupAndItsGraph[g[j]][j].push_back(i);\n else groupGraph[g[j]].push_back(g[i]);\n }\n }\n vector<int> groupTopoOrder, nodesTopoOrder;\n if(!topoSort(groupTopoOrder, groupGraph)) return {};\n for(auto &i: groupTopoOrder) if(!topoSort(nodesTopoOrder, groupAndItsGraph[i])) return {};\n return nodesTopoOrder;\n } \n};", "memory": "75287" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int,int>inner_group;\n unordered_map<int,vector<int>>outter_group;\nvector<int> inner_item(n);\nvector<vector<int>> outter_item(n);\nunordered_map<int,vector<int>> all;\n for(int i=0;i<n;i++){\n for(auto&num:beforeItems[i]){\n if(group[i] != group[num]){\n int g1 = (group[i] ==-1)?-i-1:group[i];\n int g2 = (group[num] ==-1)?-num-1:group[num];\n inner_group[g1]++;\n outter_group[g2].push_back(g1);\n \n }\n else {\n if(group[i]==-1){\n int g1 = -i-1;\n int g2 = -num-1;\n inner_group[g1]++;\n outter_group[g2].push_back(g1);\n }\n else{\n inner_item[i]++;\n outter_item[num].push_back(i);\n }\n }\n \n }\n int g = (group[i] ==-1)?-i-1:group[i];\n all[g].push_back(i);\n \n }\n vector<int> ans;\n ans.reserve(n);\n queue<int> group_q;\n for(auto&v:all){\n if(inner_group[v.first]==0)group_q.push(v.first);\n }\n while(!group_q.empty()){\n int curr_group=group_q.front();\n group_q.pop();\n queue<int> item_q;\n for(auto&num:all[curr_group]){\n if(inner_item[num]==0)item_q.push(num);\n }\n while(!item_q.empty()){\n int curr_item = item_q.front();\n item_q.pop();\n ans.push_back(curr_item);\n for(auto&num:outter_item[curr_item]){\n inner_item[num]--;\n if(inner_item[num]==0){\n item_q.push(num);\n }\n }\n \n }\n for(auto&g:outter_group[curr_group]){\n inner_group[g]--;\n if(inner_group[g]==0){\n group_q.push(g);\n }\n }\n \n }\n return ans.size() == n ? ans : vector<int>{};\n \n }\n};\n", "memory": "76562" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int idx = m;\n vector<vector<int>>groupItems(m);\n for(int i=0;i<n;i++) {\n if(group[i] == -1) {\n groupItems.push_back({});\n group[i] = idx++;\n }\n groupItems[group[i]].push_back(i);\n }\n\n int totalGrps = idx;\n vector<vector<int>>groupGraph(totalGrps);\n vector<int>groupDegree(totalGrps);\n vector<vector<int>>itemGraph(n);\n vector<int>itemDegree(n);\n for(int i=0;i<n;i++) {\n for(auto j:beforeItems[i]) {\n if(group[i] == group[j]) {\n itemGraph[j].push_back(i);\n itemDegree[i]++;\n }\n else {\n groupGraph[group[j]].push_back(group[i]);\n groupDegree[group[i]]++;\n }\n }\n }\n vector<int>groupIdx(totalGrps);\n iota(groupIdx.begin(), groupIdx.end(), 0);\n auto topoSort = [&](vector<vector<int>>&graph, vector<int>&degree, vector<int>&indexes) {\n vector<int>order;\n queue<int>q;\n for(int i:indexes) {\n if(degree[i] == 0) q.push(i);\n }\n\n while(!q.empty()) {\n int front = q.front();\n q.pop();\n order.push_back(front);\n for(int next:graph[front]) {\n if(--degree[next] == 0) {\n q.push(next);\n }\n }\n }\n return order.size() == indexes.size() ? order : vector<int>();\n };\n\n vector<int>grpOrder = topoSort(groupGraph, groupDegree, groupIdx);\n if(grpOrder.empty()) return {};\n\n vector<int>ans;\n vector<int>items(n);\n for(auto grp:grpOrder) {\n items = groupItems[grp];\n if(items.empty()) continue;\n vector<int>itemOrder = topoSort(itemGraph, itemDegree, items);\n if(itemOrder.empty()) return {};\n ans.insert(ans.end(), itemOrder.begin(), itemOrder.end());\n }\n return ans;\n }\n};", "memory": "77837" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& G, vector<vector<int>>& B) {\n vector<unordered_set<int>> A(n + 2 * m);\n vector<int> D(n + 2 * m), v;\n stack<int> sk;\n for(int i = 0; i < n; ++i)\n {\n if(G[i] != -1)\n {\n int g1 = n + G[i], g2 = n + m + G[i];\n A[g1].insert(i);\n ++D[i];\n A[i].insert(g2);\n ++D[g2];\n }\n for(int j : B[i])\n {\n int b = G[j] == -1 || G[j] == G[i] ? j : (n + m + G[j]);\n int a = G[i] == -1 || G[j] == G[i] ? i : (n + G[i]);\n if(A[b].insert(a).second)\n ++D[a];\n }\n }\n for(int i = 0; i < D.size(); ++i)\n if(!D[i])\n sk.push(i);\n while(!sk.empty())\n {\n int i = sk.top();\n sk.pop();\n if(i >= 0 && i < n)\n v.push_back(i);\n for(int j : A[i])\n if(--D[j] == 0)\n sk.push(j);\n }\n return v.size() == n ? v : vector<int>{};\n }\n};", "memory": "77837" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool topSort(vector<unordered_set<int>>& al, int i, vector<int>& res, vector<int>& stat) {\n if (stat[i] != 0) return stat[i] == 2;\n stat[i] = 1;\n for (auto n : al[i])\n if (!topSort(al, n, res, stat)) return false;\n stat[i] = 2;\n res.push_back(i);\n return true;\n}\nvector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> res_tmp, res(n), stat(n + 2 * m);\n vector<unordered_set<int>> al(n + 2 * m);\n for (auto i = 0; i < n; ++i) {\n if (group[i] != -1) {\n al[n + group[i]].insert(i);\n al[i].insert(n + m + group[i]);\n }\n for (auto j : beforeItems[i]) {\n if (group[i] != -1 && group[i] == group[j]) al[j].insert(i);\n else {\n auto ig = group[i] == -1 ? i : n + group[i];\n auto jg = group[j] == -1 ? j : n + m + group[j];\n al[jg].insert(ig);\n }\n }\n }\n for (int n = al.size() - 1; n >= 0; --n)\n if (!topSort(al, n, res_tmp, stat)) return {};\n reverse(begin(res_tmp), end(res_tmp));\n copy_if(begin(res_tmp), end(res_tmp), res.begin(), [&](int i) {return i < n; });\n return res;\n}\n};", "memory": "79112" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool topSort(vector<unordered_set<int>>& al, int i, vector<int>& res, vector<int>& stat) {\n if (stat[i] != 0) return stat[i] == 2;\n stat[i] = 1;\n for (auto n : al[i])\n if (!topSort(al, n, res, stat)) return false;\n stat[i] = 2;\n res.push_back(i);\n return true;\n}\nvector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> res_tmp, res(n), stat(n + 2 * m);\n vector<unordered_set<int>> al(n + 2 * m);\n for (auto i = 0; i < n; ++i) {\n if (group[i] != -1) {\n al[n + group[i]].insert(i);\n al[i].insert(n + m + group[i]);\n }\n for (auto j : beforeItems[i]) {\n if (group[i] != -1 && group[i] == group[j]) al[j].insert(i);\n else {\n auto ig = group[i] == -1 ? i : n + group[i];\n auto jg = group[j] == -1 ? j : n + m + group[j];\n al[jg].insert(ig);\n }\n }\n }\n for (int n = al.size() - 1; n >= 0; --n)\n if (!topSort(al, n, res_tmp, stat)) return {};\n reverse(begin(res_tmp), end(res_tmp));\n copy_if(begin(res_tmp), end(res_tmp), res.begin(), [&](int i) {return i < n; });\n return res;\n}\n};", "memory": "79112" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> ans;\n vector<vector<int>> groups(m + 1 );\n vector<vector<int>> graph(n);\n vector<int> items_dep(n, 0);\n vector<int> group_dep(m + 1, 0);\n \n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n groups[m].push_back(i);\n } else {\n groups[group[i]].push_back(i);\n }\n }\n \n for (int i = 0; i < beforeItems.size(); i++) {\n items_dep[i] = beforeItems[i].size();\n for (int j = 0; j < beforeItems[i].size(); j++) {\n graph[beforeItems[i][j]].push_back(i);\n if (group[i] != -1 && group[i] != group[beforeItems[i][j]]) {\n group_dep[group[i]]++;\n }\n }\n }\n \n queue<int> no_group_val;\n queue<int> valid_groups;\n \n for (int i = 0; i < n; i++) {\n if (group[i] == -1 && items_dep[i] == 0) {\n no_group_val.push(i);\n }\n }\n for (int i = 0; i < m; i++) {\n if (group_dep[i] == 0) {\n valid_groups.push(i);\n }\n }\n \n while (!valid_groups.empty() || !no_group_val.empty()) {\n queue<int> group_items;\n \n if (!valid_groups.empty()) {\n int curr_group = valid_groups.front();\n valid_groups.pop();\n for (int item : groups[curr_group]) {\n if (items_dep[item] == 0) {\n group_items.push(item);\n }\n }\n }\n \n while (!group_items.empty() || !no_group_val.empty()) {\n int curr_item;\n \n if (!group_items.empty()) {\n curr_item = group_items.front();\n group_items.pop();\n } else {\n curr_item = no_group_val.front();\n no_group_val.pop();\n }\n \n ans.push_back(curr_item);\n \n for (int dep_item : graph[curr_item]) {\n if (--items_dep[dep_item] == 0) {\n if (group[dep_item] == group[curr_item]) {\n group_items.push(dep_item);\n } else if (group[dep_item] == -1) {\n no_group_val.push(dep_item);\n } else {\n if (--group_dep[group[dep_item]] == 0) {\n valid_groups.push(group[dep_item]);\n }\n }\n }\n else if( group[dep_item] != group[curr_item] && group[ dep_item ] != -1 )\n if (--group_dep[group[dep_item]] == 0) {\n valid_groups.push(group[dep_item]);\n } \n }\n }\n }\n \n if (ans.size() == n) {\n return ans;\n }\n return {};\n }\n};\n\n", "memory": "80387" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> ans;\n vector<vector<int>> groups(m );\n vector<vector<int>> graph(n);\n vector<int> items_dep(n, 0);\n vector<int> group_dep(m , 0);\n \n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) {\n } else {\n groups[group[i]].push_back(i);\n }\n }\n \n for (int i = 0; i < beforeItems.size(); i++) {\n items_dep[i] = beforeItems[i].size();\n for (int j = 0; j < beforeItems[i].size(); j++) {\n graph[beforeItems[i][j]].push_back(i);\n if (group[i] != -1 && group[i] != group[beforeItems[i][j]]) {\n group_dep[group[i]]++;\n }\n }\n }\n \n queue<int> no_group_val;\n queue<int> valid_groups;\n \n for (int i = 0; i < n; i++) {\n if (group[i] == -1 && items_dep[i] == 0) {\n no_group_val.push(i);\n }\n }\n for (int i = 0; i < m; i++) {\n if (group_dep[i] == 0) {\n valid_groups.push(i);\n }\n }\n \n while (!valid_groups.empty() || !no_group_val.empty()) {\n queue<int> group_items;\n \n if (!valid_groups.empty()) {\n int curr_group = valid_groups.front();\n valid_groups.pop();\n for (int item : groups[curr_group]) {\n if (items_dep[item] == 0) {\n group_items.push(item);\n }\n }\n }\n \n while (!group_items.empty() || !no_group_val.empty()) {\n int curr_item;\n \n if (!group_items.empty()) {\n curr_item = group_items.front();\n group_items.pop();\n } else {\n curr_item = no_group_val.front();\n no_group_val.pop();\n }\n \n ans.push_back(curr_item);\n \n for (int dep_item : graph[curr_item]) {\n if (--items_dep[dep_item] == 0) {\n if (group[dep_item] == group[curr_item]) {\n group_items.push(dep_item);\n } else if (group[dep_item] == -1) {\n no_group_val.push(dep_item);\n } else {\n if (--group_dep[group[dep_item]] == 0) {\n valid_groups.push(group[dep_item]);\n }\n }\n }\n else if( group[dep_item] != group[curr_item] && group[ dep_item ] != -1 )\n if (--group_dep[group[dep_item]] == 0) {\n valid_groups.push(group[dep_item]);\n } \n }\n }\n }\n \n if (ans.size() == n) {\n return ans;\n }\n return {};\n }\n};\n\n", "memory": "80387" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& groups, vector<vector<int>>& beforeItems) {\n unordered_map<int,int> groups_indegree;\n unordered_map<int,vector<int>> groups_graph;\n unordered_map<int,int> num_items_in_groups;\n unordered_map<int,vector<int>> group_to_item_id;\n unordered_map<int,int> item_indegree;\n for(int i = 0; i < n; ++i) {\n // Initialize groups indegree to 0.\n groups_indegree[groups[i]] = 0;\n // Populate how many items are in each group\n ++num_items_in_groups[groups[i]];\n // initialize groups_to_item_id & groups_graph\n group_to_item_id[groups[i]] = vector<int>();\n groups_graph[groups[i]] = vector<int>();\n item_indegree[i] = 0;\n }\n vector<int> item_graph[n];\n for(int item_id = 0; item_id < n; ++item_id) {\n int& cur_group = groups[item_id];\n // Add this item to the groups_to_item_id \n group_to_item_id[cur_group].push_back(item_id);\n for(int& required_item : beforeItems[item_id]) {\n // Add a directed edge from required_item to item_id\n item_graph[required_item].push_back(item_id);\n // Add an indegree to item_id\n ++item_indegree[item_id];\n\n int required_item_group = groups[required_item];\n if(required_item_group != cur_group) {\n // Add a directed edge from the required_group to the cur_group\n groups_graph[required_item_group].push_back(cur_group);\n // Add an indegree to the cur_group\n ++groups_indegree[cur_group];\n }\n }\n }\n \n vector<int> res(n);\n int res_idx = 0;\n bool pushed = false;\n\n queue<int> group_q;\n for(auto& e : groups_indegree) {\n if(e.second == 0) {\n // Add groups with no indegrees into the queue first.\n group_q.push(e.first);\n }\n }\n\n if(group_q.empty()) {\n group_q.push(-1);\n }\n\n\n while(!group_q.empty()) {\n int cur_group = group_q.front();\n group_q.pop();\n // Top sort every item in this group first.\n queue<int> item_q;\n for(int& item : group_to_item_id[cur_group]) {\n if(item_indegree[item] == 0) {\n item_q.push(item);\n --item_indegree[item];\n }\n }\n\n while(!item_q.empty()) {\n int cur_item = item_q.front();\n item_q.pop();\n\n res[res_idx] = cur_item;\n ++res_idx;\n --num_items_in_groups[cur_group];\n for(int& next_item : item_graph[cur_item]) {\n --item_indegree[next_item];\n if(item_indegree[next_item] == 0 && groups[next_item] == cur_group) {\n item_q.push(next_item);\n }\n }\n }\n\n if(cur_group != -1 && num_items_in_groups[cur_group] > 0) {\n return {};\n }\n\n for(int& next_group : groups_graph[cur_group]) {\n --groups_indegree[next_group];\n if(groups_indegree[next_group] == 0) {\n group_q.push(next_group);\n }\n }\n if(group_q.empty() && !pushed) {\n group_q.push(-1);\n pushed = true;\n }\n }\n if(res_idx == n) return res;\n return {};\n }\n};", "memory": "85487" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& groups, vector<vector<int>>& beforeItems) {\n unordered_map<int,int> groups_indegree;\n unordered_map<int,vector<int>> groups_graph;\n unordered_map<int,int> num_items_in_groups;\n unordered_map<int,vector<int>> group_to_item_id;\n unordered_map<int,int> item_indegree;\n for(int i = 0; i < n; ++i) {\n // Initialize groups indegree to 0.\n groups_indegree[groups[i]] = 0;\n // Populate how many items are in each group\n ++num_items_in_groups[groups[i]];\n // initialize groups_to_item_id & groups_graph\n group_to_item_id[groups[i]] = vector<int>();\n groups_graph[groups[i]] = vector<int>();\n item_indegree[i] = 0;\n }\n vector<int> item_graph[n];\n for(int item_id = 0; item_id < n; ++item_id) {\n int& cur_group = groups[item_id];\n // Add this item to the groups_to_item_id \n group_to_item_id[cur_group].push_back(item_id);\n for(int& required_item : beforeItems[item_id]) {\n // Add a directed edge from required_item to item_id\n item_graph[required_item].push_back(item_id);\n // Add an indegree to item_id\n ++item_indegree[item_id];\n\n int required_item_group = groups[required_item];\n if(required_item_group != cur_group) {\n // Add a directed edge from the required_group to the cur_group\n groups_graph[required_item_group].push_back(cur_group);\n // Add an indegree to the cur_group\n ++groups_indegree[cur_group];\n }\n }\n }\n \n vector<int> res(n);\n int res_idx = 0;\n bool pushed = false;\n\n queue<int> group_q;\n for(auto& e : groups_indegree) {\n if(e.second == 0) {\n // Add groups with no indegrees into the queue first.\n group_q.push(e.first);\n }\n }\n\n if(group_q.empty()) {\n group_q.push(-1);\n }\n\n\n while(!group_q.empty()) {\n int cur_group = group_q.front();\n group_q.pop();\n // Top sort every item in this group first.\n queue<int> item_q;\n for(int& item : group_to_item_id[cur_group]) {\n if(item_indegree[item] == 0) {\n item_q.push(item);\n --item_indegree[item];\n }\n }\n\n while(!item_q.empty()) {\n int cur_item = item_q.front();\n item_q.pop();\n\n res[res_idx] = cur_item;\n ++res_idx;\n --num_items_in_groups[cur_group];\n for(int& next_item : item_graph[cur_item]) {\n --item_indegree[next_item];\n if(item_indegree[next_item] == 0 && groups[next_item] == cur_group) {\n item_q.push(next_item);\n }\n }\n }\n\n if(cur_group != -1 && num_items_in_groups[cur_group] > 0) {\n return {};\n }\n\n for(int& next_group : groups_graph[cur_group]) {\n --groups_indegree[next_group];\n if(groups_indegree[next_group] == 0) {\n group_q.push(next_group);\n }\n }\n if(group_q.empty() && !pushed) {\n group_q.push(-1);\n pushed = true;\n }\n }\n if(res_idx == n) return res;\n return {};\n }\n};", "memory": "85487" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int flag=0;\n vector<int> fin_ans;\n int d1[100010]={0};\n int d2[100010]={0};\n void toposort(map<int,vector<int>> & grp,int g,vector<vector<int>> &adj_e){\n queue<int> q;\n for(auto x:grp[g]){\n if(d1[x]==0) q.push(x);\n }\n while(q.size()){\n auto x=q.front();q.pop();\n fin_ans.push_back(x);\n for(auto y:adj_e[x]){\n d1[y]--;\n if(d1[y]==0) q.push(y);\n }\n }\n for(auto x:grp[g]){\n if(d1[x]!=0) flag=1;\n }\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int x=m;\n map<int,vector<int>> grp;\n for(int i=0;i<n;i++){\n if(group[i]==-1) {group[i]=x;x++;}\n grp[group[i]].push_back(i);\n }\n \n vector<vector<int>> adj_e(n);\n vector<vector<int>> adj_g(x);\n //for(int i=0;i<m-1;i++) {adj_g[i].push_back(i+1);d2[i+1]++;}\n for(int i=0;i<n;i++){\n for(auto to:beforeItems[i]){\n if(group[i]==group[to]){\n adj_e[to].push_back(i);\n d1[i]++;\n }\n else{\n adj_g[group[to]].push_back(group[i]);\n d2[group[i]]++;\n }\n }\n }\n\n queue<int> q;\n vector<int> temp_ans;\n for(int i=0;i<x;i++) if(d2[i]==0) q.push(i);\n while(q.size()){\n int g=q.front();q.pop();\n //cout<<g<<endl;\n toposort(grp,g,adj_e);\n if(flag) return temp_ans;\n for(auto y:adj_g[g]){\n d2[y]--;\n if(d2[y]==0) q.push(y);\n }\n\n }\n for(int i=0;i<x;i++) if(d2[i]!=0){return temp_ans;}\n return fin_ans;\n }\n};", "memory": "86762" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> nodeIndegree(n);\n vector<int> adj[n];\n unordered_map<int, int> groupIndegree;\n unordered_map<int, unordered_set<int>> groupAdj;\n unordered_map<int, vector<int>> groupToNode;\n for(int i = 0; i < group.size(); ++i) {\n if(group[i] == -1)\n group[i] = m++;\n groupToNode[group[i]].push_back(i);\n groupIndegree[group[i]] = 0;\n }\n for(int i = 0; i < beforeItems.size(); ++i) {\n int currentGroup = group[i];\n for(int j = 0; j < beforeItems[i].size(); ++j) {\n int dependentGroup = group[beforeItems[i][j]];\n if(currentGroup != dependentGroup && groupAdj[dependentGroup].count(currentGroup) == 0) {\n groupAdj[dependentGroup].insert(currentGroup);\n groupIndegree[currentGroup]++;\n }\n adj[beforeItems[i][j]].push_back(i);\n nodeIndegree[i]++;\n }\n }\n queue<int> groupQueue;\n for(auto x: groupIndegree) {\n cout << x.first << \" \" << x.second << endl;\n if(x.second == 0) {\n // cout << x.first << endl;\n groupQueue.push(x.first);\n }\n }\n vector<int> ans;\n while(!groupQueue.empty()) {\n int currentGroup = groupQueue.front(); groupQueue.pop();\n cout << \"currentGroup => \" << currentGroup << endl;\n for(int childGroup: groupAdj[currentGroup]) {\n groupIndegree[childGroup] -= 1;\n if(groupIndegree[childGroup] == 0) {\n groupQueue.push(childGroup);\n }\n }\n queue<int> nodeQueue;\n vector<int> nodes = groupToNode[currentGroup];\n for(int i = 0; i < nodes.size(); ++i) {\n if(nodeIndegree[nodes[i]] == 0) {\n nodeQueue.push(nodes[i]);\n }\n }\n while(!nodeQueue.empty()) {\n int node = nodeQueue.front(); nodeQueue.pop();\n cout << \"node => \" << node << endl;\n ans.push_back(node);\n for(int childNode: adj[node]) {\n cout << \"childNode => \" << childNode << endl;\n nodeIndegree[childNode] -= 1;\n if(nodeIndegree[childNode] == 0 && group[childNode] == currentGroup) {\n cout << \"i am on dependency zero => \" << childNode << endl;\n nodeQueue.push(childNode);\n }\n // cout << \"childNode => \" << childNode << endl;\n } \n }\n }\n for(int i = 0; i < nodeIndegree.size(); ++i) {\n if(nodeIndegree[i] != 0)\n return {};\n }\n return ans;\n }\n};", "memory": "86762" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> adj[100005], ele[100005];\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<int> in(n+2,0), vis(n+2,0);\n\n for(int i = 0; i<n; i++){\n if(group[i] >= 0){\n ele[group[i]].push_back(i);\n }\n }\n\n for(int i = 0; i<beforeItems.size(); i++){\n for(int j = 0; j<beforeItems[i].size(); j++){\n if(group[i] >= 0 && group[i] == group[beforeItems[i][j]]){\n adj[beforeItems[i][j]].push_back(i);\n in[i]++;\n }\n }\n }\n\n vector<int> ts;\n for(int i = 0; i<m; i++){\n queue<int> q;\n for(int j = 0; j<ele[i].size(); j++){\n if(in[ele[i][j]] == 0)\n q.push(ele[i][j]);\n }\n while(!q.empty()){\n int x = q.front(); q.pop();\n vis[x]=1;\n ts.push_back(x);\n for(auto it : adj[x]){\n in[it]--;\n if(!vis[it] && !in[it])\n q.push(it);\n }\n }\n if(!q.empty() || ts.size() != ele[i].size())\n return {};\n ele[i].clear();\n ele[i] = ts;\n ts.clear();\n }\n\n vector<pair<int,int>> groupAdj[100005];\n vector<pair<int,int>> regularAdj[100005];\n vector<int> inGroup(m+2,0), visGroup(m+2,0), visRegular(n+2,0), inRegular(n+2,0);\n for(int i = 0; i<beforeItems.size(); i++){\n for(int j = 0; j<beforeItems[i].size(); j++){\n if(group[beforeItems[i][j]] == -1){\n if(group[i] >= 0)\n inGroup[group[i]]++, regularAdj[beforeItems[i][j]].push_back({group[i],0});\n else inRegular[i]++, regularAdj[beforeItems[i][j]].push_back({i,1});\n }\n else if(group[i] != group[beforeItems[i][j]]){\n if(group[i] >= 0){\n groupAdj[group[beforeItems[i][j]]].push_back({group[i], 0});\n inGroup[group[i]]++;\n } else {\n inRegular[i]++, groupAdj[group[beforeItems[i][j]]].push_back({i, 1});\n }\n }\n }\n }\n\n queue<pair<int,int>> q;\n for(int i = 0; i<m; i++){\n if(!inGroup[i])\n q.push({i,0});\n }\n for(int i = 0; i<n; i++){\n if(group[i] == -1 && !inRegular[i])\n q.push({i,1});\n }\n while(!q.empty()){\n auto [node, type] = q.front(); q.pop();\n if(type == 0){\n visGroup[node]=1;\n for(auto it : ele[node])\n ts.push_back(it);\n for(auto it : groupAdj[node]){\n if(it.second == 0){\n inGroup[it.first]--;\n if(inGroup[it.first] == 0 && !visGroup[it.first]){\n q.push({it.first, 0});\n }\n } else {\n inRegular[it.first]--;\n if(inRegular[it.first] == 0 && !visRegular[it.first]){\n q.push({it.first, 1});\n }\n }\n }\n } else {\n visRegular[node]=1;\n ts.push_back(node);\n for(auto it : regularAdj[node]){\n if(it.second == 0){\n inGroup[it.first]--;\n if(inGroup[it.first] == 0 && !visGroup[it.first]){\n q.push({it.first, 0});\n }\n } else {\n inRegular[it.first]--;\n if(inRegular[it.first] == 0 && !visRegular[it.first]){\n q.push({it.first, 1});\n }\n }\n }\n }\n }\n if(ts.size() != n)\n return {};\n return ts;\n }\n};", "memory": "88037" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n map<int, vector<int>> grp_mem;\n vector<vector<int>> sub_ord;\n\n void topo_sort(int grp_ind, vector<vector<int>>& bfr, vector<vector<int>>& sorted_grp, vector<int>& in_degree) {\n queue<int> q;\n for (auto mem : grp_mem[grp_ind]) {\n if (in_degree[mem] == 0) {\n q.push(mem);\n }\n }\n while (!q.empty()) {\n int now = q.front();\n sorted_grp[grp_ind].push_back(now);\n q.pop();\n for (auto it : sub_ord[now]) {\n in_degree[it]--;\n if (in_degree[it] == 0) q.push(it);\n }\n }\n }\n\n void topo_sort_grp(int m1, vector<int>& in_degree, vector<int>& ans) {\n queue<int> q;\n for (int i = 0; i < m1; i++) {\n if (in_degree[i] == 0) {\n q.push(i);\n }\n }\n while (!q.empty()) {\n int now = q.front();\n ans.push_back(now);\n q.pop();\n for (auto it : sub_ord[now]) {\n in_degree[it]--;\n if (in_degree[it] == 0) q.push(it);\n }\n }\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int m1 = m;\n for (int i = 0; i < n; i++) {\n if (group[i] == -1) {\n group[i] = m1;\n m1++;\n }\n grp_mem[group[i]].push_back(i);\n }\n\n vector<vector<int>> sorted_grp(m1, vector<int>());\n vector<int> in_degree(n, 0);\n sub_ord.assign(n, vector<int>());\n for (int i = 0; i < n; i++) {\n for (auto it : beforeItems[i]) {\n if (group[i] == group[it]) {\n sub_ord[it].push_back(i);\n in_degree[i]++;\n }\n }\n }\n\n for (int i = 0; i < m1; i++) {\n topo_sort(i, beforeItems, sorted_grp, in_degree);\n if (sorted_grp[i].size() != grp_mem[i].size()) {\n return vector<int>();\n }\n }\n\n sub_ord.clear();\n in_degree.clear();\n sub_ord.assign(m1, vector<int>());\n in_degree.assign(m1, 0);\n vector<int> vis(n, 0);\n\n for (int i = 0; i < n; i++) {\n for (auto it : beforeItems[i]) {\n if (group[i] != group[it]) {\n sub_ord[group[it]].push_back(group[i]);\n in_degree[group[i]]++;\n }\n }\n }\n\n vector<int> ans;\n topo_sort_grp(m1, in_degree, ans);\n\n if (ans.size() != m1) {\n return vector<int>();\n }\n\n vector<int> res;\n for (int i = 0; i < ans.size(); i++) {\n for (auto it : sorted_grp[ans[i]]) {\n res.push_back(it);\n }\n }\n\n return res;\n }\n};\n", "memory": "89312" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void kaln( int node , map< int, map<int,vector<int>> > &gig , vector<int> &ans ){\n map<int,vector<int>> mp = gig[node];\n map<int,int> indegree;\n for( auto v : mp ){\n for( auto i : v.second ){\n indegree[i]++;\n }\n }\n queue<int> q;\n for( auto v : mp ){\n if( indegree.find(v.first) == indegree.end() ) {\n \n q.push(v.first);\n }\n }\n while(!q.empty()){\n int x = q.front();\n q.pop();\n ans.push_back(x);\n for(auto child : mp[x]){\n indegree[child]--;\n if( indegree[child] == 0 ) q.push(child);\n }\n }\n return;\n }\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n\n vector<int> grp(n);\n for(int i=0;i<n;i++){\n if( group[i] != -1 ) grp[i] = n + group[i];\n else grp[i] = i;\n }\n \n // relation b/w groups.\n map<int,vector<int>> gg;\n map<int, map<int,vector<int>> > gig;\n for( auto child : grp ){\n gig.insert({child,{}});\n }\n for(int i=0;i<n;i++){\n gig[grp[i]].insert({i,{}});\n }\n for(int i=0;i<n;i++){\n for( auto child : beforeItems[i] ){\n if( grp[i] != grp[child] ){\n gg[grp[child]].push_back(grp[i]);\n }\n else{\n gig[grp[child]][child].push_back(i);\n }\n }\n }\n for( auto child : grp ){\n if( gg.find(child) == gg.end() ){\n gg[child] = {};\n }\n }\n // map is ready , gtog bhi and insideg bhi.\n\n map<int,int> indegree;\n for( auto v : gg ){\n for( auto i : v.second ){\n indegree[i]++;\n }\n }\n queue<int> q;\n for( auto v : gg ){\n if( indegree.find(v.first) == indegree.end() ) {\n q.push(v.first);\n }\n }\n \n vector<int> ans;\n \n while(!q.empty()){\n int x = q.front();\n q.pop();\n // cout<<x<<endl;\n kaln(x,gig,ans);\n for( auto child : gg[x] ){\n \n indegree[child]--;\n if(indegree[child] == 0) q.push(child);\n \n }\n }\n if( ans.size() != n ) return {};\n return ans;\n }\n};", "memory": "90587" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\n vector<int> getTopoSort(vector<unordered_set<int>> &adj,vector<int> &nodes,vector<int> &indeg){\n int n=nodes.size();\n vector<int> topo;\n // unordered_map<int,int> vis;\n queue<int> q;\n for(auto it:nodes){\n if(indeg[it]==0){\n q.push(it);\n }\n }\n while(!q.empty()){\n int node=q.front();\n q.pop();\n topo.push_back(node);\n for(auto it:adj[node]){\n indeg[it]--;\n if(indeg[it]==0){\n q.push(it);\n }\n }\n }\n // for(auto it:topo){\n // cout<<it<<\" \";\n // }\n // cout<<endl;\n if(topo.size()<n){\n return {};\n }\n return topo;\n }\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for(int i=0;i<n;i++){\n if(group[i]==-1){\n group[i]=m++;\n }\n }\n vector<vector<int>> groups(m);\n for(int i=0;i<n;i++){\n groups[group[i]].push_back(i);\n }\n vector<unordered_set<int>> adj1(n),adj2(m);\n vector<int> indeg1(n),indeg2(m);\n for(int i=0;i<n;i++){\n for(auto it:beforeItems[i]){\n if(group[it]==group[i]){\n adj1[it].insert(i);\n indeg1[i]++;\n }\n else if(adj2[group[it]].count(group[i])==0){\n adj2[group[it]].insert(group[i]);\n indeg2[group[i]]++;\n }\n }\n }\n // for(auto it:indeg2){\n // cout<<it<<\" \";\n // }\n // cout<<endl;\n vector<int> dummy(m);\n for(int i=0;i<m;i++){\n dummy[i]=i;\n }\n vector<int> groupTopo=getTopoSort(adj2,dummy,indeg2);\n if(groupTopo.empty()){\n return {};\n }\n // cout<<\"grouped\"<<endl;\n vector<int> ans;\n for(auto it:groupTopo){\n vector<int> topo=getTopoSort(adj1,groups[it],indeg1);\n if(topo.empty() && groups[it].size()!=0){\n return {};\n }\n for(auto x:topo){\n ans.push_back(x);\n }\n }\n return ans;\n }\n};", "memory": "91862" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& G, vector<vector<int>>& B) {\n unordered_map<int, unordered_set<int>> A;\n vector<int> D(n + 2 * m), v;\n stack<int> sk;\n for(int i = 0; i < n; ++i)\n {\n if(G[i] != -1)\n {\n int g1 = n + G[i], g2 = n + m + G[i];\n A[g1].insert(i);\n ++D[i];\n A[i].insert(g2);\n ++D[g2];\n }\n for(int j : B[i])\n {\n int b = G[j] == -1 || G[j] == G[i] ? j : (n + m + G[j]);\n int a = G[i] == -1 || G[j] == G[i] ? i : (n + G[i]);\n if(A[b].insert(a).second)\n ++D[a];\n }\n }\n for(int i = 0; i < D.size(); ++i)\n if(!D[i])\n sk.push(i);\n while(!sk.empty())\n {\n int i = sk.top();\n sk.pop();\n if(i >= 0 && i < n)\n v.push_back(i);\n for(int j : A[i])\n if(--D[j] == 0)\n sk.push(j);\n }\n return v.size() == n ? v : vector<int>{};\n }\n};", "memory": "93137" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getItemsTopo(map<int, vector<int>>& adjList, map<int, set<int>>& groupMap, int groupNum) {\n map<int, int> indegreeMap;\n for(auto node : groupMap[groupNum]) {\n indegreeMap[node] = 0;\n }\n\n for(auto node : groupMap[groupNum]) {\n for(auto adjNode : adjList[node]) {\n if(indegreeMap.count(adjNode)) {\n indegreeMap[adjNode]++;\n }\n }\n }\n\n queue<int> q;\n for(auto [node, indegree] : indegreeMap) {\n if(indegree == 0) {\n q.push(node);\n }\n }\n\n if(q.empty()) return {};\n vector<int> topo;\n\n while(!q.empty()) {\n auto curnode = q.front();\n topo.push_back(curnode);\n q.pop();\n\n for(auto adjNode : adjList[curnode]) {\n if(indegreeMap.count(adjNode)) {\n indegreeMap[adjNode]--;\n if(indegreeMap[adjNode] == 0) {\n q.push(adjNode);\n }\n }\n }\n }\n vector<int> empty;\n return topo.size() != indegreeMap.size() ? empty : topo;\n }\n\n vector<int> getGroupsTopo(map<int, vector<int>>& adjList) {\n vector<int> topoSort;\n unordered_map<int,int> indegreeMap;\n for(auto& [node, adjNodes] : adjList) {\n if(indegreeMap.count(node) == 0) indegreeMap[node] = 0;\n for(auto adjNode : adjNodes) {\n indegreeMap[adjNode]++;\n }\n }\n\n queue<int> q;\n\n for(auto& [node, indegree] : indegreeMap) {\n if(indegree == 0) q.push(node);\n }\n\n if(q.empty()) return topoSort;\n\n while(!q.empty()) {\n auto curNode = q.front();\n topoSort.push_back(curNode);\n q.pop();\n\n for(auto adjNode : adjList[curNode]) {\n indegreeMap[adjNode]--;\n if(indegreeMap[adjNode] == 0) {\n q.push(adjNode);\n }\n }\n }\n vector<int> empty;\n return topoSort.size() == indegreeMap.size() ? topoSort : empty;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int maxGroup = *max_element(group.begin(), group.end());\n int extraGroup = maxGroup+1;\n\n map<int, vector<int>> itemsAdjList;\n for(int i = 0; i<n; i++) {\n for(auto node : beforeItems[i]) {\n itemsAdjList[node].push_back(i);\n }\n }\n\n if(itemsAdjList.size() == 0) {\n vector<int> ans;\n for(int g = -1; g < m; g++) {\n for(int i = 0; i<n; i++) {\n if(group[i] == g) {\n ans.push_back(i);\n }\n }\n }\n return ans;\n }\n\n map<int, set<int>> groupMap;\n\n map<int, vector<int>> groupAdjList;\n for(auto& pr : itemsAdjList) {\n auto& [node, adjNodes] = pr;\n int nodeGroup = group[node];\n if(nodeGroup == -1) {\n nodeGroup = extraGroup++;\n group[node] = nodeGroup;\n }\n\n groupMap[nodeGroup].insert(node);\n if(groupAdjList.count(nodeGroup) == 0) groupAdjList[nodeGroup] = {};\n\n for(auto adjNode : adjNodes) {\n if(group[adjNode] != nodeGroup) {\n if(group[adjNode] == -1) {\n groupAdjList[nodeGroup].push_back(extraGroup);\n groupMap[extraGroup].insert(adjNode);\n group[adjNode] = extraGroup;\n extraGroup++;\n }\n else {\n groupAdjList[nodeGroup].push_back(group[adjNode]);\n groupMap[group[adjNode]].insert(adjNode);\n }\n }\n }\n }\n for(int i = 0; i<n; i++) {\n groupMap[group[i]].insert(i);\n }\n\n vector<int> groupTopo = getGroupsTopo(groupAdjList);\n if(groupTopo.size() == 0) return {};\n\n vector<int> ans;\n for(auto groupNumber : groupTopo) {\n vector<int> subAns = getItemsTopo(itemsAdjList, groupMap, groupNumber);\n if(subAns.size() == 0) return {};\n for(auto item : subAns)\n ans.push_back(item);\n }\n\n for(int i = 0; i<n; i++) {\n if(group[i] == -1) {\n ans.push_back(i);\n }\n }\n\n for(int g = 0; g<m; g++) {\n if(groupAdjList.count(g) == 0) {\n for(auto node : groupMap[g]) {\n ans.push_back(node);\n }\n }\n }\n\n return ans;\n }\n};", "memory": "94412" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getItemsTopo(map<int, vector<int>>& adjList, map<int, set<int>>& groupMap, int groupNum) {\n map<int, int> indegreeMap;\n for(auto node : groupMap[groupNum]) {\n indegreeMap[node] = 0;\n }\n\n for(auto node : groupMap[groupNum]) {\n for(auto adjNode : adjList[node]) {\n if(indegreeMap.count(adjNode)) {\n indegreeMap[adjNode]++;\n }\n }\n }\n\n queue<int> q;\n for(auto [node, indegree] : indegreeMap) {\n if(indegree == 0) {\n q.push(node);\n }\n }\n\n if(q.empty()) return {};\n vector<int> topo;\n\n while(!q.empty()) {\n auto curnode = q.front();\n topo.push_back(curnode);\n q.pop();\n\n for(auto adjNode : adjList[curnode]) {\n if(indegreeMap.count(adjNode)) {\n indegreeMap[adjNode]--;\n if(indegreeMap[adjNode] == 0) {\n q.push(adjNode);\n }\n }\n }\n }\n vector<int> empty;\n return topo.size() != indegreeMap.size() ? empty : topo;\n }\n\n vector<int> getGroupsTopo(map<int, vector<int>>& adjList) {\n vector<int> topoSort;\n unordered_map<int,int> indegreeMap;\n for(auto& [node, adjNodes] : adjList) {\n if(indegreeMap.count(node) == 0) indegreeMap[node] = 0;\n for(auto adjNode : adjNodes) {\n indegreeMap[adjNode]++;\n }\n }\n\n queue<int> q;\n\n for(auto& [node, indegree] : indegreeMap) {\n if(indegree == 0) q.push(node);\n }\n\n if(q.empty()) return topoSort;\n\n while(!q.empty()) {\n auto curNode = q.front();\n topoSort.push_back(curNode);\n q.pop();\n\n for(auto adjNode : adjList[curNode]) {\n indegreeMap[adjNode]--;\n if(indegreeMap[adjNode] == 0) {\n q.push(adjNode);\n }\n }\n }\n vector<int> empty;\n return topoSort.size() == indegreeMap.size() ? topoSort : empty;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int maxGroup = *max_element(group.begin(), group.end());\n int extraGroup = maxGroup+1;\n\n map<int, vector<int>> itemsAdjList;\n for(int i = 0; i<n; i++) {\n for(auto node : beforeItems[i]) {\n itemsAdjList[node].push_back(i);\n }\n }\n\n if(itemsAdjList.size() == 0) {\n vector<int> ans;\n for(int g = -1; g < m; g++) {\n for(int i = 0; i<n; i++) {\n if(group[i] == g) {\n ans.push_back(i);\n }\n }\n }\n return ans;\n }\n\n map<int, set<int>> groupMap;\n\n map<int, vector<int>> groupAdjList;\n for(auto& pr : itemsAdjList) {\n auto& [node, adjNodes] = pr;\n int nodeGroup = group[node];\n if(nodeGroup == -1) {\n nodeGroup = extraGroup++;\n group[node] = nodeGroup;\n }\n\n groupMap[nodeGroup].insert(node);\n if(groupAdjList.count(nodeGroup) == 0) groupAdjList[nodeGroup] = {};\n\n for(auto adjNode : adjNodes) {\n if(group[adjNode] != nodeGroup) {\n if(group[adjNode] == -1) {\n groupAdjList[nodeGroup].push_back(extraGroup);\n groupMap[extraGroup].insert(adjNode);\n group[adjNode] = extraGroup;\n extraGroup++;\n }\n else {\n groupAdjList[nodeGroup].push_back(group[adjNode]);\n groupMap[group[adjNode]].insert(adjNode);\n }\n }\n }\n }\n for(int i = 0; i<n; i++) {\n groupMap[group[i]].insert(i);\n }\n\n vector<int> groupTopo = getGroupsTopo(groupAdjList);\n if(groupTopo.size() == 0) return {};\n\n vector<int> ans;\n for(auto groupNumber : groupTopo) {\n vector<int> subAns = getItemsTopo(itemsAdjList, groupMap, groupNumber);\n if(subAns.size() == 0) return {};\n for(auto item : subAns)\n ans.push_back(item);\n }\n\n for(int i = 0; i<n; i++) {\n if(group[i] == -1) {\n ans.push_back(i);\n }\n }\n\n for(int g = 0; g<m; g++) {\n if(groupAdjList.count(g) == 0) {\n for(auto node : groupMap[g]) {\n ans.push_back(node);\n }\n }\n }\n\n return ans;\n }\n};", "memory": "94412" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n // treat no group as unique groups\n // run two topological sorts\n int mx = 0;\n for (int i = 0; i < group.size(); i++) {\n mx = max(mx, group[i]);\n }\n\n mx++;\n map<int, vector<int>> groups;\n for (int i = 0; i < group.size(); i++) {\n if (group[i] == -1) group[i] = mx++;\n // cout << \"Group: \" << group[i] << \" \" << i << endl;\n groups[group[i]].push_back(i);\n }\n\n vector<int> gdeg(mx);\n vector<int> ideg(n);\n vector<vector<int>> adj(n + 1);\n vector<unordered_set<int>> gadj(mx + 1);\n\n for (int i = 0; i < beforeItems.size(); i++) {\n for (int j = 0; j < beforeItems[i].size(); j++) {\n adj[beforeItems[i][j]].push_back(i); \n ideg[i]++;\n if (group[beforeItems[i][j]] != group[i]){\n gadj[group[beforeItems[i][j]]].insert(group[i]);\n // gdeg[group[i]]++;\n }\n }\n }\n\n for (int i = 0; i < gadj.size(); i++) {\n for (auto x : gadj[i]) {\n gdeg[x]++;\n }\n }\n\n vector<int> ans;\n\n queue<int> topo;\n for (int i = 0; i < gdeg.size(); i++) {\n if (gdeg[i] == 0) {\n topo.push(i);\n }\n }\n\n while (topo.size()) {\n int cur = topo.front();\n topo.pop();\n\n // run the second topo sort\n queue<int> inner;\n vector<int> cur_group = groups[cur];\n // cout << \"Group: \";\n // for (auto x : cur_group) cout << x << \" \";\n // cout << endl;\n for (int i = 0; i < cur_group.size(); i++) {\n if (ideg[cur_group[i]] == 0) {\n inner.push(cur_group[i]);\n }\n }\n\n while (inner.size()) {\n int ind = inner.front();\n inner.pop();\n\n ans.push_back(ind);\n // cout << cur << \" \" << ind << endl;\n\n for (auto x : adj[ind]) {\n ideg[x]--;\n if (ideg[x] == 0 && group[x] == cur) {\n inner.push(x);\n }\n }\n }\n\n // cout << \"Next deps: \";\n for (auto x : gadj[cur]) {\n // cout << x << \" \";\n gdeg[x]--;\n if (gdeg[x] == 0) {\n topo.push(x);\n }\n }\n // cout << endl;\n // for (auto x : gdeg) {\n // cout << x << \" \";\n // }\n // cout << endl;\n }\n // for (auto x : ans) cout << x << \" \";\n if (ans.size() != group.size()) return {};\n\n return ans;\n }\n};", "memory": "95687" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> adj;\n vector<int> in, head, tail, order;\n void topsort(int s){\n int n = adj.size();\n queue<int> q;\n q.push(s);\n while(q.size()){\n int u = q.front(); q.pop();\n order.push_back(u);\n for(int v : adj[u]){\n in[v]--;\n if(!in[v]) q.push(v);\n }\n }\n }\n \n vector<int> sortItems(int n, int m, vector<int>& g, vector<vector<int>>& b){\n adj.resize(n + 2 * m, vector<int>()); // 2 * m nodes for head and tail of each grp\n in.resize(n + 2 * m, 0);\n head.resize(m, 0);\n tail.resize(m, 0);\n for(int i = 0, id = n; i < m; i++){\n head[i] = id++; // ith grp ka head id\n tail[i] = id++; // ith grp ka tail id + 1\n }\n\n // Graph preprocessing\n // T <- (group nodes) <- H\n for(int v = 0; v < n; v++){\n // edge connecting to the node to head and tail of the grp, of which node belongs\n if(g[v] != -1){ // grpHead -> v -> grpTail\n int H = head[g[v]];\n int T = tail[g[v]];\n adj[v].push_back(H); in[H]++;\n adj[T].push_back(v); in[v]++;\n } \n \n // edge, to represent the actual dependency between grps\n for(int u : b[v]){ // u -> v\n int from = g[u];\n int to = g[v];\n int uu = u, vv = v;\n if(from != to && from != -1) uu = head[from];\n if(from != to && to != -1) vv = tail[to];\n adj[uu].push_back(vv);\n in[vv]++;\n }\n }\n vector<int> node;\n for(int i = 0; i < n + 2 * m; i++){\n if(!in[i]) node.push_back(i);\n }\n for(int s : node){\n topsort(s);\n }\n vector<int> res;\n for(int i : order){\n if(i < n) res.push_back(i);\n }\n if(res.size() != n) res.clear();\n return res;\n }\n};", "memory": "96962" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> adj;\n vector<int> in, head, tail, order;\n void topsort(int s){\n int n = adj.size();\n queue<int> q;\n q.push(s);\n while(q.size()){\n int u = q.front(); q.pop();\n order.push_back(u);\n for(int v : adj[u]){\n in[v]--;\n if(!in[v]) q.push(v);\n }\n }\n }\n \n vector<int> sortItems(int n, int m, vector<int>& g, vector<vector<int>>& b){\n adj.resize(n + 2 * m, vector<int>()); // 2 * m nodes for head and tail of each grp\n in.resize(n + 2 * m, 0);\n head.resize(m, 0);\n tail.resize(m, 0);\n for(int i = 0, id = n; i < m; i++){\n head[i] = id++; // ith grp ka head id\n tail[i] = id++; // ith grp ka tail id + 1\n }\n\n // Graph preprocessing\n // T <- (group nodes) <- H\n for(int v = 0; v < n; v++){\n // edge connecting to the node to head and tail of the grp, of which node belongs\n if(g[v] != -1){ // grpHead <- v <- grpTail\n int H = head[g[v]];\n int T = tail[g[v]];\n adj[v].push_back(H); in[H]++;\n adj[T].push_back(v); in[v]++;\n } \n \n // edge, to represent the actual dependency between grps\n for(int u : b[v]){ // u -> v\n int from = g[u];\n int to = g[v];\n int uu = u, vv = v;\n if(from != to && from != -1) uu = head[from];\n if(from != to && to != -1) vv = tail[to];\n adj[uu].push_back(vv);\n in[vv]++;\n }\n }\n vector<int> node;\n for(int i = 0; i < n + 2 * m; i++){\n if(!in[i]) node.push_back(i);\n }\n for(int s : node){\n topsort(s);\n }\n vector<int> res;\n for(int i : order){\n if(i < n) res.push_back(i);\n }\n if(res.size() != n) res.clear();\n return res;\n }\n};", "memory": "96962" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& G, vector<vector<int>>& B) {\n unordered_map<int, unordered_set<int>> A;\n unordered_map<int, int> D;\n vector<int> v;\n stack<int> sk;\n for(int i = 0; i < n; ++i)\n {\n D[i];\n if(G[i] != -1)\n {\n int g = G[i] + n;\n D[-g];\n A[-g].insert(i);\n ++D[i];\n A[i].insert(g);//.second)\n ++D[g];\n }\n for(int j : B[i])\n {\n int b = G[j] == -1 || G[j] == G[i] ? j : (G[j] + n);\n int a = G[i] == -1 || G[j] == G[i] ? i : -(G[i] + n);\n if(A[b].insert(a).second)\n ++D[a];\n }\n }\n for(auto &[i, d] : D)\n {\n // cout<<i<<' '<<d<<endl;\n if(!d)\n sk.push(i);\n }\n while(!sk.empty())\n {\n int i = sk.top();\n sk.pop();\n if(i >= 0 && i < n)\n v.push_back(i);\n for(int j : A[i])\n if(--D[j] == 0)\n sk.push(j);\n }\n // return u;\n return v.size() == n ? v : vector<int>{};\n }\n};", "memory": "98237" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n std::unordered_multimap<int, int> group_map;\n std::unordered_multimap<int, int> group_adj_map;\n std::unordered_multimap<int, int> node_adj_map;\n void createNodeAdjMap(const vector<vector<int>>& beforeItems){\n for(int i = 0; i < beforeItems.size(); i++) {\n for(auto element : beforeItems[i]){\n node_adj_map.emplace(element, i);\n \n }\n }\n }\n void createGroupMap(const int & m, vector<int>& group){\n //generate all the groups\n int non_group_count = m;\n \n for(int i = 0; i < group.size(); i++) {\n if(group[i] == -1){\n //if not part of a group, create a new group to make group sorting easy\n group_map.emplace(non_group_count, i);\n group[i] = non_group_count;\n non_group_count++;\n }\n else{\n group_map.emplace(group[i], i);\n }\n }\n }\n void createGroupAdjMap(const vector<int>& group){\n \n //create the group map using the node map \n //get a node from the node map\n for(int i = 0; i < group.size(); i++){\n //find which group the node corresponds to\n int parent_node = i;\n int parent_node_group = group[parent_node];\n //find the nodes children/dependents\n auto range = node_adj_map.equal_range(parent_node);\n for(auto it = range.first; it != range.second; it++) {\n \n int child_node = it -> second;\n //find which groups the children/dependents are apart of\n int child_node_group = group[child_node];\n //map group to group\n if(parent_node_group == child_node_group){\n continue;\n }\n group_adj_map.emplace(parent_node_group, child_node_group);\n }\n \n } \n }\n\n \n\n vector<int> groupTopSortWithCycleDetection(const std::unordered_set<int> & group_set){\n std::unordered_set<int> group_perm;\n std::unordered_set<int> group_temp;\n std::stack<int> return_stack;\n // go through every element\n for(auto element : group_set){\n if(!groupTopSortdfs(element, group_set, group_perm, group_temp, return_stack)){\n return {};\n }\n }\n std::vector<int> return_vector;\n while(!return_stack.empty()){\n return_vector.push_back(return_stack.top());\n return_stack.pop();\n }\n return return_vector;\n }\n \n\n bool groupTopSortdfs(const int element, const std::unordered_set<int> & group_set, std::unordered_set<int> & group_perm,\n std::unordered_set<int> & group_temp,\n std::stack<int> & return_stack){\n\n if(group_perm.find(element) != group_perm.end()){\n return true;\n }\n if(group_temp.find(element) != group_temp.end()){\n return false;\n }\n \n //temp mark the node\n group_temp.insert(element);\n auto group_neighbor_range = group_adj_map.equal_range(element);\n for(auto it = group_neighbor_range.first; it != group_neighbor_range.second; it++){\n \n //dfs all the children\n if(!groupTopSortdfs(it -> second, group_set, group_perm, group_temp, return_stack)){\n return false;\n }\n }\n return_stack.push(element);\n group_temp.erase(element);\n group_perm.insert(element);\n return true;\n }\n\n\n vector<int> nodeTopSortWithCycleDetection(std::vector<int> & group_sort, std::vector<int> & groups) {\n std::vector<int> return_vector;\n for(auto group : group_sort) {\n //run top sort on this group\n std::vector<int> vec = topSortWithinGroup(group, groups);\n if(vec.size() == 0){\n return {};\n }\n return_vector.insert(return_vector.end(), vec.begin(), vec.end());\n }\n\n return return_vector;\n }\n\n vector<int> topSortWithinGroup(const int & group, vector<int> & groups) {\n auto group_range = group_map.equal_range(group);\n std::stack<int> return_stack;\n std::unordered_set<int> perm;\n std::unordered_set<int> temp;\n for(auto it = group_range.first; it != group_range.second; it++) {\n \n if(!dfsWithinGroup(groups, it -> second, perm, temp, return_stack)){\n return {};\n }\n }\n std::vector<int> return_vector;\n std::cout << \" \" << std::endl;\n while(!return_stack.empty()){\n std::cout << return_stack.top() << std::endl;\n return_vector.push_back(return_stack.top());\n return_stack.pop();\n }\n \n return return_vector;\n }\n bool dfsWithinGroup(std::vector<int> & groups, const int element, std::unordered_set<int> & perm,\n std::unordered_set<int> & temp, std::stack<int> & return_stack){\n if(perm.find(element) != perm.end()){\n return true;\n }\n if(temp.find(element) != temp.end()){\n return false;\n }\n temp.insert(element);\n //get list of all children\n auto children_range = node_adj_map.equal_range(element);\n //for all valid children \n for(auto it = children_range.first; it != children_range.second; it++){\n if(groups[element] != groups[it -> second]){\n continue;\n }\n else if(!dfsWithinGroup(groups, it -> second, perm, temp, return_stack)){\n return false;\n }\n }\n temp.erase(element);\n perm.insert(element);\n return_stack.push(element);\n \n return true;\n }\n \n \n \n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n createNodeAdjMap(beforeItems);\n createGroupMap(m, group);\n createGroupAdjMap(group);\n\n //top sort groups with cycle detection returning {}\n //get all the unique groups\n std::unordered_set<int> set;\n for(auto element : group) {\n set.insert(element);\n }\n //make sure 5 groups\n int groups = set.size();\n \n // std::cout << groups << std::endl;\n\n\n //top sort groups with cycle detection returning {}\n std::vector<int> group_sort = groupTopSortWithCycleDetection(set);\n if(group_sort.size() == 0){\n return {};\n }\n for(int i = 0; i < group_sort.size(); i++) {\n // std::cout << group_sort[i] << std::endl;\n }\n\n //using the list of groups, top sort each group and place each node into a list\n std::vector<int> node_sort = nodeTopSortWithCycleDetection(group_sort, group);\n\n return node_sort;\n }\n};", "memory": "98237" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void fun(int g,vector<int>& grp,vector<int>& inDeg,vector<vector<int>>& adj,vector<int>& topoSort,vector<int>& vec){\n queue<int> q;\n int n=grp.size();\n for(int i: vec){\n // cout<<i<<\" \";\n if(inDeg[i]==0){\n q.push(i);\n }\n }\n while(q.size()){\n int i=q.front();\n topoSort.push_back(i);\n q.pop();\n for(auto& j: adj[i]){\n if(--inDeg[j]==0 && grp[j]==g) q.push(j);\n }\n }\n // cout<<endl;\n }\n vector<int> grpGrphSort(int n,vector<int>& inDeg,vector<unordered_set<int>>& adjGrpGrph){\n vector<int> topoSort;\n queue<int> q;\n for(int i=0;i<n;i++){\n if(inDeg[i]==0){\n q.push(i);\n }\n }\n while(q.size()){\n int i=q.front();\n topoSort.push_back(i);\n q.pop();\n for(auto& j: adjGrpGrph[i]){\n if(--inDeg[j]==0) q.push(j);\n }\n }\n return topoSort;\n }\n vector<int> sortItems(int n, int m, vector<int>& grp, vector<vector<int>>& befItm) {\n vector<vector<int>> adj(n);\n vector<vector<int>> newGrp(n);\n for(int i=0;i<n;i++){\n for(int& j: befItm[i]){\n adj[j].push_back(i);\n }\n }\n int firstNonGrpEle=-1;\n int firstEmptyGrp=0;\n for(int i=0;i<n;i++){\n if(grp[i]!=-1){\n newGrp[grp[i]].push_back(i);\n while(firstEmptyGrp<n && newGrp[firstEmptyGrp].size()) firstEmptyGrp++;\n }\n else if(firstNonGrpEle==-1) firstNonGrpEle=i;\n }\n\n if(firstNonGrpEle!=-1){\n while(firstNonGrpEle<n){\n grp[firstNonGrpEle]=firstEmptyGrp;\n newGrp[firstEmptyGrp].push_back(firstNonGrpEle);\n while(firstNonGrpEle<n && grp[firstNonGrpEle]!=-1) firstNonGrpEle++;\n while(firstEmptyGrp<n && newGrp[firstEmptyGrp].size()) firstEmptyGrp++;\n }\n }\n // for(int i=0;i<n;i++){\n // for(int j: newGrp[i]) cout<<j<<\" \";\n // cout<<endl;\n // }\n vector<unordered_set<int>> adjGrpGrph(n);\n for(int i=0;i<n;i++){\n for(int& j: adj[i]){\n if(grp[i]!=grp[j]) adjGrpGrph[grp[i]].insert(grp[j]);\n }\n }\n vector<int> inDeg(n,0);\n for(int i=0;i<n;i++){\n for(auto& j: adjGrpGrph[i]){\n inDeg[j]++;\n }\n }\n vector<int> ans=grpGrphSort(n,inDeg,adjGrpGrph);\n if(ans.size()<n) return {};\n vector<int> topoSort;\n for(int i=0;i<n;i++){\n for(auto& j: adj[i]){\n inDeg[j]++;\n }\n }\n for(int i=0;i<n;i++) fun(ans[i],grp,inDeg,adj,topoSort,newGrp[ans[i]]);\n if(topoSort.size()<n) return {};\n return topoSort;\n }\n};", "memory": "99512" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic: \n vector<int> topSort(map<int, vector<int>> &adjList){\n map<int, int> inDeg;\n for(auto it: adjList){\n if(inDeg.count(it.first) == 0) inDeg[it.first] = 0;\n for(int neigh: it.second)\n ++inDeg[neigh];\n }\n\n queue<int> q;\n for(auto it: inDeg)\n if(inDeg.count(it.first) != 0 && it.second == 0)\n q.push(it.first);\n\n vector<int> ans;\n while(!q.empty()){\n int f = q.front();\n ans.push_back(f);\n q.pop();\n\n for(int neigh: adjList[f]){\n --inDeg[neigh];\n if(inDeg[neigh] == 0)\n q.push(neigh);\n }\n }\n\n return (ans.size() == adjList.size()) ? ans : vector<int>{};\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& prevs) {\n int currG = m;\n map<int, vector<int>> groups;\n for(int i=0; i<n; ++i){\n if(group[i] == -1) group[i] = currG++;\n groups[group[i]] = {i};\n }\n\n map<int, vector<int>> adjList;\n map<int, map<int, vector<int>>> groupAdjList;\n for(int i=0; i<n; ++i){\n for(auto prev: prevs[i])\n if(group[prev] != group[i]) adjList[group[prev]].push_back(group[i]);\n else groupAdjList[group[i]][prev].push_back(i);\n\n if(adjList.count(group[i]) == 0)\n adjList[group[i]] = {};\n if(groupAdjList[group[i]].count(i) == 0)\n groupAdjList[group[i]][i] = {}; \n }\n \n for(auto it: groupAdjList){\n vector<int> g = topSort(it.second);\n if(g.size() == 0) return {};\n groups[it.first] = g;\n }\n\n vector<int> topSorted = topSort(adjList);\n if(topSorted.size() == 0) return {};\n vector<int> ans;\n\n for(int it: topSorted)\n ans.insert(ans.end(), groups[it].begin(), groups[it].end());\n\n return ans;\n }\n};", "memory": "100787" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int groupId = m;\n map<int, set<int>> groupSet;\n\n for(int i=0; i<n; i++){\n if(group[i]==-1){\n group[i] = (groupId++);\n }\n groupSet[group[i]].insert(i);\n }\n\n map<int, set<int>> next;\n map<int, int> inDegree;\n\n for(int i=0;i<n;i++){\n for(int j: beforeItems[i]){\n if(group[i]!=group[j]){continue;}\n next[j].insert(i);\n inDegree[i]++;\n }\n }\n map<int, vector<int>> groupVector;\n for(int i=0; i<groupId; i++){\n if(groupSet[i].size()==0){continue;}\n vector<int> groupOrder = TS(groupSet[i], next, inDegree);\n // cout<<\"group:\"<<i<<endl;\n // for(int num: groupOrder){cout<<num<<' ';}cout<<endl;\n if(groupOrder.size()==0){return {};}\n groupVector[i] = groupOrder;\n }\n\n next.clear();\n inDegree.clear();\n\n for(int i=0;i<n;i++){\n for(int j: beforeItems[i]){\n if(group[i]==group[j]){continue;}\n if(next[group[j]].find(group[i])==next[group[j]].end()){\n next[group[j]].insert(group[i]);\n inDegree[group[i]]++;\n }\n }\n }\n\n set<int> groupAll;\n for(int i=0;i<n;i++){\n groupAll.insert(group[i]);\n }\n\n vector<int> order = TS(groupAll, next, inDegree);\n if(order.size()==0){return {};}\n vector<int> res;\n\n for(int i: order){\n for(int j: groupVector[i]){\n res.push_back(j);\n }\n }\n return res;\n }\n\n vector<int> TS(set<int>& group, map<int, set<int>>& next, map<int, int>& inDegree){\n vector<int> res;\n queue<int> q;\n for(int item: group){\n if(inDegree[item]==0){\n q.push(item);\n }\n }\n\n while(!q.empty()){\n int cur = q.front();\n q.pop();\n res.push_back(cur);\n\n for(int next: next[cur]){\n inDegree[next]--;\n if(inDegree[next]==0){\n q.push(next);\n }\n }\n }\n if(res.size()==group.size()){return res;}\n return {};\n }\n};", "memory": "102062" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> topSort(unordered_map<int, vector<int>> &adj) {\n int N = adj.size();\n\n unordered_map<int, int> indegree;\n for(auto entry: adj)\n indegree[entry.first] = 0;\n for(auto entry: adj) {\n int u = entry.first;\n for(int v: adj[u]) {\n indegree[v]++;\n }\n }\n\n queue<int> Q;\n for(auto entry: indegree) {\n int u = entry.first;\n if(indegree[u] == 0) \n Q.push(u);\n }\n\n vector<int> ans(0);\n \n while(!Q.empty()) {\n int u = Q.front(); Q.pop();\n for(int v: adj[u]) {\n indegree[v]--;\n if(indegree[v] == 0)\n Q.push(v);\n }\n ans.push_back(u);\n }\n\n if(ans.size() != N)\n return vector<int>(0);\n\n return ans;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n // draw edges from parent to child (before element to after element)\n\n // putting lone elements in new groups\n for(int u=0; u<n; u++) {\n if(group[u] == -1) {\n group[u] = m;\n m++;\n }\n }\n\n // make group adj list\n unordered_map<int, vector<int>> adjG;\n for(int g=0; g<m; g++)\n adjG[g] = vector<int>(0);\n for(int u=0; u<n; u++) {\n for(int v: beforeItems[u]) {\n if(group[v] != group[u])\n adjG[group[v]].push_back(group[u]);\n }\n }\n\n // top sort groups\n vector<int> topSortedG = topSort(adjG);\n if(topSortedG.size() == 0)\n return vector<int>(0);\n\n // grouping items\n unordered_map<int, set<int>> groupItems;\n for(int u=0; u<n; u++) {\n groupItems[group[u]].insert(u);\n }\n\n vector<int> ANS(0);\n\n for(int g: topSortedG) {\n set<int> currentGroup = groupItems[g];\n\n if(currentGroup.size() == 0)\n continue;\n \n // making adjacency list for current group\n unordered_map<int, vector<int>> adj;\n for(int u: currentGroup)\n adj[u] = vector<int>(0);\n for(int u: currentGroup) {\n for(int v: beforeItems[u]) {\n if(currentGroup.contains(v))\n adj[v].push_back(u);\n }\n }\n\n // running top sort on this\n vector<int> topSortedCurrentGroup = topSort(adj);\n if(topSortedCurrentGroup.size() == 0)\n return vector<int>(0);\n\n ANS.insert(ANS.end(), topSortedCurrentGroup.begin(), topSortedCurrentGroup.end());\n }\n\n return ANS;\n }\n};", "memory": "103337" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> topoSort(vector<vector<int>> &edges, int n){\n vector<vector<int>> graph(n);\n vector<int> indegree(n);\n\n for (auto edge : edges){\n graph[edge[0]].push_back(edge[1]);\n indegree[edge[1]]++;\n }\n\n queue<int> q; \n\n for (int i = 0; i < n; i++){\n if (indegree[i] == 0) q.push(i);\n }\n\n vector<int> ans;\n\n while (!q.empty()){\n int node = q.front(); \n q.pop(); \n\n ans.push_back(node);\n for (int child : graph[node]){\n indegree[child]--; \n if (indegree[child] == 0) q.push(child);\n }\n }\n\n return ans;\n }\n\n vector<int> topoSort(vector<vector<int>> &edges, vector<int> &nodes){\n unordered_map<int, vector<int>> graph; \n unordered_map<int, int> indegree;\n vector<int> ans;\n\n for (int i = 0; i < nodes.size(); i++){\n graph[nodes[i]] = {};\n indegree[nodes[i]] = 0;\n }\n\n for (auto edge : edges){\n graph[edge[0]].push_back(edge[1]);\n indegree[edge[1]]++;\n }\n\n queue<int> q;\n for (auto p : indegree){\n if (p.second == 0) q.push(p.first);\n }\n\n while (!q.empty()){\n int node = q.front(); \n q.pop(); \n\n ans.push_back(node);\n\n for (int child : graph[node]){\n indegree[child]--; \n if (indegree[child] == 0) q.push(child);\n }\n }\n\n return ans;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n for (int i = 0; i < n; i++){\n if (group[i] == -1) group[i] = m++;\n }\n\n // sort groups\n vector<vector<int>> groupEdges; \n vector<vector<vector<int>>> edges(m);\n vector<vector<int>> items(m);\n\n for (int i = 0; i < n; i++){\n items[group[i]].push_back(i);\n for (int j = 0; j < beforeItems[i].size(); j++){\n if (group[i] == group[beforeItems[i][j]]) edges[group[i]].push_back({beforeItems[i][j], i});\n else groupEdges.push_back({group[beforeItems[i][j]], group[i]});\n }\n }\n\n vector<int> groupSorted = topoSort(groupEdges, m);\n\n if (groupSorted.size() != m) return {};\n\n vector<vector<int>> withinGroups(m);\n\n for (int i = 0; i < m; i++){\n withinGroups[i] = topoSort(edges[i], items[i]);\n if (withinGroups[i].size() != items[i].size()) return {};\n }\n\n vector<int> ans;\n\n for (int i = 0; i < m; i++){\n ans.insert(ans.end(), withinGroups[groupSorted[i]].begin(), withinGroups[groupSorted[i]].end());\n }\n\n return ans;\n }\n};", "memory": "104612" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n int groupId = m;\n map<int, set<int>> groupItem;\n\n for(int i=0;i<n;i++){\n if(group[i]==-1){\n group[i]=(groupId++);\n }\n groupItem[group[i]].insert(i);\n }\n\n map<int, set<int>> next;\n map<int, int> indegree;\n\n for(int i=0;i<n;i++){\n for(int j: beforeItems[i]){\n if(group[i]!=group[j]){continue;}\n next[j].insert(i);\n indegree[i]++;\n }\n }\n\n map<int, vector<int>> groupOrdered;\n for(int i=0;i<groupId;i++){\n if(groupItem[i].size()==0){continue;}\n groupOrdered[i] = TS(groupItem[i], next, indegree);\n\n // cout<<\"group:\"<<i<<endl;\n // for(int num: groupOrdered[i]){cout<<num<<' ';}\n // cout<<endl;\n if(groupOrdered[i].size()==0){return {};}\n }\n\n next.clear();\n indegree.clear();\n\n for(int i=0;i<n;i++){\n for(int j: beforeItems[i]){\n if(group[i]==group[j]){continue;}\n if(next[group[j]].find(group[i])==next[group[j]].end())\n {\n next[group[j]].insert(group[i]);\n indegree[group[i]]++;\n }\n \n }\n }\n\n set<int> allItem;\n for(int i=0;i<groupId;i++){allItem.insert(i);}\n // for(int i: allItem){cout<<i<<' ';}cout<<endl;\n vector<int> order = TS(allItem, next, indegree);\n // cout<<\"order:\"<<order.size()<<endl;\n if(order.size()==0){return {};}\n\n vector<int> res;\n for(int i: order){\n for(int item: groupOrdered[i]){\n res.push_back(item);\n }\n }\n return res;\n }\n\n vector<int> TS(set<int>& groups, map<int, set<int>> &next, map<int, int>& indegree){\n queue<int> q;\n vector<int> res;\n for(int node: groups){\n if(indegree[node]==0){\n q.push(node);\n }\n }\n\n while(!q.empty()){\n int cur = q.front();\n q.pop();\n res.push_back(cur);\n\n for(int next: next[cur]){\n indegree[next]--;\n if(indegree[next]==0){\n q.push(next);\n }\n }\n }\n \n if(res.size()==groups.size()){return res;}\n return {};\n }\n};", "memory": "105887" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n inline int encode(int n, int grpIdx, int i) {\n return (grpIdx == -1) ? i : (n + grpIdx);\n }\n\n vector<int> topoSort(unordered_map<int, vector<int>>& edges, unordered_map<int, int>& inDeg) {\n queue<int> q;\n for (auto& p : inDeg) {\n if (p.second == 0) {\n q.push(p.first);\n }\n }\n\n vector<int> res;\n while (!q.empty()) {\n int curr = q.front();\n q.pop();\n res.push_back(curr);\n for (int nxt : edges[curr]) {\n if (--inDeg[nxt] == 0) {\n q.push(nxt);\n }\n }\n }\n\n if (res.size() < edges.size()) {\n return {};\n }\n return res;\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n vector<unordered_map<int, vector<int>>> edgeMaps(m);\n vector<unordered_map<int, int>> inDegMaps(m);\n unordered_map<int, vector<int>> grpEdges;\n unordered_map<int, int> grpInDeg;\n for (int i = 0; i < m; ++i) {\n grpInDeg[i] = 0;\n }\n for (int i = 0; i < n; ++i) {\n int grp = group[i];\n if (grp == -1) {\n continue;\n }\n inDegMaps[grp][i] = 0;\n\n for (int bf : beforeItems[i]) {\n int grpB = group[bf];\n if (grp == grpB) {\n edgeMaps[grp][bf].push_back(i);\n ++inDegMaps[grp][i];\n } else if (grpB != -1) {\n grpEdges[grpB].push_back(grp);\n ++grpInDeg[grp];\n }\n }\n }\n\n vector<int> grpOrder = topoSort(grpEdges, grpInDeg);\n if (grpOrder.size() < m) {\n return {};\n }\n vector<vector<int>> orders(m);\n for (int i = 0; i < m; ++i) {\n orders[i] = topoSort(edgeMaps[i], inDegMaps[i]);\n if (orders[i].size() < inDegMaps[i].size()) {\n return {};\n }\n }\n\n unordered_map<int, vector<int>> metaEdges;\n unordered_map<int, int> metaInDeg;\n for (int i = 0; i < n; ++i) {\n int keyCurr = encode(n, group[i], i);\n if (!metaInDeg.count(keyCurr)) {\n metaInDeg[keyCurr] = 0;\n }\n\n for (int bf : beforeItems[i]) {\n int keyBf = encode(n, group[bf], bf);\n if (keyCurr == keyBf) {\n continue;\n }\n metaEdges[keyBf].push_back(keyCurr);\n ++metaInDeg[keyCurr];\n }\n }\n vector<int> metaOrder = topoSort(metaEdges, metaInDeg);\n if (metaOrder.size() < metaInDeg.size()) {\n return {};\n }\n\n vector<int> res;\n for (int curr : metaOrder) {\n if (curr < n) {\n res.push_back(curr);\n } else {\n for (int x : orders[curr - n]) {\n res.push_back(x);\n }\n }\n }\n return res;\n }\n};", "memory": "107162" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic: \n vector<int> topSort(unordered_map<int, vector<int>> &adjList){\n unordered_map<int, int> inDeg;\n for(auto it: adjList){\n if(inDeg.count(it.first) == 0) inDeg[it.first] = 0;\n for(int neigh: it.second)\n ++inDeg[neigh];\n }\n\n queue<int> q;\n for(auto it: inDeg)\n if(inDeg.count(it.first) != 0 && it.second == 0)\n q.push(it.first);\n\n vector<int> ans;\n while(!q.empty()){\n int f = q.front();\n ans.push_back(f);\n q.pop();\n\n for(int neigh: adjList[f]){\n --inDeg[neigh];\n if(inDeg[neigh] == 0)\n q.push(neigh);\n }\n }\n\n return (ans.size() == adjList.size()) ? ans : vector<int>{};\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& prevs) {\n int currG = m;\n unordered_map<int, vector<int>> groups;\n for(int i=0; i<n; ++i){\n if(group[i] == -1) group[i] = currG++;\n groups[group[i]] = {i};\n }\n\n unordered_map<int, vector<int>> adjList;\n unordered_map<int, unordered_map<int, vector<int>>> groupAdjList;\n for(int i=0; i<n; ++i){\n for(auto prev: prevs[i])\n if(group[prev] != group[i]) adjList[group[prev]].push_back(group[i]);\n else groupAdjList[group[i]][prev].push_back(i);\n\n if(adjList.count(group[i]) == 0) adjList[group[i]] = {};\n if(groupAdjList[group[i]].count(i) == 0) groupAdjList[group[i]][i] = {}; \n }\n \n for(auto it: groupAdjList){\n vector<int> g = topSort(it.second);\n if(g.size() == 0) return {};\n groups[it.first] = g;\n }\n\n vector<int> topSorted = topSort(adjList);\n if(topSorted.size() == 0) return {};\n vector<int> ans;\n\n for(int it: topSorted)\n ans.insert(ans.end(), groups[it].begin(), groups[it].end());\n\n return ans;\n }\n};", "memory": "108437" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic: \n vector<int> topSort(unordered_map<int, vector<int>> &adjList){\n unordered_map<int, int> inDeg;\n for(auto it: adjList){\n if(inDeg.count(it.first) == 0) inDeg[it.first] = 0;\n for(int neigh: it.second)\n ++inDeg[neigh];\n }\n\n queue<int> q;\n for(auto it: inDeg)\n if(inDeg.count(it.first) != 0 && it.second == 0)\n q.push(it.first);\n\n vector<int> ans;\n while(!q.empty()){\n int f = q.front();\n ans.push_back(f);\n q.pop();\n\n for(int neigh: adjList[f]){\n --inDeg[neigh];\n if(inDeg[neigh] == 0)\n q.push(neigh);\n }\n }\n\n return (ans.size() == adjList.size()) ? ans : vector<int>{};\n }\n\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& prevs) {\n int currG = m;\n unordered_map<int, vector<int>> groups;\n for(int i=0; i<n; ++i){\n if(group[i] == -1) group[i] = currG++;\n groups[group[i]] = {i};\n }\n\n unordered_map<int, vector<int>> adjList;\n unordered_map<int, unordered_map<int, vector<int>>> groupAdjList;\n for(int i=0; i<n; ++i){\n for(auto prev: prevs[i])\n if(group[prev] != group[i]) adjList[group[prev]].push_back(group[i]);\n else groupAdjList[group[i]][prev].push_back(i);\n\n if(adjList.count(group[i]) == 0) adjList[group[i]] = {};\n if(groupAdjList[group[i]].count(i) == 0) groupAdjList[group[i]][i] = {}; \n }\n \n for(auto it: groupAdjList){\n vector<int> g = topSort(it.second);\n if(g.size() == 0) return {};\n groups[it.first] = g;\n }\n\n vector<int> topSorted = topSort(adjList);\n if(topSorted.size() == 0) return {};\n vector<int> ans;\n\n for(int it: topSorted)\n ans.insert(ans.end(), groups[it].begin(), groups[it].end());\n\n return ans;\n }\n};", "memory": "109712" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) \n {\n unordered_map<int, unordered_set<int>>groupItems;\n int nextGroupId = m;\n\n for (int i=0; i<n; i++)\n {\n if (group[i]==-1)\n {\n group[i] = nextGroupId;\n nextGroupId += 1;\n }\n groupItems[group[i]].insert(i);\n }\n\n // build graph inside each group\n unordered_map<int, unordered_set<int>>next;\n unordered_map<int, int>inDegree;\n for (int i=0; i<n; i++) \n for (int j: beforeItems[i])\n {\n if (group[i]!=group[j]) continue;\n if (next[j].find(i)==next[j].end()) \n {\n next[j].insert(i);\n inDegree[i] += 1;\n }\n }\n // sort nodes inside each group\n unordered_map<int, vector<int>>groupItemsOrdered;\n for (auto x: groupItems)\n {\n int groupId = x.first;\n groupItemsOrdered[groupId] = topologySort(groupItems[groupId], next, inDegree);\n if (groupItemsOrdered[groupId].size() != groupItems[groupId].size())\n return {};\n }\n\n // build graph among groups\n next.clear();\n inDegree.clear();\n for (int i=0; i<n; i++) \n for (int j: beforeItems[i])\n {\n if (group[i]==group[j]) continue;\n if (next[group[j]].find(group[i])==next[group[j]].end()) \n {\n next[group[j]].insert(group[i]);\n inDegree[group[i]] += 1;\n }\n }\n // sort groups\n unordered_set<int>groups;\n for (int i=0; i<n; i++) groups.insert(group[i]);\n vector<int>groupOrdered = topologySort(groups, next, inDegree);\n\n vector<int>rets;\n for (int groupId: groupOrdered)\n {\n for (auto node: groupItemsOrdered[groupId])\n rets.push_back(node);\n }\n return rets;\n }\n\n vector<int> topologySort (unordered_set<int>&nodes, unordered_map<int, unordered_set<int>>&next, unordered_map<int, int>&inDegree)\n {\n queue<int>q;\n vector<int>ret;\n for (auto node: nodes)\n {\n if (inDegree[node]==0)\n q.push(node);\n }\n while (!q.empty())\n {\n int cur = q.front();\n q.pop();\n ret.push_back(cur); \n for (auto next: next[cur] )\n {\n inDegree[next] -= 1;\n if (inDegree[next] == 0)\n q.push(next);\n }\n }\n \n if (ret.size()==nodes.size())\n return ret;\n else\n return {};\n }\n};", "memory": "110987" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n \n unordered_map<int, unordered_set<int>> groups;\n unordered_map<int, unordered_set<int>> graph;\n unordered_map<int, int> indegree;\n\n int ngsf = -1;\n for(int i = 0; i < n; i++)\n {\n if(indegree.find(i) == indegree.end())\n indegree[i] = 0;\n \n if(group[i] == -1){\n group[i] = ngsf;\n groups[ngsf--].insert(i);\n }\n else\n groups[group[i]].insert(i);\n }\n\n unordered_map<int, unordered_set<int>> order;\n unordered_map<int, int> ind_groups;\n\n\n for(int i = ngsf+1; i < m; i++)\n {\n if(ind_groups.find(i) == ind_groups.end())\n ind_groups[i] = 0;\n }\n for(int i = 0; i < n; i++)\n {\n if(beforeItems[i].size() == 0)\n continue;\n for(auto before : beforeItems[i])\n {\n if(groups[group[i]].find(before) != groups[group[i]].end())\n {graph[before].insert(i);\n indegree[i]++;}\n else\n {\n if(order[group[before]].find(group[i]) == order[group[before]].end())\n{ order[group[before]].insert(group[i]);\n ind_groups[group[i]]++;}\n }\n }\n }\n\n //perform topo sort on each group\n unordered_map<int, vector<int>> topos;\n for(auto it : groups)\n {\n int group = it.first;\n auto nodes = it.second;\n queue<int> q;\n\n for(auto node : nodes)\n {\n if(indegree[node] == 0)\n q.push(node);\n }\n\n int vis = 0;\n vector<int> topo;\n\n while(not q.empty())\n {\n int node = q.front();\n q.pop();\n vis++;\n topo.push_back(node);\n\n if(graph.find(node) != graph.end())\n {\n for(auto kid : graph[node])\n {\n indegree[kid]--;\n if(not indegree[kid])\n q.push(kid);\n }\n }\n }\n\n if(vis != nodes.size())\n return {};\n topos[group] = topo;\n }\n\n vector<int> final_order;\n queue<int> q;\n\n for(int i = ngsf+1; i < m; i++)\n {\n if(ind_groups[i] == 0)\n q.push(i);\n }\n\n int vis = 0;\n\n while(not q.empty())\n {\n int node = q.front();\n q.pop();\n vis++;\n final_order.push_back(node);\n if(order.find(node) != order.end())\n {\n for(auto kid : order[node])\n {\n ind_groups[kid]--;\n if(not ind_groups[kid])\n q.push(kid);\n }\n }\n }\n if(vis != (m-ngsf-1))\n return {};\n \n vector<int> ans;\n\n for(auto i : final_order)\n {\n auto v = topos[i];\n for(auto j : v)\n ans.push_back(j);\n }\n\n return ans;\n }\n};", "memory": "112262" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n unordered_map<int, set<int>> groupGraph;\n unordered_map<int, vector<int>> groupAndItems;\n unordered_map<int, int> itemAndGroup;\n int idxNewGroup = m;\n for(int i = 0; i < group.size(); i++) {\n if(group[i] == -1) {\n groupAndItems[idxNewGroup].push_back(i);\n itemAndGroup[i] = idxNewGroup++;\n } else {\n groupAndItems[group[i]].push_back(i);\n itemAndGroup[i] = group[i];\n } \n }\n\n for(auto &[group, items]: groupAndItems) {\n unordered_map<int, set<int>> itemsGraph;\n for(int &item: items) {\n vector<int> before = beforeItems[item];\n for(int &num: before) {\n if(itemAndGroup[num] != group) continue;\n itemsGraph[num].insert(item);\n }\n }\n for(int &item: items) \n if(!itemsGraph.count(item)) itemsGraph[item] = {};\n items = apply_khan(itemsGraph);\n if(items.size() == 0) return {};\n for(int &item: items) {\n vector<int> before = beforeItems[item];\n for(int &num: before) {\n int before_group = itemAndGroup[num];\n if(before_group == group) continue;\n groupGraph[itemAndGroup[num]].insert(group);\n }\n }\n }\n\n for(auto &[group, items]: groupAndItems) { \n if(!groupGraph.count(group)) groupGraph[group] = {};\n }\n\n vector<int> result;\n vector<int> x = apply_khan(groupGraph);\n for(int &group: x) {\n result.insert(result.end(), groupAndItems[group].begin(), groupAndItems[group].end());\n }\n return result;\n }\n\n vector<int> apply_khan(unordered_map<int, set<int>> graph) {\n unordered_map<int, int> in_going;\n for(auto& [from, to]: graph) {\n if(!in_going.count(from)) in_going[from] = 0;\n for(int num: to) in_going[num]++;\n }\n\n vector<int> result;\n queue<int> q;\n for(auto &[x, y]: in_going)\n if(!y) q.push(x);\n\n while(q.size()) {\n int current = q.front(); q.pop();\n result.push_back(current);\n for(int child: graph[current]) {\n in_going[child]--;\n if(!in_going[child]) q.push(child);\n }\n }\n\n if(result.size() != in_going.size()) return {};\n return result;\n }\n};", "memory": "113537" }
1,309
<p>There are&nbsp;<code>n</code>&nbsp;items each&nbsp;belonging to zero or one of&nbsp;<code>m</code>&nbsp;groups where <code>group[i]</code>&nbsp;is the group that the <code>i</code>-th item belongs to and it&#39;s equal to <code>-1</code>&nbsp;if the <code>i</code>-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.</p> <p>Return a sorted list of the items such that:</p> <ul> <li>The items that belong to the same group are next to each other in the sorted list.</li> <li>There are some&nbsp;relations&nbsp;between these items where&nbsp;<code>beforeItems[i]</code>&nbsp;is a list containing all the items that should come before the&nbsp;<code>i</code>-th item in the sorted array (to the left of the&nbsp;<code>i</code>-th item).</li> </ul> <p>Return any solution if there is more than one solution and return an <strong>empty list</strong>&nbsp;if there is no solution.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/11/1359_ex1.png" style="width: 191px; height: 181px;" /></strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] <strong>Output:</strong> [6,3,4,1,5,2,0,7] </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] <strong>Output:</strong> [] <strong>Explanation:</strong>&nbsp;This is the same as example 1 except that 4 needs to be before 6 in the sorted list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>group.length == beforeItems.length == n</code></li> <li><code>-1 &lt;= group[i] &lt;= m - 1</code></li> <li><code>0 &lt;= beforeItems[i].length &lt;= n - 1</code></li> <li><code>0 &lt;= beforeItems[i][j] &lt;= n - 1</code></li> <li><code>i != beforeItems[i][j]</code></li> <li><code>beforeItems[i]&nbsp;</code>does not contain&nbsp;duplicates elements.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {\n \n unordered_map<int, unordered_set<int>> groups;\n unordered_map<int, unordered_set<int>> graph;\n unordered_map<int, int> indegree;\n\n int ngsf = -1;\n for(int i = 0; i < n; i++)\n {\n if(indegree.find(i) == indegree.end())\n indegree[i] = 0;\n \n if(group[i] == -1){\n group[i] = ngsf;\n groups[ngsf--].insert(i);\n }\n else\n groups[group[i]].insert(i);\n }\n\n unordered_map<int, unordered_set<int>> order;\n unordered_map<int, int> ind_groups;\n\n\n for(int i = ngsf+1; i < m; i++)\n {\n for(auto j : groups[i])\n cout << j << \" \";\n cout << endl;\n if(ind_groups.find(i) == ind_groups.end())\n ind_groups[i] = 0;\n }\n for(int i = 0; i < n; i++)\n {\n if(beforeItems[i].size() == 0)\n continue;\n for(auto before : beforeItems[i])\n {\n if(groups[group[i]].find(before) != groups[group[i]].end())\n {graph[before].insert(i);\n indegree[i]++;}\n else\n {\n if(order[group[before]].find(group[i]) == order[group[before]].end())\n{ order[group[before]].insert(group[i]);\n ind_groups[group[i]]++;}\n }\n }\n }\n\n //printing the graph\n // for(auto it : graph)\n // {\n // cout << it.first << \" :\";\n\n // for(auto k : it.second)\n // cout << k << \" \";\n // cout << endl;\n // }\n\n // cout << \"printing the order of groups to return\" << endl;\n // for(auto it : order)\n // {\n // cout << it.first << \" :\";\n\n // for(auto k : it.second)\n // cout << k << \" \";\n \n // cout << endl;\n // }\n //perform topo sort on each group\n unordered_map<int, vector<int>> topos;\n for(auto it : groups)\n {\n int group = it.first;\n auto nodes = it.second;\n\n cout << \"group : \" << group << endl;\n\n // cout << \"nodes : \" << endl;\n\n // for(auto z : nodes)\n // cout << z << \" \";\n // cout << endl;\n\n queue<int> q;\n\n for(auto node : nodes)\n {\n if(indegree[node] == 0)\n q.push(node);\n }\n\n int vis = 0;\n vector<int> topo;\n while(not q.empty())\n {\n int node = q.front();\n q.pop();\n vis++;\n topo.push_back(node);\n\n if(graph.find(node) != graph.end())\n {\n for(auto kid : graph[node])\n {\n indegree[kid]--;\n if(not indegree[kid])\n q.push(kid);\n }\n }\n }\n // cout << \" printing topo for each group : \" << endl;\n // for(auto z : topo)\n // cout << z << \" \";\n\n // cout << endl;\n if(vis != nodes.size())\n return {};\n topos[group] = topo;\n }\n\n vector<int> final_order;\n queue<int> q;\n\n for(int i = ngsf+1; i < m; i++)\n {\n if(ind_groups[i] == 0)\n q.push(i), cout << i, cout << endl;\n }\n\n int vis = 0;\n\n while(not q.empty())\n {\n int node = q.front();\n q.pop();\n vis++;\n final_order.push_back(node);\n if(order.find(node) != order.end())\n {\n for(auto kid : order[node])\n {\n ind_groups[kid]--;\n if(not ind_groups[kid])\n q.push(kid);\n }\n }\n }\n cout << \"printing final order\" << endl;\n for(auto z : final_order)\n cout << z << \" \";\n cout << endl;\n if(vis != (m-ngsf-1))\n return {};\n \n vector<int> ans;\n for(auto i : final_order)\n {\n auto v = topos[i];\n for(auto j : v)\n ans.push_back(j);\n }\n return ans;\n }\n};", "memory": "114812" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n int ans[70];\n int k=0;\n for(int i=1;i<arr.size();i++)\n {\n int j=1;\n while(arr[i]==arr[i-1]&&i<arr.size()-1)\n {\n \n i++;\n \n j++;\n \n }\n if(arr[i]==arr[i-1]&&i==arr.size()-1)\n {\n j++;\n }\n else if(i==arr.size()-1){\n ans[k]=j;\n k++;\n j=1;\n }\n ans[k]=j;\n \n for(int o=0;o<k;o++)\n {\n if(ans[o]==ans[k])\n {\n return false;\n }\n }\n k++;\n\n }\n return true;\n \n }\n};", "memory": "10500" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n sort(arr.begin(), arr.end());\n unordered_set<int> seen;\n \n int cnt = 0;\n\n int i=0;\n int j=0;\n for (i; j<arr.size(); j++) {\n if (arr[i] != arr[j]) {\n if (seen.count(j-i)) {\n return false;\n } else {\n seen.insert(j-i);\n i = j;\n }\n }\n }\n\n if (seen.count(j-i))\n return false;\n \n return true;\n }\n};", "memory": "10600" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n vector<int> occurrences;\n sort(arr.begin(), arr.end());\n int count = 1;\n for (int i = 0; i < arr.size() - 1; i++)\n {\n if (arr[i] == arr[i + 1])\n count++;\n else\n {\n occurrences.push_back(count);\n count = 1;\n }\n }\n occurrences.push_back(count);\n sort(occurrences.begin(), occurrences.end());\n for (int i = 0; i < occurrences.size()-1; i++){\n if (occurrences[i] == occurrences[i+1])\n {\n return false;\n }\n }\n return true;\n }\n};", "memory": "10700" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n \n vector<int> ans;\n\n\n for(int i=0;i<arr.size();i++){\n int temp=arr[i];\n if(temp==-2333) continue;\n int cnt=1;\n for(int j=i+1;j<arr.size();j++){\n if(temp==arr[j]) {\n if(arr[j]==-2333){\n\n continue;\n }\n else{\n cnt++;\n arr[j]=-2333;\n }\n\n }\n\n }\n ans.push_back(cnt);\n\n }\n\n\n \n for(int i=0;i<ans.size();i++){\n int temp=ans[i];\n for(int j=i+1;j<ans.size();j++){\n if(temp==ans[j]) return false;\n }\n }\n \n return true;\n }\n};", "memory": "10700" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n std::vector<int> values;\n std::vector<int> counts;\n\n\n for (int i=0; i < arr.size(); i++)\n {\n auto j = std::find(values.begin(), values.end(), arr[i]);\n\n if (j == values.end()){\n values.push_back(arr[i]);\n counts.push_back(1);\n }\n else{\n int index = j - values.begin(); \n counts[index] += 1;\n }\n }\n std::set s = std::set(counts.begin(), counts.end());\n\n \n return s.size() == counts.size();\n }\n};", "memory": "10800" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n \n int n=arr.size();\n \n vector<int> ans;\n sort(arr.begin(),arr.end());\n for(int i=0;i<n;)\n { \n int c=1;\n for(int j=i+1;j<n;j++)\n {\n if(arr[i]==arr[j])\n {\n c++; \n }\n }\n ans.push_back(c);\n i+=c;\n }\n sort(ans.begin(),ans.end());\n\n for(int i=0;i<ans.size()-1;i++)\n {\n if(ans[i]==ans[i+1]) return false;\n }\n return true;\n }\n};", "memory": "10800" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n vector<int> ans;\n int size = arr.size();\n sort(arr.begin(),arr.end());\n int i = 0;\n while(i<size){\n int count = 1;\n for(int j = i+1;j<size;j++){\n if (arr[i] == arr[j]){\n count++;\n }\n else{\n break;\n }\n }\n ans.push_back(count);\n i = i + count;\n }\n size = ans.size();\n sort(ans.begin(), ans.end());\n for(int i = 0 ; i< size-1;i++){\n if(ans[i] == ans[i+1]){\n return false;\n }\n \n }\n \n return true;\n }\n};", "memory": "10900" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n vector<int> ans;\n int size =arr.size();\n sort(arr.begin(),arr.end());\n int i =0;\n while(i<size){\n int count =1;\n for(int j = i+1; j<size; j++){\n if(arr[i]==arr[j]){\n count++;\n }\n else{\n break;\n }\n }\n ans.push_back(count);\n i = i+count;\n }\n size=ans.size();\n sort(ans.begin(),ans.end());\n for(int i =0; i<size-1; i++){\n if(ans[i]==ans[i+1]){\n return false;\n }\n }\n return true;\n }\n};", "memory": "10900" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n vector<int> res;\n\n sort(arr.begin(), arr.end());\n for(int i = 0; i < arr.size(); i++)\n {\n int count = 0;\n while((i+1) < arr.size() && arr[i] == arr[i+1])\n {\n i++;\n count++;\n }\n res.push_back(count);\n }\n sort(res.begin(), res.end());\n for(int i = 0; i < res.size()-1; i++)\n {\n if(res[i] == res[i+1])\n return false;\n }\n return true;\n }\n};", "memory": "11000" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n vector<int>ans;\n for(int i=0; i<arr.size(); i++){\n int cnt = 1;\n while(i+1 < arr.size() && arr[i] == arr[i+1]){\n cnt++;\n i++;\n }\n ans.push_back(cnt);\n }\n sort(ans.begin(),ans.end());\n for(int i=0; i<ans.size()-1; i++){\n if(ans[i] == ans[i+1]){\n return false;\n }\n }\n return true;\n }\n};", "memory": "11000" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n map<int, int> m;\n for(int value : arr) {\n if (m.find(value) == m.cend()) {\n m.insert({value, 1});\n } else {\n m[value] = m[value] + 1;\n }\n }\n\n vector<int> counts;\n for (auto it : m) {\n counts.push_back(it.second);\n } \n\n for (auto it : m) {\n if (count(counts.cbegin(), counts.cend(), it.second) != 1) {\n return false;\n }\n }\n\n return true;\n\n }\n};", "memory": "11100" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n // unordered_map\n map<int,int> m;\n for (int i = 0; i < arr.size(); i++) {\n if (m.find(arr[i]) != m.end()) {\n m[arr[i]] += 1; \n } else {\n m[arr[i]] = 1;\n }\n }\n map<int, int>::iterator it;\n set<int> s;\n for (it = m.begin(); it != m.end(); it++)\n {\n if (s.find(it->second) != s.end()) {\n return false;\n }\n s.insert(it->second);\n }\n return true;\n }\n};", "memory": "11200" }
1,319
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,2,2,1,1,3] <strong>Output:</strong> true <strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [1,2] <strong>Output:</strong> false </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0] <strong>Output:</strong> true </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length &lt;= 1000</code></li> <li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n unordered_map<int, int> counts;\n\n for (const auto& num : arr) {\n counts[num] += 1;\n }\n \n unordered_set<int> uniqueCounts;\n for (const auto& [val, count] : counts) {\n if (uniqueCounts.find(count) != uniqueCounts.end()) {\n return false;\n }\n uniqueCounts.insert(count);\n }\n\n return true;\n }\n};", "memory": "11300" }