2365. Task Scheduler II LeetCode Solution

In this guide, you will get 2365. Task Scheduler II LeetCode Solution with the best time and space complexity. The solution to Task Scheduler II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Task Scheduler II solution in C++
  4. Task Scheduler II solution in Java
  5. Task Scheduler II solution in Python
  6. Additional Resources
2365. Task Scheduler II LeetCode Solution image

Problem Statement of Task Scheduler II

You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task.
You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed.
Each day, until all tasks have been completed, you must either:

Complete the next task from tasks, or
Take a break.

Return the minimum number of days needed to complete all tasks.

Example 1:

Input: tasks = [1,2,1,2,3,1], space = 3
Output: 9
Explanation:
One way to complete all tasks in 9 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
Day 7: Take a break.
Day 8: Complete the 4th task.
Day 9: Complete the 5th task.
It can be shown that the tasks cannot be completed in less than 9 days.

See also  1385. Find the Distance Value Between Two Arrays LeetCode Solution

Example 2:

Input: tasks = [5,8,8,5], space = 2
Output: 6
Explanation:
One way to complete all tasks in 6 days is as follows:
Day 1: Complete the 0th task.
Day 2: Complete the 1st task.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd task.
Day 6: Complete the 3rd task.
It can be shown that the tasks cannot be completed in less than 6 days.

Constraints:

1 <= tasks.length <= 105
1 <= tasks[i] <= 109
1 <= space <= tasks.length

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2365. Task Scheduler II LeetCode Solution in C++

class Solution {
 public:
  long long taskSchedulerII(vector<int>& tasks, int space) {
    unordered_map<int, long> taskToNextAvailable;
    long ans = 0;

    for (const int task : tasks) {
      ans = max(ans + 1, taskToNextAvailable[task]);
      taskToNextAvailable[task] = ans + space + 1;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2365. Task Scheduler II LeetCode Solution in Java

class Solution {
  public long taskSchedulerII(int[] tasks, int space) {
    Map<Integer, Long> taskToNextAvailable = new HashMap<>();
    long ans = 0;

    for (final int task : tasks) {
      ans = Math.max(ans + 1, taskToNextAvailable.getOrDefault(task, 0L));
      taskToNextAvailable.put(task, ans + space + 1);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2365. Task Scheduler II LeetCode Solution in Python

class Solution:
  def taskSchedulerII(self, tasks: list[int], space: int) -> int:
    taskToNextAvailable = collections.defaultdict(int)
    ans = 0

    for task in tasks:
      ans = max(ans + 1, taskToNextAvailable[task])
      taskToNextAvailable[task] = ans + space + 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.