Maximum Number of Potholes That Can Be Fixed - Problem

You're a road maintenance engineer with a limited budget who needs to fix as many potholes as possible on a damaged road. The road is represented as a string where 'x' represents a pothole and '.' represents smooth pavement.

Here's the key constraint: You can only repair consecutive potholes in a single operation. To repair n consecutive potholes costs n + 1 dollars (there's a fixed setup cost plus the cost per pothole).

Goal: Determine the maximum number of potholes you can fix without exceeding your budget.

Example: Road "x.xxx..x" has pothole groups of sizes [1, 3, 1]. Fixing 1 pothole costs 2, fixing 3 consecutive potholes costs 4, etc.

Input & Output

example_1.py โ€” Basic Case
$ Input: road = "x.xxx.x", budget = 7
โ€บ Output: 4
๐Ÿ’ก Note: We have pothole groups of sizes [1, 3, 1]. We can repair the group of size 3 (cost 4) and one group of size 1 (cost 2), totaling 4 potholes for cost 6, which is within budget 7.
example_2.py โ€” Limited Budget
$ Input: road = "xxx.x.x", budget = 4
โ€บ Output: 3
๐Ÿ’ก Note: We have groups [3, 1, 1]. We can either repair the group of size 3 (cost 4) or two groups of size 1 (cost 2+2=4). Repairing the size-3 group gives us 3 potholes, which is better than 2 potholes from the two size-1 groups.
example_3.py โ€” Edge Case
$ Input: road = ".....", budget = 10
โ€บ Output: 0
๐Ÿ’ก Note: The road has no potholes, so we can't repair anything regardless of budget.

Constraints

  • 1 โ‰ค road.length โ‰ค 104
  • 1 โ‰ค budget โ‰ค 106
  • road consists only of characters '.' and 'x'
  • Each repair operation must fix consecutive potholes

Visualization

Tap to expand
Road Repair OptimizationOriginal Road: x.xxx.xx.xxx.xIdentified Groups: [1, 3, 1]Group 1Size: 1, Cost: 2Group 2Size: 3, Cost: 4Group 3Size: 1, Cost: 2Efficiency Analysis (Budget = 7)Size 1 groups: 1/2 = 0.5 potholes per dollarSize 3 group: 3/4 = 0.75 potholes per dollarOptimal Strategy: Choose by Best Ratioโœ“ Select size-3 group: Cost 4, Potholes 3, Remaining budget: 3โœ“ Select size-1 group: Cost 2, Potholes 1, Remaining budget: 1โœ— Cannot afford another size-1 group (cost 2 > remaining 1)Total: 4 potholes repaired for cost 6
Understanding the Visualization
1
Identify Segments
Parse the road to find all consecutive pothole groups
2
Calculate Efficiency
Smaller groups have better pothole-to-cost ratios due to fixed setup cost
3
Greedy Selection
Always choose the most efficient option first
4
Maximize Repairs
Continue until budget is exhausted
Key Takeaway
๐ŸŽฏ Key Insight: Due to the fixed setup cost, smaller pothole groups are more cost-efficient. The greedy approach of always choosing the most efficient option first guarantees the optimal solution.
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 15
31.5K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen