Created
August 18, 2017 08:03
-
-
Save cc2011/c3627429b3a4d0cbad7893259d18629b to your computer and use it in GitHub Desktop.
636. Exclusive Time of Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===================== | |
====== ====== | |
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