Quick 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.

Visit the most destinations without exceeding a travel budget

Company: Booking.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are planning a trip and have a total budget of `budget` dollars. You are given an integer array `costs`, where `costs[i]` is the cost of visiting the `i`-th destination. You may visit destinations in any order, but you must pay the full cost of each destination you choose to visit. You cannot partially visit a destination. Return the maximum number of destinations you can visit without exceeding your `budget`. ### Function Signature ```python def max_destinations(costs: list[int], budget: int) -> int: ``` ### Rules - Each destination can be visited at most once. - A set of destinations fits the budget when the sum of their costs is less than or equal to `budget`. Spending the whole budget exactly is allowed. - Return only the number of destinations. ### Constraints - `1 <= len(costs) <= 10^5` - `0 <= costs[i] <= 10^4` - `0 <= budget <= 10^9` ### Examples ```text Input: costs = [200,150,300,100,250], budget = 500 Output: 3 ``` Sorted, the costs are 100, 150, 200, 250, 300. The three cheapest add up to 450, and any four destinations cost at least 100 + 150 + 200 + 250 = 700, which is over 500. ```text Input: costs = [50,50,50,50], budget = 125 Output: 2 ``` Two destinations cost 100. A third would push the total to 150, which exceeds 125. ```text Input: costs = [0,7,3,0], budget = 3 Output: 3 ``` The two free destinations and the one costing 3 add up to exactly 3, which is within the budget. All four together would cost 10.

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.

You are planning a trip with a total budget of `budget` dollars. You are given an integer array `costs`, where `costs[i]` is the cost of visiting the `i`-th destination. You may visit destinations in any order, but you must pay the full cost of each destination you choose to visit. A destination cannot be partially visited, and each destination can be visited at most once. A set of destinations fits the budget when the sum of their costs is less than or equal to `budget`. Spending the whole budget exactly is allowed. Implement `max_destinations(costs, budget)` and return the maximum number of destinations you can visit without going over `budget`. Return only that count as an integer. Visiting no destination is always possible, so the answer is 0 when even the cheapest destination costs more than `budget`. ### Constraints - `1 <= len(costs) <= 10^5` - `0 <= costs[i] <= 10^4` - `0 <= budget <= 10^9` The sum of all costs is at most 10^5 * 10^4 = 10^9, so no total and no budget value goes above 2^31 - 1. The Java and C++ signatures still take `budget` as a 64-bit integer (`long` in Java, `long long` in C++). ### Example 1 ```text Input: costs = [200, 150, 300, 100, 250], budget = 500 Output: 3 ``` Sorted, the costs are 100, 150, 200, 250, 300. The three cheapest add up to 450, and any four destinations cost at least 100 + 150 + 200 + 250 = 700, which is over 500. ### Example 2 ```text Input: costs = [0, 7, 3, 0], budget = 3 Output: 3 ``` The two free destinations and the one costing 3 add up to exactly 3, which is within the budget. All four together would cost 10.

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

  1. The order of the visits never changes the total you pay. Focus only on which destinations go into the chosen set.
  2. Suppose some set of k destinations fits. Which k destinations would leave the most money unspent?
  3. A destination that costs 0 always fits, even when the budget is 0, and a total equal to the budget is allowed.

Most Destinations Within a Travel Budget

easyBooking.comSoftware Engineer

You are planning a trip with a total budget of budget dollars. You are given an integer array costs, where costs[i] is the cost of visiting the i-th destination.

You may visit destinations in any order, but you must pay the full cost of each destination you choose to visit. A destination cannot be partially visited, and each destination can be visited at most once.

A set of destinations fits the budget when the sum of their costs is less than or equal to budget. Spending the whole budget exactly is allowed.

Implement max_destinations(costs, budget) and return the maximum number of destinations you can visit without going over budget. Return only that count as an integer. Visiting no destination is always possible, so the answer is 0 when even the cheapest destination costs more than budget.

Constraints

  • 1 <= len(costs) <= 10^5
  • 0 <= costs[i] <= 10^4
  • 0 <= budget <= 10^9

The sum of all costs is at most 10^5 * 10^4 = 10^9, so no total and no budget value goes above 2^31 - 1. The Java and C++ signatures still take budget as a 64-bit integer (long in Java, long long in C++).

Example 1

Input: costs = [200, 150, 300, 100, 250], budget = 500
Output: 3

Sorted, the costs are 100, 150, 200, 250, 300. The three cheapest add up to 450, and any four destinations cost at least 100 + 150 + 200 + 250 = 700, which is over 500.

Example 2

Input: costs = [0, 7, 3, 0], budget = 3
Output: 3

The two free destinations and the one costing 3 add up to exactly 3, which is within the budget. All four together would cost 10.

Examples

Example 1
Input
([200, 150, 300, 100, 250], 500)
Output
3
Notes
Source example: sorted costs 100, 150, 200, 250, 300 give running totals 100, 250, 450, 700, so three fit within 500.
Example 2
Input
([0, 7, 3, 0], 3)
Output
3
Notes
Source example: two free destinations plus the one costing 3 total exactly 3, which equals the budget; adding 7 would make 10.

Constraints

  • •1 <= len(costs) <= 10^5
  • •0 <= costs[i] <= 10^4
  • •0 <= budget <= 10^9

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_destinations(costs: list[int], budget: int) -> int:
Sample call
max_destinations([200, 150, 300, 100, 250], 500)
Language
Case 1
Input
([200, 150, 300, 100, 250], 500)
Expected
3
Case 2
Input
([0, 7, 3, 0], 3)
Expected
3

Browse More Questions

Show the approach

Approach

Sort the costs in ascending order and take destinations from the cheapest, keeping a running total. Stop at the first cost that would push the total above budget; the number taken so far is the answer.

For any k, the k cheapest destinations have the smallest total of all sets of k destinations, because swapping any chosen destination for a cheaper unchosen one never raises the total. So k destinations can be visited exactly when the sum of the k smallest costs is at most budget. Every cost is nonnegative, so these prefix sums never decrease: once one prefix goes over the budget, every longer prefix does too, and the count at the first failure is the maximum.

Edge cases: a budget of 0 still admits every zero-cost destination; a total equal to the budget counts, so the check is total + cost > budget; if even the cheapest destination costs more than the budget the answer is 0; if the full total fits the answer is len(costs). All totals stay at or below 10^9, and the Java and C++ references keep the total in a 64-bit variable.

Time complexity:
O(n log n)
Space complexity:
O(n)