Similar Problems

Similar Problems not available

Calculate Delayed Arrival Time - Leetcode Solution

Companies:

LeetCode:  Calculate Delayed Arrival Time Leetcode Solution

Difficulty: Easy

Topics: math  

The problem statement on LeetCode is as follows:

You are given an array of positive integers representing the arrival times of a taxi queue. The taxis are numbered from 1 to n. Your task is to calculate the delayed arrival time for each taxi. A taxi is considered delayed if its arrival time is after the expected arrival time, which is equal to the sum of the arrival times of all taxis which arrived before it. The delayed arrival time of a taxi is the difference between its actual and expected arrival times.

Write a function that takes in an array of positive integers times and returns an array of integers representing the delayed arrival times of the taxis.

Note: The expected arrival time of the first taxi is always 0.

Example:

Input: times = [1, 3, 3, 5, 7] Output: [0, 0, 1, 2, 7]

Explanation:

  • Taxi 1 arrives at time 1 and is on time (expected = 0)
  • Taxi 2 arrives at time 3 and is on time (expected = 1 + 0 = 1)
  • Taxi 3 arrives at time 3 and is delayed by 1 minute (expected = 1 + 3 = 4)
  • Taxi 4 arrives at time 5 and is delayed by 2 minutes (expected = 1 + 3 + 3 = 7)
  • Taxi 5 arrives at time 7 and is delayed by 7 minutes (expected = 1 + 3 + 3 + 5 = 12)

Approach:

We need to find the expected arrival time of each taxi, which is equal to the sum of the arrival times of all taxis that arrived before it. We can calculate this by keeping a running sum of the previous taxi arrival times and adding that to the expected arrival time of the current taxi. If the actual arrival time of the taxi is greater than the expected arrival time, we calculate the delay as the difference between the two.

Let's go over the solution step by step:

  1. Initialize variables:
  • A running_sum variable to keep track of the arrival times of previous taxis
  • A result list to store the delayed arrival times of each taxi
  • A n variable to represent the total number of taxis
  1. Loop through the arrival times of the taxis:
  • Calculate the expected arrival time of the current taxi as running_sum
  • If the actual arrival time of the current taxi is greater than the expected arrival time, calculate the delay as the difference between the two
  • Add the delay (or 0 if the taxi is on time) to the result list
  • Add the arrival time of the current taxi to the running_sum variable
  1. Return the result list

Python code:

class Solution: def delayed_arrival(self, times: List[int]) -> List[int]: n = len(times) running_sum = 0 result = []

    for i in range(n):
        expected = running_sum
        delay = max(0, times[i] - expected)
        result.append(delay)
        running_sum += times[i]

    return result

Time complexity: O(n) Space complexity: O(n)

This solution has a time complexity of O(n) because it iterates through the input array only once. It also has a space complexity of O(n) because it stores the delayed arrival times of each taxi in a separate list.

Calculate Delayed Arrival Time Solution Code

1