-
-
Notifications
You must be signed in to change notification settings - Fork 155
Create 3199- Maximum Number of Potholes That Can Be Fixed.md #1506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...ns/lc-solutions/3100-3199/3199- Maximum Number of Potholes That Can Be Fixed.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
id: maximum-number-of-potholes-that-can-be-fixed | ||
title: maximum number of potholes that can be fixed | ||
sidebar_label: 3119. maximum number of potholes that can be fixed | ||
tags: | ||
- String | ||
- Greedy | ||
- Sorting | ||
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." | ||
--- | ||
|
||
|
||
|
||
## Problem | ||
|
||
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: | ||
|
||
- Choose an index $i$ from nums and increase $nums[i]$ by $1$ for a cost of $cost1$. | ||
|
||
- Choose two different indices $i$, $j$, from nums and increase $nums[i]$ and $nums[j]$ by $1$ for a cost of $cost2$. | ||
|
||
|
||
### Constraints | ||
|
||
- 1 <= road.length <= 105 | ||
- 1 <= budget <= 105 + 1 | ||
sunnyshin8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- oad consists only of characters '.' and 'x' | ||
--- | ||
|
||
## Approach | ||
|
||
Approach: Greedy Algorithm | ||
Identify Pothole Segments: | ||
|
||
The getSortedPotholeLengths method processes the input string road, which represents a road with potholes as segments of characters other than '.'. | ||
This method splits the road string by the character '.', which separates the potholes, and calculates the length of each pothole segment. | ||
It then sorts these lengths in descending order to prioritize repairing the largest potholes first. | ||
Prioritize Larger Potholes: | ||
|
||
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. | ||
Budget Management: | ||
|
||
The main method maxPotholes iterates over the sorted pothole lengths. | ||
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). | ||
If the current pothole segment can be fully repaired within the remaining budget, the algorithm repairs it and updates the budget accordingly. | ||
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. | ||
|
||
## Maximum Number of Potholes That Can Be Fixed | ||
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. | ||
|
||
#### Code in Java | ||
|
||
```java | ||
class Solution { | ||
public int maxPotholes(String road, int budget) { | ||
int ans = 0; | ||
|
||
for (final int length : getSortedPotholeLengths(road)) { | ||
final int canRepair = Math.max(0, budget - 1); | ||
if (length > canRepair) | ||
return ans + canRepair; | ||
ans += length; | ||
budget -= length + 1; | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
private List<Integer> getSortedPotholeLengths(final String road) { | ||
List<Integer> potholeLengths = new ArrayList<>(); | ||
for (String pothole : road.split("\\.")) | ||
potholeLengths.add(pothole.length()); | ||
Collections.sort(potholeLengths, Collections.reverseOrder()); | ||
return potholeLengths; | ||
} | ||
} | ||
|
||
### Complexity Analysis | ||
|
||
#### Time Complexity:𝑂 | ||
sunnyshin8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( | ||
sort | ||
) | ||
O(sort) | ||
|
||
#### Space Complexity: O(n) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.