Skip to content

Instantly share code, notes, and snippets.

@cc2011
Created August 18, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cc2011/c3627429b3a4d0cbad7893259d18629b to your computer and use it in GitHub Desktop.
Save cc2011/c3627429b3a4d0cbad7893259d18629b to your computer and use it in GitHub Desktop.
636. Exclusive Time of Functions
=====================
====== ======
public class Solution {
class Pair {
int time;
int id;
boolean isStart;
int occupy;
Pair(int t, int i, boolean s){
time = t;
id = i;
isStart = s;
occupy = 0;
}
}
public int[] exclusiveTime(int n, List<String> logs) {
Pair[] pair = new Pair[logs.size()];
for (int i = 0; i < logs.size(); i++) {
String[] str = logs.get(i).split(":");
pair[i] = new Pair(Integer.parseInt(str[2]), Integer.parseInt(str[0]), str[1].equals("start"));
}
Stack<Pair> stack = new Stack<>();
int[] res = new int[n];
for (Pair currP : pair) {
if(currP.isStart) {
stack.push(currP);
} else {
Pair popP = stack.pop();
int time = currP.time - popP.time +1;
int actualTime = time - popP.occupy;
if(!stack.isEmpty())
stack.peek().occupy += time;
res[popP.id] += actualTime;//
}
}
return res;
}
}
["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
0 7
====== ==
====|==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment