Coding Quiz - ATM Problem
In this coding quiz, you'll write a function to calculate the minimum total time required for people to withdraw money from an ATM using a greedy algorithm.
People are standing in line, and the time it takes for each person to withdraw money is given.
The goal is to minimize the total time taken for all people to withdraw money.
Write a program to compute the total time by optimizing the order in which people stand in line.
def solution(times): # Write your code here return
Constraints
-
The number of people in line is between 1 and 1,000.
-
The time it takes for each person to withdraw money from the ATM is between 1 and 1,000 minutes.
Example Input/Output
-
Input:
[3, 1, 4, 3, 2]
(Time taken for each person to withdraw money) -
Output:
32
(Minimum total time taken)
Explanation of Example
In the example, the time each person takes to withdraw money is [3, 1, 4, 3, 2] minutes. By arranging them in order of least time, we get [1, 2, 3, 3, 4]. When their money is withdrawn in this order, the waiting time for each becomes:
-
First person: 1 minute
-
Second person: 1 (first person) + 2 = 3 minutes
-
Third person: 3 (up to second person) + 3 = 6 minutes
-
Fourth person: 6 (up to third person) + 3 = 9 minutes
-
Fifth person: 9 (up to fourth person) + 4 = 13 minutes
Thus, the total time becomes 1 + 3 + 6 + 9 + 13 = 32 minutes.
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help