FastPrepFastPrep
Problem Brief

Compute Maximum Scheduled Tasks

NEW GRADOA

At a high-paced analytics platform, efficiently scheduling tasks within designated time constraints is crucial. The system consists of n mandatory tasks and n optional tasks. Two arrays, mandatoryTasks and optionalTasks, specify the duration in hours of each task. Here, mandatoryTasks[i] indicates the time required for the i-th mandatory task, while optionalTasks[i] represents the duration of the i-th optional task.

Each workday has a strict limit of maxDailyHours, within which tasks must be scheduled. It is required that exactly one mandatory task is scheduled each day. If time permits, at most one optional task can also be included on the same day. The total time spent on a day cannot exceed maxDailyHours. The challenge is to determine the highest number of optional tasks that can be scheduled over the course of n days while adhering to these constraints.

Function Description

Complete the function computeMaxScheduledTasks in the editor below.

computeMaxScheduledTasks has the following parameters:

  1. 1. int maxDailyHours: the maximum number of hours available each day.
  2. 2. int mandatoryTasks[n]: an array representing the duration of mandatory tasks.
  3. 3. int optionalTasks[n]: an array representing the duration of optional tasks.

Returns

int: the highest number of optional tasks that can be completed within the given constraints.

1Example 1

Input
maxDailyHours = 7, mandatoryTasks = [4, 5, 2, 4], optionalTasks = [5, 6, 3, 4]
Output
2
Explanation

Day 1: Schedule the first mandatory task (4 hours) and the third optional task (3 hours). Total: 4 + 3 = 7 hours.

Day 2: Schedule the second mandatory task (5 hours). No optional task can be added. Total: 5 hours.

Day 3: Schedule the third mandatory task (2 hours) and the first optional task (5 hours). Total: 2 + 5 = 7 hours.

Day 4: Schedule the fourth mandatory task (4 hours). No optional task can be added. Total: 4 hours.

After optimizing task scheduling, the maximum number of optional tasks that can be included is 2.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n, maxDailyHours ≤ 2*10^5
  • 1 ≤ mandatoryTasks[i], optionalTasks[i] ≤ maxDailyHours
public int computeMaxScheduledTasks(int maxDailyHours, int[] mandatoryTasks, int[] optionalTasks) {
  // write your code here
}
Input

maxDailyHours

7

mandatoryTasks

[4, 5, 2, 4]

optionalTasks

[5, 6, 3, 4]

Output

2

Sign in to submit your solution.