Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 2594. Minimum Time to Repair Cars
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

2594. Minimum Time to Repair Cars

2594. Minimum Time to Repair Cars

Difficulty: Medium

Topics:Array,Binary Search

You are given an integer arrayranks representing theranks of some mechanics.ranksi is the rank of theith mechanic. A mechanic with a rankr can repair n cars inr * n2 minutes.

You are also given an integercars representing the total number of cars waiting in the garage to be repaired.

Returntheminimum time taken to repair all the cars.

Note: All the mechanics can repair the cars simultaneously.

Example 1:

  • Input: ranks = [4,2,3,1], cars = 10
  • Output: 16
  • Explanation:
    • The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
    • The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
    • The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
    • The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
    • It can be proved that the cars cannot be repaired in less than 16 minutes.

Example 2:

  • Input: ranks = [5,1,8], cars = 6
  • Output: 16
  • Explanation:
    • The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
    • The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
    • The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
    • It can be proved that the cars cannot be repaired in less than 16 minutes.

Constraints:

  • 1 <= ranks.length <= 105
  • 1 <= ranks[i] <= 100
  • 1 <= cars <= 106

Hint:

  1. For a predefined fixed time, can all the cars be repaired?
  2. Try using binary search on the answer.

Solution:

We need to determine the minimum time required for a group of mechanics with different ranks to repair all the cars in a garage. Each mechanic's repair time is determined by their rank and the number of cars they repair. The goal is to minimize the maximum time taken by any mechanic.

Approach

The problem can be efficiently solved using binary search on the possible time values. The key insight is to check if a given timeT allows all cars to be repaired by the mechanics working simultaneously. For each mechanic with rankr, the maximum number of cars they can repair in timeT is given byfloor(sqrt(T / r)). We sum these values for all mechanics and check if the total is at least the number of cars needing repair. If it is,T is a feasible time, and we search for a smaller time; otherwise, we search for a larger time.

Let's implement this solution in PHP:2594. Minimum Time to Repair Cars

<?php/** * @param Integer[] $ranks * @param Integer $cars * @return Integer */functionrepairCars($ranks,$cars){........./**     * go to ./solution.php     */}// Example Usage:$ranks1=[4,2,3,1];$cars1=10;echorepairCars($ranks1,$cars1)."\n";// Output: 16$ranks2=[5,1,8];$cars2=6;echorepairCars($ranks2,$cars2)."\n";// Output: 16?>
Enter fullscreen modeExit fullscreen mode

Explanation:

  1. Binary Search Setup: We initializelow to 1 andhigh to the maximum possible time, which is when the highest-ranked mechanic repairs all cars alone.
  2. Binary Search Loop: In each iteration, we calculate the midpointmid and check if all cars can be repaired withinmid minutes.
  3. Feasibility Check: For each mechanic's rank, we compute the maximum number of cars they can repair inmid minutes. Summing these values gives the total number of cars that can be repaired. If this sum meets or exceeds the required number of cars,mid is a feasible time, and we adjust the search range to find a smaller feasible time. Otherwise, we increase the search range.
  4. Termination: The loop terminates whenlow equalshigh, which is the minimum feasible time.

This approach efficiently narrows down the possible minimum time using binary search, ensuring an optimal solution with a time complexity of O(n log(max_rank * cars^2)), wheren is the number of mechanics.

Contact Links

If you found this series helpful, please consider giving therepository a star on GitHub or sharing the post on your favorite social networks 😍.Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a Technical Lead with holistic knowledge of software development and design. I am also experienced in coordinating with stakeholders
  • Location
    2 NO MUSLIM NAGAR, MATUAIL, TUSHARDHARA, DEMRA - 1362, DHAKA, BANGLADESH
  • Education
    AHSANULLAH UNIVERSITY OF SCIENCE AND TECHNOLOGY
  • Pronouns
    he/him
  • Work
    TECH LEAD at MT TECHNOLOGIES
  • Joined

More fromMD ARIFUL HAQUE

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp