Visit the most destinations without exceeding a travel budget

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.

Booking.com logo
Booking.com
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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.

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.