Weighted Lottery Ticket Assignment
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests prefix sums and binary search to map ticket numbers into population-weighted ranges efficiently.
You are running a weighted lottery system where cities receive ticket ranges proportional to their population. Each city is assigned a contiguous block of consecutive ticket numbers, in the order given. City 0 receives tickets 1 through populations[0], city 1 receives tickets populations[0] + 1 through populations[0] + populations[1], and so forth.
Given a list of ticket numbers, determine which city owns each ticket.
Write a function assign_tickets(cities, populations, tickets) that returns a list of city names corresponding to each ticket number in the input order.
Constraints
1 ≤ len(cities) = len(populations) ≤ 200,000- All city names are unique
1 ≤ populations[i] ≤ 10^12sum(populations) ≤ 2^53 - 1- Each ticket is an integer in
[1, sum(populations)] 0 ≤ len(tickets) ≤ 200,000- Tickets may appear in any order and may be repeated
- A ticket number that falls exactly on a boundary between two cities belongs to the earlier city in the list
Notes
- The sum of all populations defines the total ticket space
- Ticket numbering starts at 1, not 0
- Each city's range is contiguous and non-overlapping
Examples
Example 1
Input: assign_tickets(['A', 'B', 'C'], [2, 3, 1], [1, 2, 3, 5, 6])
Output: ['A', 'A', 'B', 'B', 'C']
Example 2
Input: assign_tickets(['Only'], [1000000000000], [1, 2, 999999999999, 1000000000000])
Output: ['Only', 'Only', 'Only', 'Only']
Example 3
Input: assign_tickets(['Paris', 'Tokyo', 'Lima'], [5, 1, 4], [10, 1, 6, 5, 10, 2])
Output: ['Lima', 'Paris', 'Tokyo', 'Paris', 'Lima', 'Paris']
Hints
Hint 1
Build an array of cumulative population totals to define the boundary of each city's ticket range.
Hint 2
For each ticket, find the first city whose cumulative total is greater than or equal to the ticket number.
Hint 3
Binary search on the cumulative array will locate the owning city in O(log n) time per ticket.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is to recognize that each city owns a contiguous range of ticket numbers. We can precompute the boundaries of these ranges using cumulative sums, then use binary search to quickly locate which range contains each query ticket.
Step-by-step breakdown
-
Build cumulative boundaries: Create an array where
cumulative[i]represents the highest ticket number owned by cityi. City0owns tickets1throughcumulative[0], city1owns ticketscumulative[0] + 1throughcumulative[1], and so on. -
Binary search for each ticket: For a given ticket
t, we need the smallest indexiwherecumulative[i] >= t. This identifies the city that owns tickett. -
Handle boundary cases carefully: When a ticket equals a cumulative boundary exactly, it belongs to the city at that boundary index, not the next city. Using
bisect_leftfrom Python'sbisectmodule gives us exactly this behavior.
Why it works
Each city i owns exactly populations[i] consecutive tickets. The cumulative array partitions the entire ticket space [1, total] into non-overlapping intervals. Since cumulative is strictly increasing (all populations are positive), binary search correctly identifies the unique owning city for any valid ticket.
For example, with populations [2, 3, 1]:
cumulative = [2, 5, 6]- City A owns tickets 1–2 (up to cumulative[0])
- City B owns tickets 3–5 (up to cumulative[1])
- City C owns ticket 6 (up to cumulative[2])
Ticket 2 is assigned to city A because cumulative[0] = 2 >= 2, and it's the first such index.
Complexity
- Time: O(n + m log n) where n is the number of cities and m is the number of tickets. We build the cumulative array in O(n), then perform O(log n) binary search for each of m tickets.
- Space: O(n) for the cumulative array, plus O(m) for the output.
With the maximum constraints (200,000 cities and 200,000 tickets), this performs roughly 3.6 million comparisons, well within time limits.
from bisect import bisect_left
from typing import List
def assign_tickets(cities: List[str], populations: List[int], tickets: List[int]) -> List[str]:
cumulative = []
running_sum = 0
for pop in populations:
running_sum += pop
cumulative.append(running_sum)
return [cities[bisect_left(cumulative, t)] for t in tickets]