Programming/Algorithm Question:
You are acting as a bank. You are given a list of customers’ transactions.
The transactions can be either a deposit (positive value) or a
withdrawal(negative value).
The initial balance in the bank is 0.
The bank can choose any customer to start serving from the list.
Once a customer is picked, the bank will continue serving the next customer in the list until the bank’s balance becomes zero or negative.
At that point, the bank will close.
Your task is to determine the maximum number of customers that can be served by the bank.
For example, given the customer_transactions = [-1, 2, 4, -3, 5, -10, 3, 10, 1],
you need to find the maximum number of customers that can be served by the bank.
The answer in this case is 4 i.e. [ 2, 4, -3, 5]
Answer to the above question
def max_customers_served_brute_force(transactions):
max_served = 0
for i in range(len(transactions)): # Start from each customer
balance = 0
count = 0
for j in range(i, len(transactions)):# Process customers fromindex i onward
balance += transactions[j]
if balance < 0:
break # Stop if the balance goes negative
count += 1
max_served = max(max_served, count)
return max_served