-
Notifications
You must be signed in to change notification settings - Fork 115
/
0636-ExclusiveTimeOfFunctions.cs
42 lines (38 loc) · 1.2 KB
/
0636-ExclusiveTimeOfFunctions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//-----------------------------------------------------------------------------
// Runtime: 272ms
// Memory Usage: 33 MB
// Link: https://leetcode.com/submissions/detail/372042707/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0636_ExclusiveTimeOfFunctions
{
public int[] ExclusiveTime(int n, IList<string> logs)
{
var stack = new Stack<int>();
var result = new int[n];
var start = 0;
foreach (var log in logs)
{
var str = log.Split(':');
var id = int.Parse(str[0]);
var time = int.Parse(str[2]);
if (str[1] == "start")
{
if (stack.Count > 0)
result[stack.Peek()] += time - start;
stack.Push(id);
start = time;
}
else
{
result[stack.Peek()] += time - start + 1;
stack.Pop();
start = time + 1;
}
}
return result;
}
}
}