Skip to content

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Comment on lines +42 to +43
Copy link
Member

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

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;
}
}