LeetCode636. Exclusive Time of Functions
来源:互联网 发布:python ide推荐 编辑:程序博客网 时间:2024/06/07 17:16
One-pass solution.
During iterating through the input logs, there are 4 possible conditions:
- Previous log is “start”, current log is “start”. This means that either the previous function is calling some other function or it’s calling itself recursively. However, it makes no difference for those 2 cases, we just add the time gap between these 2 start to the total exclusive time of previous function.
- Previous log is “start”, current log is “end”. This means that current function runs to an end and haven’t call any other functions including itself. In this case we simply add this time gap to the total exclusive time of current function. Note this have a different calcuiating method, which is
endTime - startTime + 1
. - Previous log is “end”, current log is “start”. This means that a previous function call is completed and the original function is running and is calling another function. Thus this time gap should be added to the original outer function that performs the call. However, there is another case for this as well. If there is no function performs the call, that means during this time gap the CPU is idle, and we should just ignore this time gap.
- Previous log is “end”, current log is “end”. This means that a previous function call is end, and the function that performs the call is completed as well. Thus this time gap should be added to the outer function that performs the call.
Summarize them up, we get the following solution.
This algorithm runs in O(n)
time, O(n)
space worst case for length-n input.
class Solution { public int[] exclusiveTime(int n, List<String> logs) { int[] ret = new int[n]; Deque<Integer> deque = new ArrayDeque<>(); String[] log = logs.get(0).split(":"); deque.push(Integer.parseInt(log[0])); int prevTime = Integer.parseInt(log[2]); for (int i = 0; i < logs.size(); i++) { log = logs.get(i).split(":"); int time = Integer.parseInt(log[2]); if (log[1].equals("start")) { if (!deque.isEmpty()) { ret[deque.peek()] += (time - prevTime); } prevTime = time; deque.push(Integer.parseInt(log[0])); } else { // "end" ret[deque.pop()] += (time - prevTime + 1); prevTime = time + 1; } } return ret; }}
阅读全文
0 0
- LeetCode636. Exclusive Time of Functions
- 636. Exclusive Time of Functions
- 636. Exclusive Time of Functions
- [leetcode]636. Exclusive Time of Functions
- [LeetCode]636. Exclusive Time of Functions
- [LeetCode] 636. Exclusive Time of Functions
- leetcode 636. Exclusive Time of Functions
- Exclusive Time of Functions问题及解法
- LeetCode.636 Exclusive Time of Functions
- LeetCode 636 : Exclusive Time of Functions(python)
- leetcode 636. Exclusive Time of Functions 函数的单独运行时间
- Top 10 custom JavaScript functions of all time
- Windows MFC Time Functions
- Functions about time
- Some functions about time
- Compile-time Functions
- mysql 5.6 Date/Time Functions
- List of C functions
- nginx 编译安装
- 关于技术提升的一点思考
- centOS下安装SVN服务端
- Tag
- 【性能测试】-VUGen生成脚本自动保存时,未到100%闪退问题解决方案
- LeetCode636. Exclusive Time of Functions
- Linux中如何安装.rpm、.tar、.tar.…
- STL总结
- SecureCRT rz 上传文件失败问题
- google的zxing实现二维码生成和解…
- android Timer TimerTask 启动与停止及常见无法停止问题解决
- TCP与UDP区别
- Java中多线程join方法介绍与用法
- Java中Runnable和Thread的区别