forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stack] Add a solution to Exclusive Time of Functions
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
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
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
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 | ||
} | ||
} |