binary search

wonderful world·2023년 3월 18일

coding

목록 보기
8/9

2594. Minimum Time to Repair Cars

https://leetcode.com/contest/biweekly-contest-100/problems/minimum-time-to-repair-cars/

You are given an integer array ranks representing the ranks of some mechanics. ranksi is the rank of the ith mechanic. A mechanic with a rank r can repair n cars in r * n2 minutes.

You are also given an integer cars representing the total number of cars waiting in the garage to be repaired.
Return the minimum 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.​​​​​

by LeetFuncker1

class Solution:
    def repairCars(self, ranks, cars):
        low, high = 0, 10**15
        def solve(m):
            rem = cars
            for i in range(len(ranks)):
                x = m // ranks[i]
                x = int(math.sqrt(x))
                rem -= x
                if (rem <= 0):
                    break
            return rem
        while(low <= high):
            mid = (low + high) // 2
            if(solve(mid) <= 0):
                high = mid - 1
            else:
                low = mid + 1
        return low
profile
hello wirld

0개의 댓글