Problem Description
You are given an infinite number line of bags (one per coordinate) where some bags contain coins. The coins are given as non‐overlapping segments in the array coins, where each element coins[i] = [lᵢ, rᵢ, cᵢ] indicates that every bag from coordinate lᵢ to rᵢ contains cᵢ coins. In addition, you are given an integer k. The task is to select k consecutive bags (i.e. a window of length k) so that the total coins collected from those bags is maximized.
Key Insights
- The coin distribution is piecewise constant: only segments [lᵢ, rᵢ] have coins (with value cᵢ) and all other positions yield 0.
- The coin segments do not overlap; hence the coin function is defined by disjoint intervals.
- Rather than iterating bag‐by‐bag (the coordinates go up to 10^9), we can “compress” the problem into intervals and use prefix sum ideas.
- The maximum window is achieved when its boundaries align with one or more endpoints of these segments. Thus, only a limited set of candidate starting positions (such as each segment’s l, r, or adjusted r – k + 1) need be considered.
- With a prefix sum for the coin “integral” (i.e. the cumulative number of coins up to any coordinate), one can answer any query “what is the total coins in [L, R]?” in O(log n) using binary search on the segments.
Space and Time Complexity
Time Complexity: O(n log n + m log n)
• O(n log n) to sort the segments and build the prefix sums, where n = coins.length.
• O(m log n) for iterating over m candidate starting positions (m is O(n)) and answering each window query in O(log n).
Space Complexity: O(n)
• Storing the segments along with helper arrays for binary search and prefix sums.
Solution
We first sort the given coin segments by their starting coordinate. Because only segments have nonzero coin values, we “integrate” the coin contributions in each segment by computing a prefix sum over the intervals. In more detail, we build two helper arrays: • ends – an array of the ending coordinates from each segment. • prefix – prefix[i] is the total number of coins in all segments up to index i (each segment’s total coin count is (rᵢ - lᵢ + 1) * cᵢ).
We then build a function query(x) that returns the total coins from coordinate 1 to x. This function works by binary searching the segments array using the “ends” array. For any query x, we add up the full contributions from segments that lie completely before x and add the partial contribution for the first segment that x falls into (if any).
Once the query function is prepared, note that the total coins in a window [L, L+k-1] are given by query(L+k-1) – query(L-1). The twist is to find the best L. A common observation is that the maximum is achieved when L is “aligned” with one of the boundaries of the coin segments. Therefore, we consider a candidate set for L that includes: • Each segment’s starting coordinate (lᵢ). • Each segment’s ending coordinate (rᵢ). • And also max(1, rᵢ - k + 1) so that the window ending at rᵢ is exactly captured. We iterate over these candidate L values (making sure they are ≥1) and compute the window’s total coins using our query function. The maximum answer is returned.
Below are code solutions with detailed, line‐by‐line comments in Python, JavaScript, C++, and Java.