Find the minimum time after which one can exchange notes in Python

Suppose there are n cashiers exchanging money. At any moment, the i-th cashier has ki people waiting in line. The j-th person in line for the i-th cashier has m[i,j] notes to exchange. We need to find the minimum time required to exchange notes, given that each cashier takes 5 seconds to scan a single note and 15 seconds to complete the exchange process for each customer.

Problem Understanding

The total time for each cashier consists of ?

  • Scanning time: 5 seconds × total notes for all customers
  • Exchange time: 15 seconds × number of customers

We need to find which cashier takes the least total time to serve all their customers.

Algorithm Steps

  • For each cashier, calculate total time = (number of customers × 15) + (total notes × 5)
  • Compare all cashiers and return the minimum time

Example

Consider 6 cashiers, each with 12 customers, where each customer has different numbers of notes ?

def minTimeToExchange(k, m):
    n = len(k)
    minimum = float('inf')  # Better than hardcoded large number
    
    for i in range(n):
        # Base time for serving k[i] customers (15 seconds each)
        temp = k[i] * 15
        
        # Add scanning time for all notes
        for j in range(k[i]):
            temp += m[i][j] * 5
            
        # Update minimum if current cashier is faster
        if temp < minimum:
            minimum = temp
    
    return minimum

# Input data
k = [12, 12, 12, 12, 12, 12]  # 6 cashiers, each with 12 customers
m = [
    [7,8,9,7,9,6,10,9,9,6,7,8],    # Cashier 0: notes per customer
    [10,7,10,9,8,9,9,9,9,6,5,6],   # Cashier 1: notes per customer
    [9,8,8,9,8,6,7,9,10,6,6,7],    # Cashier 2: notes per customer
    [7,6,9,6,6,9,8,9,6,6,8,9],     # Cashier 3: notes per customer
    [9,8,7,6,5,10,8,10,7,6,6,8],   # Cashier 4: notes per customer
    [8,7,6,5,7,9,7,9,6,5,5,7]      # Cashier 5: notes per customer
]

result = minTimeToExchange(k, m)
print(f"Minimum time to exchange notes: {result} seconds")
Minimum time to exchange notes: 585 seconds

Step-by-Step Calculation

Let's trace through the calculation for cashier 5 (which gives the minimum time) ?

# Cashier 5 calculation
customers = 12
notes_per_customer = [8,7,6,5,7,9,7,9,6,5,5,7]

# Exchange time: 15 seconds per customer
exchange_time = customers * 15
print(f"Exchange time: {customers} × 15 = {exchange_time} seconds")

# Scanning time: 5 seconds per note
total_notes = sum(notes_per_customer)
scanning_time = total_notes * 5
print(f"Scanning time: {total_notes} × 5 = {scanning_time} seconds")

# Total time
total_time = exchange_time + scanning_time
print(f"Total time: {exchange_time} + {scanning_time} = {total_time} seconds")
Exchange time: 12 × 15 = 180 seconds
Scanning time: 81 × 5 = 405 seconds
Total time: 180 + 405 = 585 seconds

Optimized Solution

We can simplify the solution using Python's built-in functions ?

def minTimeToExchange_optimized(k, m):
    times = []
    
    for i in range(len(k)):
        exchange_time = k[i] * 15
        scanning_time = sum(m[i]) * 5
        total_time = exchange_time + scanning_time
        times.append(total_time)
    
    return min(times)

# Test with the same data
k = [12, 12, 12, 12, 12, 12]
m = [
    [7,8,9,7,9,6,10,9,9,6,7,8],
    [10,7,10,9,8,9,9,9,9,6,5,6],
    [9,8,8,9,8,6,7,9,10,6,6,7],
    [7,6,9,6,6,9,8,9,6,6,8,9],
    [9,8,7,6,5,10,8,10,7,6,6,8],
    [8,7,6,5,7,9,7,9,6,5,5,7]
]

result = minTimeToExchange_optimized(k, m)
print(f"Minimum time: {result} seconds")
Minimum time: 585 seconds

Conclusion

To find the minimum exchange time, calculate the total time for each cashier (15 seconds per customer + 5 seconds per note) and return the minimum. The solution has O(n×m) time complexity where n is the number of cashiers and m is the maximum customers per cashier.

Updated on: 2026-03-25T09:50:49+05:30

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements