Monday, September 23, 2019

Leetcode 1203: Sort Items by Groups Respecting Dependencies

https://leetcode.com/problems/sort-items-by-groups-respecting-dependencies/description/



Notes:

This question is a hard question, due to its complicated logic. It is mainly about the topological sort algorithm.

First of all, there are more than one groups. And we need to run a topological sort to all the groups, to make sure they can be arranged;

Second, inside each group, we also need to run a topological sort, to make sure that they also can be arranged in the required orders.

The topological sort is a typical algorithm, which usually requires the indgrees of each note. Firstly we will start the sort with the node having a indgree of 0 which means there is no prerequisite. During visiting or sorting, we can gradually decrease the indgree of the unvisited node when their pre-nodes are visited. Once its indegree becomes 0, it can ready to be visited.

Please check the code below:

  1. class Solution {
  2. public:
  3.     vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {
  4.         vector<int> res;
  5.         unordered_map<int, set<int>> gs;//group structures;
  6.         unordered_map<int, set<int>> group_graph;//group relations
  7.         unordered_map<int, int> group_indgree;//group indgrees;
  8.         unordered_map<int, set<int>> memb_graph;//member relations;
  9.         unordered_map<int, int> memb_indgree;//memeber indgree;
  10.        
  11.         int num_group = m;
  12.         for(int i=0; i<group.size(); ++i) {
  13.             if(group[i] == -1) {//listed as one unique group
  14.                 group[i] = num_group++;
  15.             }
  16.             gs[group[i]].insert(i);
  17.         }
  18.        
  19.         for(int to=0; to<beforeItems.size(); ++to) {
  20.             int to_group = group[to];
  21.             for(auto &from : beforeItems[to]) {
  22.                 int from_group = group[from];
  23.                 if(from_group == to_group) {//belong to the same sub-group;
  24.                 if(!memb_graph[from].count(to)) {
  25.                     memb_graph[from].insert(to);
  26.                     ++memb_indgree[to];
  27.     }
  28.                 }
  29.                 else {
  30.     if(!group_graph[from_group].count(to_group)) {
  31.                     group_graph[from_group].insert(to_group);
  32.                     ++group_indgree[to_group];
  33.     }
  34.                 }
  35.             }
  36.         }
  37.         vector<int> group_res;
  38.         queue<int> q;
  39.         for(int i=0; i<num_group; ++i) {
  40.             if(group_indgree[i] == 0) {
  41.                 q.push(i);
  42.                 group_res.push_back(i);
  43.             }
  44.         }
  45.         while(q.size()) {
  46.             int t = q.front();
  47.             q.pop();
  48.             for(auto &a : group_graph[t]) {
  49.                 if(group_indgree[a] == 0) continue;
  50.                 --group_indgree[a];
  51.                 if(group_indgree[a] == 0) {
  52.                     q.push(a);
  53.                     group_res.push_back(a);
  54.                 }
  55.             }
  56.         }
  57.         if(group_res.size() != num_group) return {};
  58.      
  59.         for(auto &a : group_res) {//check each group
  60.             int ct = 0;
  61.             for(auto &b : gs[a]) {
  62.                 if(memb_indgree[b] == 0) {
  63.                     q.push(b);
  64.                     res.push_back(b);
  65.                     ++ct;
  66.                 }
  67.             }
  68.             while(q.size()) {
  69.                 auto t = q.front();
  70.                 q.pop();
  71.                 for(auto &c : memb_graph[t]) {
  72.                     if(memb_indgree[c] == 0) continue;
  73.                     --memb_indgree[c];
  74.                     if(memb_indgree[c] == 0) {
  75.                         q.push(c);
  76.                         res.push_back(c);
  77.                         ++ct;
  78.                     }
  79.                 }
  80.             }
  81.             if(ct != gs[a].size()) return {};
  82.         }
  83.         return res;
  84.     }
  85. };

No comments:

Post a Comment