Exclusive Time of Functions in C++

C++Server Side ProgrammingProgramming

Suppose on a single threaded CPU, we execute some functions. Now each function has a unique id between 0 and N-1. We will store logs in timestamp order that describe when a function is entered or exited.

Here each log is a string written this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, if the string is like "0:start:3" this means that the function with id 0 started at the beginning of timestamp 3. "1:end:2" means the function with id 1 ended at the end of timestamp 2. A function's exclusive time is the number of units of time spent in this function.

So if the input is like n = 2 and logs = ["0:start:0","1:start:2","1:end:5","0:end:6"], then the output will be [3,4]. This is because function 0 starts at the beginning of time 0, then it executes 2 units of time and reaches the end of time 1. After that function 1 starts at the beginning of time 2, executes 4 units of time and ends at time 5. The function 0 is running again at the beginning of time 6, and also ends at the end of time 6, thus executing for 1 unit of time. So we can see that the function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.

To solve this, we will follow these steps −

  • Define an array ret of size n, define stack st

  • j := 0, prev := 0

  • for i in range 0 to size of log array – 1

    • temp := logs[i], j := 0, id := 0, num := 0, type := empty string

  • while temp[j] is not a colon

    • id := id * 10 + temp[j] as number

    • increase j by 1

  • increase j by 1

  • while temp[j] is not a colon

    • type := type concatenate temp[j]

    • increase j by 1

  • increase j by 1

  • while j < size of temp

    • num := num * 10 + temp[j] as number

    • increase j by 1

  • if type = start, then

    • if st is not empty

      • increase ret[stack top element] by num – prev

    • insert d into st, prev := num

  • otherwise

    • x := top of st, and delete top of stack

    • ret[x] := ret[x] + (num + 1) – prev

    • prev := num + 1

  • return ret

Example(C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   vector<int> exclusiveTime(int n, vector<string>& logs) {
      vector <int> ret(n);
      stack <int> st;
      int id, num;
      int j = 0;
      string temp;
      string type;
      int prev = 0;
      for(int i = 0; i < logs.size(); i++){
         temp = logs[i];
         j = 0;
         id = 0;
         num = 0;
         type = "";
         while(temp[j] != ':'){
            id = id * 10 + (temp[j] - '0');
            j++;
         }
         j++;
         while(temp[j] != ':'){
            type += temp[j];
            j++;
         }
         j++;
         while(j < temp.size()){
            num = num * 10 + temp[j] - '0';
            j++;
         }
         if(type == "start"){
            if(!st.empty()){
               ret[st.top()] += num - prev;
            }
            st.push(id);
            prev = num;
         } else {
            int x = st.top();
            st.pop();
            ret[x] += (num + 1) - prev;
            prev = num + 1;
         }
      }
      return ret;
   }
};
main(){
   vector<string> v = {"0:start:0","1:start:2","1:end:5","0:end:6"};
   Solution ob;
   print_vector(ob.exclusiveTime(2, v));
}

Input

2
["0:start:0","1:start:2","1:end:5","0:end:6"]

Output

[3, 4, ]
raja
Updated on 02-May-2020 08:48:11

Advertisements