13. Bus Ticket Change
β GFG solution to the Bus Ticket Change problem using a greedy strategy to ensure correct change for every customer in sequence. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[] representing passengers in a queue. Each bus ticket costs 5 coins, and arr[i] denotes the note a passenger uses to pay (which can be 5, 10, or 20). You must serve the passengers in the given order and always provide the correct change so that each passenger effectively pays exactly 5 coins. Your task is to determine whether it is possible to serve all passengers in the queue without ever running out of change.
π Examples
Example 1
Input: arr[] = [5, 5, 5, 10, 20]
Output: true
Explanation: From the first 3 customers, we collect three 5 coins bills in order.
From the fourth customer, we collect a 10 coins bill and give back a 5 coins.
From the fifth customer, we give a 10 coins bill and a 5 coins bill.
Since all customers got correct change we return true.Example 2
Input: arr[] = [5, 5, 10, 10, 20]
Output: false
Explanation: From the first two customers in order, we collect two 5 coins bills.
For the next two customers in order, we collect a 10 coins bill and give back a 5 coins bill.
For the last customer, we cannot give the change of 15 coins back because we only have two 10 coins bills.
Since not every customer received the correct change, the answer is false.π Constraints
$1 \le \text{arr.size()} \le 10^5$
$\text{arr}[i]$ contains only [5, 10, 20]
β
My Approach
The optimal approach uses a Greedy Strategy with denomination tracking to efficiently validate change-making capability:
Greedy Change Management
Initialize Counters:
Track two counters:
f(count of 5-coin notes) andt(count of 10-coin notes).These represent the available change at any point.
Process Each Payment:
For 5-coin payment: Simply increment the count of 5-coin notes (no change needed).
For 10-coin payment: Check if we have at least one 5-coin note for change. If yes, give it and store the 10-coin note. Otherwise, return false.
For 20-coin payment: Apply greedy strategy - prefer giving one 10-coin and one 5-coin note (to preserve 5-coin notes). If not possible, give three 5-coin notes. If neither option works, return false.
Greedy Optimization:
Always prefer larger denominations when giving change for 20-coin payments.
This preserves smaller 5-coin notes which are more versatile for future transactions.
Validation:
If all passengers are served successfully without running out of change, return true.
Return false immediately upon first failure to provide correct change.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We process each passenger exactly once in a single pass through the array.
Expected Auxiliary Space Complexity: O(1), as we only use two integer variables to track the count of 5-coin and 10-coin notes, regardless of input size.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated