LeetCode 1767: Find the Subtasks That Did Not Execute Solution

Master LeetCode problem 1767 (Find the Subtasks That Did Not Execute), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1767. Find the Subtasks That Did Not Execute

Problem Explanation

Explanation:

To solve this problem, we need to find the subtasks that did not execute given the information about the tasks that were executed in a specific order. We can use a stack to keep track of the tasks that are currently executing. While iterating through the executed tasks, we can simulate the execution and handle different cases accordingly.

  1. Initialize an empty stack to simulate the execution of tasks.
  2. Iterate through the executed tasks:
    • If the task is not a subtask, push it to the stack.
    • If the task is a subtask and the stack is not empty:
      • Pop tasks from the stack until we find a matching subtask or the stack becomes empty.
      • If a matching subtask is found, continue to the next executed task.
      • Otherwise, the subtask did not execute, so add it to the result.
  3. After iterating through all executed tasks, the remaining tasks in the stack are the subtasks that did not execute. :

Solution Code

class Solution {
    public List<Integer> findSubtasks(int[] executedTasks, int[] subtasks) {
        Stack<Integer> stack = new Stack<>();
        List<Integer> result = new ArrayList<>();
        
        int subIndex = 0;
        for (int task : executedTasks) {
            if (subIndex < subtasks.length && task == subtasks[subIndex]) {
                while (!stack.isEmpty() && stack.peek() != subtasks[subIndex]) {
                    result.add(stack.pop());
                }
                if (!stack.isEmpty()) {
                    stack.pop();
                }
                subIndex++;
            } else {
                stack.push(task);
            }
        }
        
        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1767 (Find the Subtasks That Did Not Execute)?

This page provides optimized solutions for LeetCode problem 1767 (Find the Subtasks That Did Not Execute) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1767 (Find the Subtasks That Did Not Execute)?

The time complexity for LeetCode 1767 (Find the Subtasks That Did Not Execute) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1767 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1767 in Java, C++, or Python.

Back to LeetCode Solutions