You are given a string road, consisting only of characters "x" and ".", where each "x" denotes a pothole and each "." denotes a smooth road, and an integer budget.
In one repair operation, you can repair n consecutive potholes for a price of n + 1.
Return the maximum number of potholes that can be fixed such that the sum of the prices of all of the fixes doesn't go over the given budget.
Example 1:
Input: road = "..", budget = 5
Output: 0
Explanation:
There are no potholes to be fixed.
Example 2:
Input: road = "..xxxxx", budget = 4
Output: 3
Explanation:
We fix the first three potholes (they are consecutive). The budget needed for this task is 3 + 1 = 4.
Example 3:
Input: road = "x.x.xxx...x", budget = 14
Output: 6
Explanation:
We can fix all the potholes. The total cost would be (1 + 1) + (1 + 1) + (3 + 1) + (1 + 1) = 10 which is within our budget of 14.
Constraints:
1 <= road.length <= 1051 <= budget <= 105 + 1road consists only of characters '.' and 'x'.Problem Overview: You receive a road represented as a string where potholes appear as consecutive segments. Repairing a segment has a fixed setup overhead plus a cost proportional to the number of potholes fixed. Given a repair budget B, the task is to maximize how many potholes you can repair.
Approach 1: Brute Force Segment Selection (Exponential)
Start by scanning the string and extracting every contiguous pothole segment. Then try all combinations of segments to see which set fits within the budget and produces the highest number of repaired potholes. Each segment repair costs length + 1 due to the setup overhead. This approach quickly becomes infeasible because the number of segment combinations grows exponentially. Time complexity is O(2^k) where k is the number of pothole segments, and space complexity is O(k) for storing segment lengths.
Approach 2: Greedy with Sorting (O(n log n))
First iterate through the string and collect the length of every contiguous pothole segment. Each segment of length k costs k + 1 to repair. The key observation: because every segment has the same extra overhead of +1, repairing smaller segments first gives more potholes per unit budget. Store segment lengths in an array, sort them, and process from smallest to largest. If the remaining budget can cover the full cost k + 1, repair the entire segment and subtract the cost. If not, use the remaining budget to repair as many potholes as possible within that segment (up to budget - 1 because of setup cost). Time complexity is O(n log n) due to sorting, and space complexity is O(n) for storing segment lengths. This approach uses ideas from greedy algorithms and simple sorting.
Approach 3: Counting + Greedy Optimization (O(n))
Sorting can be avoided by counting segment lengths while scanning the road. Instead of storing every segment and sorting later, track them in frequency buckets keyed by length. Then iterate from the smallest length upward and greedily spend the budget repairing segments. This preserves the same greedy insight but reduces sorting overhead. The scan to detect segments takes O(n) time, and processing bucket counts also takes linear time relative to possible segment lengths. Space complexity remains O(n). This solution combines string traversal with a greedy cost allocation strategy.
Recommended for interviews: The greedy segment strategy with sorting is the expected answer. It shows you can convert a string parsing problem into a cost optimization problem and apply a greedy ordering. Mentioning the brute force approach demonstrates problem exploration, but the sorted greedy solution shows the optimization interviewers look for.
First, we count the number of each continuous pothole, recorded in the array cnt, i.e., cnt[k] represents there are cnt[k] continuous potholes of length k.
Since we want to repair as many potholes as possible, and for a continuous pothole of length k, we need to spend a cost of k + 1, we should prioritize repairing longer potholes to minimize the cost.
Therefore, we start repairing from the longest pothole. For a pothole of length k, the maximum number we can repair is t = min(budget / (k + 1), cnt[k]). We add the number of repairs multiplied by the length k to the answer, then update the remaining budget. For the remaining cnt[k] - t potholes of length k, we merge them into the potholes of length k - 1. Continue this process until all potholes are traversed.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the string road.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Segment Selection | O(2^k) | O(k) | Conceptual baseline for small inputs or reasoning about segment choices |
| Greedy with Sorting | O(n log n) | O(n) | General solution; simple to implement and commonly expected in interviews |
| Counting + Greedy | O(n) | O(n) | When segment lengths are processed during scanning and sorting can be avoided |
3119. Maximum Number of Potholes That Can Be Fixed (Leetcode Medium) • Programming Live with Larry • 1,246 views views
Practice Maximum Number of Potholes That Can Be Fixed with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor