Visit the most destinations without exceeding a travel budget
Company: Booking.com
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Overview: Given the cost of each destination and a total travel budget, find the largest number of destinations you can afford when every visit is paid in full. The problem tests greedy reasoning, a short proof that the choice is optimal, and edge cases such as free destinations and a budget spent exactly.
Constraints
- 1 <= len(costs) <= 10^5
- 0 <= costs[i] <= 10^4
- 0 <= budget <= 10^9
Examples
Input: ([200, 150, 300, 100, 250], 500)
Expected Output: 3
Explanation: Source example: sorted costs 100, 150, 200, 250, 300 give running totals 100, 250, 450, 700, so three fit within 500.
Input: ([0, 7, 3, 0], 3)
Expected Output: 3
Explanation: Source example: two free destinations plus the one costing 3 total exactly 3, which equals the budget; adding 7 would make 10.
Hints
- The order of the visits never changes the total you pay. Focus only on which destinations go into the chosen set.
- Suppose some set of k destinations fits. Which k destinations would leave the most money unspent?
- A destination that costs 0 always fits, even when the budget is 0, and a total equal to the budget is allowed.