Skip to content

Commit 0550b87

Browse files
committed
[Stack] Add a solution to Exclusive Time of Functions
1 parent 7efda15 commit 0550b87

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 315 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 316 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -164,6 +164,7 @@
164164
[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [Swift](./Stack/ValidParentheses.swift)| Easy| O(n)| O(n)|
165165
[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Swift](./Stack/LongestValidParentheses.swift)| Hard| O(n)| O(n)|
166166
[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Swift](./Stack/EvaluateReversePolishNotation.swift)| Medium| O(n)| O(n)|
167+
[Exclusive Time of Functions](https://leetcode.com/problems/exclusive-time-of-functions/)| [Swift](./Stack/ExclusiveTimeFunctions.swift) | Medium| O(n)| O(n)|
167168
[Simplify Path](https://leetcode.com/problems/simplify-path/)| [Swift](./Stack/SimplifyPath.swift)| Medium| O(n)| O(n)|
168169
[Remove K Digits](https://leetcode.com/problems/remove-k-digits/)| [Swift](./Stack/RemoveKDigits.swift)| Medium| O(n)| O(n)|
169170
[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/)| [Swift](./Stack/TernaryExpressionParser.swift)| Medium| O(n)| O(n)|

Stack/ExclusiveTimeFunctions.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/exclusive-time-of-functions/
3+
* Primary idea: Use a stack to keep task start time, update it when a new task starts.
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*/
6+
7+
class ExclusiveTimeFunctions {
8+
func exclusiveTime(_ n: Int, _ logs: [String]) -> [Int] {
9+
var stack = [(Int, Int)](), res = Array(repeating: 0, count: n)
10+
11+
for log in logs {
12+
// parse log
13+
let logInfo = log.components(separatedBy: ":")
14+
let id = Int(logInfo[0])!, isStart = logInfo[1] == "start", time = Int(logInfo[2])!
15+
16+
if isStart {
17+
if let last = stack.last {
18+
res[last.0] += time - last.1
19+
}
20+
21+
stack.append((id, time))
22+
} else {
23+
let startTime = stack.removeLast().1
24+
25+
res[id] += time - startTime + 1
26+
27+
if var last = stack.last {
28+
last.1 = time + 1
29+
stack[stack.count - 1] = last
30+
}
31+
}
32+
}
33+
34+
return res
35+
}
36+
}

0 commit comments

Comments
 (0)