Similar Problems

Similar Problems not available

The Number Of Full Rounds You Have Played - Leetcode Solution

Companies:

LeetCode:  The Number Of Full Rounds You Have Played Leetcode Solution

Difficulty: Medium

Topics: math string  

Problem Statement:

You are playing a video game where you are competing in a series of rounds. Each round has a fixed length of time, given as a positive integer, roundTime. During a round, you have to complete a fixed number of tasks, given as a positive integer, numTasks. You receive a score for each task you complete correctly, which is calculated as (roundTime - timeTaken) * scorePerTask, where timeTaken is the amount of time you took to complete that task and scorePerTask is the score you receive for one correct completion of a task. If you complete all numTasks tasks before the end of the round time, any remaining time is added to your score as a bonus.

You play a total of n rounds, where the round times and number of tasks for each round are given in two arrays, startTime and finishTime, and startTask and finishTask, respectively. More formally, for each i (0-indexed), you play the ith round for finishTime[i] - startTime[i] seconds and must complete finishTask[i] - startTask[i] tasks. You have a total of totalTime seconds to play all n rounds in any order you choose. Determine the maximum total score you can achieve if you play optimally.

Assumptions:

  1. 1 ≤ n ≤ 10^4
  2. 1 ≤ totalTime ≤ 10^9
  3. 1 ≤ startTime[i] ≤ finishTime[i] ≤ 10^9
  4. 1 ≤ startTask[i] ≤ finishTask[i] ≤ 10^4
  5. 1 ≤ scorePerTask ≤ 100

Example:

Input:

startTime = [1,2,3,4,5] finishTime = [6,7,8,9,10] startTask = [1,2,3,4,5] finishTask = [5,5,5,5,5] scorePerTask = 1 totalTime = 15

Output: 21

Explanation:

You can choose to play the rounds in any order. One optimal strategy is:

  1. Play round 0 (start time 1, finish time 6) and complete all 5 tasks. You score 4 * 1 + 3 * 1 + 2 * 1 + 1 * 1 + (6 - 5) * 1 = 15.
  2. Play round 2 (start time 3, finish time 8) and complete all 5 tasks. You score 5 * 1 + 4 * 1 + 3 * 1 + 2 * 1 + 1 * 1 + (8 - 7) * 1 = 20.
  3. Play round 4 (start time 5, finish time 10) and complete all 5 tasks. You score 5 * 1 + 4 * 1 + 3 * 1 + 2 * 1 + 1 * 1 + (10 - 9) * 1 = 21. The maximum total score is 15 + 20 + 21 = 56.

Solution:

The problem requires us to play optimally, which means we have to maximize the total score we can achieve. To achieve this, we can follow the below steps:

Step 1: Parse the input and calculate the time taken to complete one task for each round.

Step 2: Sort the rounds based on the start time.

Step 3: Play each round in order of their start time.

Step 4: If we have enough time to complete all tasks in a round, complete them and add the bonus time to the score.

Step 5: If we don't have enough time to complete all tasks in a round, complete as many tasks as possible and move to the next round.

Step 6: Repeat steps 4 to 5 until we run out of time or we complete all rounds.

Step 7: Return the maximum total score achieved.

Let's implement the above algorithm in Python:

def numberOfRounds(start_time, finish_time, start_task, finish_task, score_per_task, total_time): n = len(start_time) time_per_task = [float(finish_time[i] - start_time[i]) / (finish_task[i] - start_task[i]) for i in range(n)] rounds = sorted(zip(start_time, finish_time, start_task, finish_task, time_per_task), key=lambda x: x[0]) i = score = 0 while i < n and total_time >= rounds[i][0]: t1, t2, s1, s2, tpt = rounds[i] tasks = min(finish_task[i], int((total_time - t1) / tpt + 1e-9), s2) - max(start_task[i], s1) if tasks > 0: score += tasks * (total_time - int(t1 + tasks * tpt + 1e-9)) * score_per_task + (total_time - (t1 + tasks * tpt)) total_time -= int(t1 + tasks * tpt + 1e-9) else: i += 1 return score

We start by calculating the time taken to complete one task for each round in Step 1. We do this by dividing the time taken for a round by the number of tasks in that round.

Next, we sort the rounds based on the start time in Step 2.

In Step 3, we loop over the rounds in order of their start time. We keep playing each round until we run out of time or we complete all rounds.

In Step 4, we check if we have enough time to complete all tasks in a round. If so, we complete all tasks and add the bonus time to the score. We calculate the bonus time by subtracting the time taken to complete all tasks from the round time.

In Step 5, if we don't have enough time to complete all tasks in a round, we complete as many tasks as possible and move to the next round. We calculate the number of tasks we can complete by dividing the remaining time by the time taken to complete one task in that round.

In Step 6, we repeat Steps 4 and 5 until we run out of time or we complete all rounds.

Finally, in Step 7, we return the maximum total score achieved.

The time complexity of this solution is O(n log n), where n is the number of rounds, due to the sorting operation in Step 2. The space complexity is O(n), which is the space required to store the time taken to complete one task for each round.

The Number Of Full Rounds You Have Played Solution Code

1