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. π
π§© Problem Description
π 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
β
My Approach
Greedy Change Management
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated