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 nconsecutive 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.
Input: road ="..", budget =5Output: 0Explanation:
There are no potholes to be fixed.
Example 2:
1
2
3
4
5
Input: road ="..xxxxx", budget =4Output: 3Explanation:
We fix the first three potholes(they are consecutive). The budget needed forthis task is`3 + 1 = 4`.
Example 3:
1
2
3
4
5
Input: road ="x.x.xxx...x", budget =14Output: 6Explanation:
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.
To maximize the number of potholes fixed within the budget, we should repair the smallest groups of consecutive potholes first, as their cost per pothole is lower. By sorting all groups of consecutive potholes by their size, we can greedily fix as many as possible.
classSolution {
public:int maxPotholesFixed(string road, int budget) {
vector<int> groups;
int n = road.size(), cnt =0, ans =0;
for (int i =0; i <= n; ++i) {
if (i < n && road[i] =='x') cnt++;
elseif (cnt >0) { groups.push_back(cnt); cnt =0; }
}
sort(groups.begin(), groups.end());
for (int g : groups) {
int cost = g +1;
if (budget >= cost) { ans += g; budget -= cost; }
else {
int canFix = min(g, budget /2);
ans += canFix;
break;
}
}
return ans;
}
};
classSolution {
publicintmaxPotholesFixed(String road, int budget) {
List<Integer> groups =new ArrayList<>();
int cnt = 0, ans = 0, n = road.length();
for (int i = 0; i <= n; i++) {
if (i < n && road.charAt(i) =='x') cnt++;
elseif (cnt > 0) { groups.add(cnt); cnt = 0; }
}
Collections.sort(groups);
for (int g : groups) {
int cost = g + 1;
if (budget >= cost) { ans += g; budget -= cost; }
else {
int canFix = Math.min(g, budget / 2);
ans += canFix;
break;
}
}
return ans;
}
}
classSolution {
funmaxPotholesFixed(road: String, budget: Int): Int {
val groups = mutableListOf<Int>()
var cnt = 0var ans = 0for (i in0..road.length) {
if (i < road.length && road[i] =='x') cnt++elseif (cnt > 0) { groups.add(cnt); cnt = 0 }
}
groups.sort()
var b = budget
for (g in groups) {
val cost = g + 1if (b >= cost) { ans += g; b -= cost }
else {
val canFix = minOf(g, b / 2)
ans += canFix
break }
}
return ans
}
}
classSolution:
defmaxPotholesFixed(self, road: str, budget: int) -> int:
groups: list[int] = []
cnt: int =0 ans: int =0 n: int = len(road)
for i in range(n +1):
if i < n and road[i] =='x':
cnt +=1elif cnt >0:
groups.append(cnt)
cnt =0 groups.sort()
for g in groups:
cost = g +1if budget >= cost:
ans += g
budget -= cost
else:
can_fix = min(g, budget //2)
ans += can_fix
breakreturn ans
impl Solution {
pubfnmax_potholes_fixed(road: String, mut budget: i32) -> i32 {
letmut groups =vec![];
letmut cnt =0;
letmut ans =0;
let n = road.len();
let chars: Vec<char>= road.chars().collect();
for i in0..=n {
if i < n && chars[i] =='x' {
cnt +=1;
} elseif cnt >0 {
groups.push(cnt);
cnt =0;
}
}
groups.sort();
for g in groups {
let cost = g +1;
if budget >= cost {
ans += g;
budget -= cost;
} else {
let can_fix = std::cmp::min(g, budget /2);
ans += can_fix;
break;
}
}
ans
}
}