Skip to content

Commit

Permalink
[Stack] Add a solution to Exclusive Time of Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jan 1, 2020
1 parent 7efda15 commit 0550b87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[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.
[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.

## Contributors

Expand Down Expand Up @@ -164,6 +164,7 @@
[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [Swift](./Stack/ValidParentheses.swift)| Easy| O(n)| O(n)|
[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Swift](./Stack/LongestValidParentheses.swift)| Hard| O(n)| O(n)|
[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Swift](./Stack/EvaluateReversePolishNotation.swift)| Medium| O(n)| O(n)|
[Exclusive Time of Functions](https://leetcode.com/problems/exclusive-time-of-functions/)| [Swift](./Stack/ExclusiveTimeFunctions.swift) | Medium| O(n)| O(n)|
[Simplify Path](https://leetcode.com/problems/simplify-path/)| [Swift](./Stack/SimplifyPath.swift)| Medium| O(n)| O(n)|
[Remove K Digits](https://leetcode.com/problems/remove-k-digits/)| [Swift](./Stack/RemoveKDigits.swift)| Medium| O(n)| O(n)|
[Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/)| [Swift](./Stack/TernaryExpressionParser.swift)| Medium| O(n)| O(n)|
Expand Down
36 changes: 36 additions & 0 deletions Stack/ExclusiveTimeFunctions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Question Link: https://leetcode.com/problems/exclusive-time-of-functions/
* Primary idea: Use a stack to keep task start time, update it when a new task starts.
* Time Complexity: O(n), Space Complexity: O(n)
*/

class ExclusiveTimeFunctions {
func exclusiveTime(_ n: Int, _ logs: [String]) -> [Int] {
var stack = [(Int, Int)](), res = Array(repeating: 0, count: n)

for log in logs {
// parse log
let logInfo = log.components(separatedBy: ":")
let id = Int(logInfo[0])!, isStart = logInfo[1] == "start", time = Int(logInfo[2])!

if isStart {
if let last = stack.last {
res[last.0] += time - last.1
}

stack.append((id, time))
} else {
let startTime = stack.removeLast().1

res[id] += time - startTime + 1

if var last = stack.last {
last.1 = time + 1
stack[stack.count - 1] = last
}
}
}

return res
}
}

0 comments on commit 0550b87

Please sign in to comment.