diff --git a/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Explanation.md b/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Explanation.md new file mode 100644 index 0000000..3bd13ac --- /dev/null +++ b/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Explanation.md @@ -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) \ No newline at end of file diff --git a/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Solution.java b/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Solution.java new file mode 100644 index 0000000..565ef0d --- /dev/null +++ b/Daily Questions/#2594 - Minimum Time to Repair Cars - Medium/Solution.java @@ -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; + } + } \ No newline at end of file