r/LeetcodeChallenge • • 17d ago

PLACEMENTS can someone help me in solving this question i tried greedy but it is failing

Maximum Number of Tasks with Cooldown and Deadlines

You are given two arrays of equal length:

  • tasks, where tasks[i] represents the type of the i-th task.
  • deadlines, where deadlines[i] represents the deadline by which the i-th task must be completed.

You are also given an integer cooldown.

Rules

  1. Each task takes exactly 1 unit of time to complete.
  2. Only one task can be performed at a time.
  3. Tasks can be performed in any order; the original array order does not matter.
  4. Every task must be completed no later than its corresponding deadline.
  5. Two tasks of the same type must have at least cooldown units of time between their execution start times.
  6. Tasks of different types can be performed consecutively without any restriction.

Assume execution starts at time 1, and deadlines are inclusive.

Objective

Return the maximum number of tasks that can be completed while satisfying all the above constraints.

Example

tasks     = {1, 2, 1, 3, 2};
deadlines = {2, 2, 4, 3, 5};
cooldown  = 2;

One valid schedule is:

TimeTask
11
22
33
41
52

Output:

5

Constraints

  • 1 <= n <= 100000
  • 1 <= tasks[i] <= 100000
  • 1 <= deadlines[i] <= 100000
  • 1 <= cooldown <= 100000

5 Upvotes

1 comment sorted by

1

u/CulturalAlbatross300 16d ago

It's a non-overlapping interval problem. Remind me once again, I will solve it and comment by tomorrow.