Skip to content

Commit 86dc1dd

Browse files
authored
Create 3199- Maximum Number of Potholes That Can Be Fixed.md
1 parent dffdd80 commit 86dc1dd

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
id: maximum-number-of-potholes-that-can-be-fixed
3+
title: maximum number of potholes that can be fixed
4+
sidebar_label: 3119. maximum number of potholes that can be fixed
5+
tags:
6+
- String
7+
- Greedy
8+
- Sorting
9+
description: "The given Solution class uses a greedy approach to maximize the number of potholes repaired within the given budget by prioritizing the repair of larger contiguous pothole segments first."
10+
---
11+
12+
13+
14+
## Problem
15+
16+
You are given an integer array $nums$ and two integers $cost1$ and $cost2$. You are allowed to perform either of the following operations any number of times:
17+
18+
- Choose an index $i$ from nums and increase $nums[i]$ by $1$ for a cost of $cost1$.
19+
20+
- Choose two different indices $i$, $j$, from nums and increase $nums[i]$ and $nums[j]$ by $1$ for a cost of $cost2$.
21+
22+
23+
### Constraints
24+
25+
- 1 <= road.length <= 105
26+
- 1 <= budget <= 105 + 1
27+
- oad consists only of characters '.' and 'x'
28+
---
29+
30+
## Approach
31+
32+
Approach: Greedy Algorithm
33+
Identify Pothole Segments:
34+
35+
The getSortedPotholeLengths method processes the input string road, which represents a road with potholes as segments of characters other than '.'.
36+
This method splits the road string by the character '.', which separates the potholes, and calculates the length of each pothole segment.
37+
It then sorts these lengths in descending order to prioritize repairing the largest potholes first.
38+
Prioritize Larger Potholes:
39+
40+
By sorting the pothole lengths in descending order, the algorithm ensures that it considers the largest potholes first. This is a key part of the greedy strategy, aiming to get the maximum immediate benefit from the budget.
41+
Budget Management:
42+
43+
The main method maxPotholes iterates over the sorted pothole lengths.
44+
For each pothole length, it calculates the maximum number of potholes that can be repaired given the remaining budget. Specifically, for each pothole segment, the cost to repair it is the length of the segment plus one (the cost of repairing the segment plus a fixed cost for starting the repair process).
45+
If the current pothole segment can be fully repaired within the remaining budget, the algorithm repairs it and updates the budget accordingly.
46+
If the current pothole segment cannot be fully repaired within the remaining budget, the algorithm calculates how many potholes can be repaired with the remaining budget and returns the total number of potholes repaired so far.
47+
48+
## Maximum Number of Potholes That Can Be Fixed
49+
The greedy approach focuses on repairing the largest potholes first to maximize the number of potholes that can be repaired within the given budget. The sorted list of pothole lengths allows the algorithm to quickly determine the most cost-effective repairs at each step, ensuring that the budget is utilized efficiently.
50+
51+
#### Code in Java
52+
53+
```java
54+
class Solution {
55+
public int maxPotholes(String road, int budget) {
56+
int ans = 0;
57+
58+
for (final int length : getSortedPotholeLengths(road)) {
59+
final int canRepair = Math.max(0, budget - 1);
60+
if (length > canRepair)
61+
return ans + canRepair;
62+
ans += length;
63+
budget -= length + 1;
64+
}
65+
66+
return ans;
67+
}
68+
69+
private List<Integer> getSortedPotholeLengths(final String road) {
70+
List<Integer> potholeLengths = new ArrayList<>();
71+
for (String pothole : road.split("\\."))
72+
potholeLengths.add(pothole.length());
73+
Collections.sort(potholeLengths, Collections.reverseOrder());
74+
return potholeLengths;
75+
}
76+
}
77+
78+
### Complexity Analysis
79+
80+
#### Time Complexity:𝑂
81+
(
82+
sort
83+
)
84+
O(sort)
85+
86+
#### Space Complexity: O(n)
87+

0 commit comments

Comments
 (0)