Process Execution Time [Shigo Friend Test]
Look in Telegramunknown
c_cpp
a year ago
1.8 kB
6
Indexable
// THINK: A process can start and end at the same time? vector<int> getTotalExecutionTime(int n, vector<string> logs) { vector<int> startTime(n), endTime(n); for (string &s: logs) { string temp, temp2; // parse start vector for (char c: s) { if (c == ':') break; temp.push_back(c); } // parse end vector for (auto it = s.rbegin(); it != s.rend(); it++) { if (*it == ':') break; temp2.push_back(*it); } int processNo = stoi(temp); reverse(temp.begin(), temp.end()); // int processTime = stoi(temp2); int start_time, end_time; if (s.contains("start")) { start_time = stoi(temp2); startTime[processNo] = start_time; } if (s.contains("end")) { end_time = stoi(temp2); endTime[processNo] = end_time; } } vector<int> ans(n); int maxTime = 3e3 + 1; for (int currTime = 0, runningProcess = -1; currTime < maxTime; currTime++) { bool flag = false; // search in start time for (int i = 0; i < n; ++i) if (startTime[i] == currTime) runningProcess = i; // search in end time for (int i = 0; i < n; ++i) if (endTime[i] == currTime) flag = true; if (runningProcess >= 0 && runningProcess < n) ans[runningProcess]++; if (flag) { flag = false; runningProcess = -1; } } return ans; }
Editor is loading...
Leave a Comment