Exclusive Time of Functions


  • 0

    Click here to see the full article post


  • 1
    L

    I hope LeetCode should do some video solution instead of text, which hackerrank and geeksforgeeks are very good examples. As you know, sometimes the long text is boring to read.


  • 0
    D

    nice article, very detailed


  • 0
    A

    "Approach #1 Using Stack [Time Limit Exceeded]" - looks misleading. The accepted solution is stack-based. Any approach can be misused. When you count using fingers instead of just adding/subtracting two numbers, you're doing just that - wasting time. You could have added Thread.Sleep(1000) as well.


  • 0
    U

    "..pushing the first function's id from the given logslogs list onto the array." i think it meant stack??


  • 0
    K

    Share my concise solution:

    public int[] exclusiveTime(int n, List<String> logs) {
        int[] res = new int[n];
        Stack<int[]> stack = new Stack<>();
        
        for(String log : logs) {
            String[] s = log.split(":");
            int idx = Integer.valueOf(s[0]);
            int t = Integer.valueOf(s[2]);
            
            if(s[1].equals("start")) {
                stack.push(new int[]{idx, t});
            } else {
                int v = t-stack.pop()[1]+1;
                res[idx] += v;
                if(!stack.empty()) res[stack.peek()[0]] -= v;
            }
        }
        return res;
    }

  • 0
    A

    Why even mention the first solution? Is there something I'm missing? I'm confused when it is ever an effective strategy to have a while loop to increment count 1 by 1 when going from some number i to j where i < j?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.