Budget-Constrained Round-Trip Selection
Greedy & IntervalsAuto-graded · 22 test casesLast updated
Tests greedy selection and sorting to maximize items within a budget while minimizing cost for that maximum count.
You are planning round-trip visits starting from your base at position 0 on a one-dimensional number line. You have a list of destinations (integers representing positions) and a travel budget (maximum total distance you can cover).
Each destination requires a separate round trip from position 0 to that destination and back. The cost of visiting a destination at position x is 2 * |x| (the round-trip distance). Duplicate positions in the input list represent the same destination and are only visited once.
Your goal is to:
- Maximize the number of distinct destinations you visit
- Minimize the total distance traveled to achieve that maximum number of visits
Return a list [count, distance] where count is the maximum number of destinations you can visit within the budget, and distance is the minimum total distance required to visit that many destinations.
Constraints
- 1 ≤ length of
destinations≤ 10^5 - -10^6 ≤ each destination value ≤ 10^6
- 0 ≤
budget≤ 10^9 - If no destinations can be visited within the budget, return
[0, 0] - Position
0(if present in the list) costs nothing to visit
Examples
Example 1
Input: budget_constrained_trips([2, -3, 5, -1], 10)
Output: [2, 6]
Example 2
Input: budget_constrained_trips([10, 20], 15)
Output: [0, 0]
Hints
Hint 1
Consider what strategy maximizes the number of locations you can afford to visit
Hint 2
Each location has an independent cost. Which locations should you prioritize?
Hint 3
Sort the costs and take locations greedily while staying within budget
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that each destination has an independent, fixed cost equal to 2 * |position| for the round trip. To fit the maximum number of destinations within a fixed budget, we should visit the cheapest destinations first.
Algorithm:
- Remove duplicate destinations since each unique position is visited only once
- Calculate the cost for each unique destination:
2 * |position| - Sort these costs in ascending order
- Greedily select destinations starting from the cheapest, accumulating the total cost
- Stop when adding the next destination would exceed the budget
- Return the count of selected destinations and the accumulated total cost
Why this works:
The greedy approach is optimal because costs are independent and non-negative. If any set of k destinations fits within the budget, then the k cheapest destinations must also fit (their sum is minimal among all size-k subsets). Similarly, for a fixed count k, the k cheapest destinations yield the minimum possible total distance.
Edge cases:
- If
budget = 0, only destination0(if present) can be visited at zero cost - Duplicates like
[5, 5, 5]count as a single destination - Positions
xand-xare distinct destinations with equal cost2|x| - If no destinations fit the budget, return
[0, 0]
Complexity:
- Time: O(n log n) for deduplication and sorting, where n is the number of destinations
- Space: O(n) for storing unique destinations and their costs
Solution
from typing import List
def budget_constrained_trips(destinations: List[int], budget: int) -> List[int]:
# Remove duplicates - each position is visited at most once
# Calculate the cost for each unique destination
costs = sorted(2 * abs(x) for x in set(destinations))
count = 0
total_distance = 0
# Greedily select cheapest destinations first
for cost in costs:
if total_distance + cost > budget:
break
total_distance += cost
count += 1
return [count, total_distance]