One-pass solution.

During iterating through the input logs, there are 4 possible conditions:

  1. 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.
  2. 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.
  3. 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.
  4. 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;}
}

LeetCode636. Exclusive Time of Functions相关推荐

  1. LeetCode 636 Exclusive Time of Functions

    LeetCode 636 Exclusive Time of Functions Problem Description: 记录每一函数运行时间,其中需要注意到当前函数start开始时,上一个函数可能 ...

  2. leetcode 636. Exclusive Time of Functions | 636. 函数的独占时间(Stack)

    题目 https://leetcode.com/problems/exclusive-time-of-functions/ 题解 类似于括号匹配问题,遍历 list,每一次来到新元素时,结算当前正在执 ...

  3. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  4. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  6. LinkedIn TAG

    1 [leetcode]243. Shortest Word Distance最短单词距离 Two Pointers 2 [leetcode]244. Shortest Word Distance I ...

  7. [译]5.11. Functions and Variables Featured in This Chapter 本章的函数,变量和特性

    目录:http://www.cnblogs.com/WuCountry/archive/2008/11/15/1333960.html   [不提供插图,读者最好从网上下载源书] 5.11. Func ...

  8. UE3 Light Functions (光照函数)

    Light Functions (光照函数) Light Functions (光照函数) 概述 Decals(贴花) vs 光照函数 可以使用哪些材质表达式? Texture 2D(二维贴图) Te ...

  9. 读《Evolving Algebraic Constructions for Designing Bent Boolean Functions》

    Picek, S., Jakobovic, D.: Evolving algebraic constructions for designing bent Boolean functions. In: ...

最新文章

  1. Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】
  2. hdu_1861_游船出租_201402282130
  3. 【LeetCode 总结】Leetcode 题型分类总结、索引与常用接口函数
  4. java opencv orb_opencv python ORB算法
  5. 常见问题_自动打螺丝机常见问题及解决办法
  6. 阅读一定时间后获得实现逻辑_大家都在好奇,18天读18本书的60分钟高效阅读,怎么这么火...
  7. Flask 扩展 Flask-Script
  8. 开源 java CMS - FreeCMS2.8 数据对象 site
  9. 【AudioVideo】MediaRecorder概述(21)
  10. jQuery插件编写,
  11. web.config中namespace的配置(针对页面中引用)
  12. vue http get html模板,怎么用vue.js做异步请求?
  13. 华为hcip认证题库在哪找?HCIP如何考取?
  14. 可以自我学习的AI五子棋
  15. mysql中使用触发器实例
  16. MacOs提升效率必备软件
  17. Android学习别“走弯路”,移动端混合开发框架
  18. Python拉勾网爬虫实现
  19. pip 安装库出错:Defaulting to user installation because normal site-packages is not writeable
  20. Architect架构师简历模板

热门文章

  1. Java中获取时间戳的方式
  2. odd在python_python odd
  3. ubuntu重启快捷键
  4. 微信小程序-贪吃蛇关键代码
  5. 写给那些傻傻想做服务器开发的朋友
  6. Android4.0之后系统通知栏icon显示不全问题
  7. Java学生成绩管理系统(含源码+论文+答辩PPT等)
  8. golang操作excel两种excelize包对比
  9. 大家一起快乐的学习电脑绝技吧
  10. jsonready onload 与_【前端面试合集】面试题向三