Ctrl K

Pothole Repairs on a Budget

Medium
A road is described by the string road: 'x' is a pothole and '.' is smooth pavement. You have a budget of budget. On one trip the crew patches k potholes that sit next to each other with no gaps, and the trip costs k + 1. There can be any number of trips, and a trip may patch only part of a run of potholes. Return the largest number of potholes you can repair without spending more than budget.
Example 1
road='xx.xxxx.x', budget=7 → 5

Fix the run of four potholes for 4 + 1 = 5. The remaining 2 pay for one more pothole from the pair, at 1 + 1 = 2. Five potholes in total.

Example 2
road='x.x.x', budget=6 → 3

Each lone pothole costs 2, and the budget covers all three.

Example 3
road='...', budget=4 → 0

The road is smooth; there is nothing to fix.

Constraints
  • 1 ≤ len(road) ≤ 10⁵
  • road consists only of the characters '.' and 'x'
  • 1 ≤ budget ≤ 10⁵ + 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumNo Twin Letters Side by Side MediumRound Prices to a Total MediumOne Word Outranks the Other MediumDistinct Letter Counts Medium
loading Python…
Input
road='xx.xxxx.x', budget=7
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 130 tests on the server, hidden ones included.