-
Notifications
You must be signed in to change notification settings - Fork 20
Solution #2594 - Mridul/Edited - 16/03/2025 #32
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
Open
mrid88
wants to merge
3
commits into
Dijkstra-Edu:master
Choose a base branch
from
mrid88:feature/minimum-time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
43 changes: 43 additions & 0 deletions
43
Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Explanation.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,43 @@ | ||
# Explanation of the `repairCars` Code | ||
|
||
This Java class provides an efficient solution for calculating the minimum time required to repair a given number of cars, given the repair capabilities of different mechanics. The code uses a **binary search algorithm** to minimize the time. | ||
|
||
--- | ||
|
||
## Class: `Solution` | ||
|
||
The `Solution` class consists of two primary methods: | ||
- `repairCars(int[] ranks, int cars)` | ||
- `numCarsFixed(int[] ranks, long minutes)` | ||
|
||
--- | ||
|
||
### 1. Method: `repairCars` | ||
|
||
#### **Purpose** | ||
This method calculates the minimum time required to repair all the cars. | ||
|
||
#### **Parameters** | ||
- `ranks`: An array of integers, where each element represents the "rank" of a mechanic. A lower rank indicates a faster mechanic. | ||
- `cars`: An integer indicating the number of cars that need to be repaired. | ||
|
||
#### **Logic** | ||
1. **Initialization**: | ||
- `l` is initialized to `0` (minimum possible time). | ||
- `r` is initialized to the maximum possible time: | ||
|
||
|
||
\[ | ||
r = \text{(minimum rank)} \times \text{cars}^2 | ||
\] | ||
|
||
|
||
- This is because the worst-case scenario occurs when the fastest mechanic fixes all cars, and time increases quadratically with the number of cars. | ||
|
||
2. **Binary Search**: | ||
- While `l` is less than `r`, calculate the middle point `m = (l + r) / 2`. | ||
- Check if it is possible to repair the required number of cars within `m` minutes by calling `numCarsFixed(ranks, m)`: | ||
- If **true**, reduce the upper bound `r` to `m` (try to minimize time | ||
|
||
Time COmplexity: O(n) | ||
Space Complexity: O(n) | ||
23 changes: 23 additions & 0 deletions
23
Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Solution.java
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,23 @@ | ||
class Solution { | ||
public long repairCars(int[] ranks, int cars) { | ||
long l = 0; | ||
long r = (long) Arrays.stream(ranks).min().getAsInt() * cars * cars; | ||
|
||
while (l < r) { | ||
final long m = (l + r) / 2; | ||
if (numCarsFixed(ranks, m) >= cars) | ||
r = m; | ||
else | ||
l = m + 1; | ||
} | ||
|
||
return l; | ||
} | ||
|
||
private long numCarsFixed(int[] ranks, long minutes) { | ||
long carsFixed = 0; | ||
for (final int rank : ranks) | ||
carsFixed += Math.sqrt(minutes / rank); | ||
return carsFixed; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly explain the Time and Space Complexity as well