diff --git a/2594. Minimum Time to Repair Cars.py b/2594. Minimum Time to Repair Cars.py new file mode 100644 index 0000000..4b71d45 --- /dev/null +++ b/2594. Minimum Time to Repair Cars.py @@ -0,0 +1,51 @@ +2594. 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.​​​​​ +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 +''' +# Solution + + +class Solution: + def repairCars(self, ranks: List[int], cars: int) -> int: + def check(t: int) -> bool: + return sum(int(sqrt(t // r)) for r in ranks) >= cars + + return bisect_left(range(ranks[0] * cars * cars), True, key=check) + +# Time complexity: O(n) # n is the length of ranks +# Space complexity: O(1) diff --git a/3169. Count Days Without Meetings.py b/3169. Count Days Without Meetings.py new file mode 100644 index 0000000..7eccb44 --- /dev/null +++ b/3169. Count Days Without Meetings.py @@ -0,0 +1,79 @@ +3169. Count Days Without Meetings + +""" +You are given a positive integer days representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array meetings of size n where, meetings[i] = [start_i, end_i] represents the starting and ending days of meeting i (inclusive). + +Return the count of days when the employee is available for work but no meetings are scheduled. + +Note: The meetings may overlap. + + + +Example 1: + +Input: days = 10, meetings = [[5,7],[1,3],[9,10]] + +Output: 2 + +Explanation: + +There is no meeting scheduled on the 4th and 8th days. + +Example 2: + +Input: days = 5, meetings = [[2,4],[1,3]] + +Output: 1 + +Explanation: + +There is no meeting scheduled on the 5th day. + +Example 3: + +Input: days = 6, meetings = [[1,6]] + +Output: 0 + +Explanation: + +Meetings are scheduled for all working days. + + + +Constraints: + +1 <= days <= 109 +1 <= meetings.length <= 105 +meetings[i].length == 2 +1 <= meetings[i][0] <= meetings[i][1] <= days +""" + +class Solution: + def minAvailableDuration(self, days: int, meetings: List[List[int]]) -> int: + meetings.sort() + i = 0 + while i < len(meetings): + start, end = meetings[i] + j = i + 1 + while j < len(meetings) and meetings[j][0] <= end: + end = max(end, meetings[j][1]) + j += 1 + if end - start >= days: + return start + i = j + days -= (end - start) + if days <= 0: + return -1 + +Solution: + class Solution: + def countDays(self, days: int, meetings: List[List[int]]) -> int: + meetings.sort() + ans = last = 0 + for st, ed in meetings: + if last < st: + ans += st - last - 1 + last = max(last, ed) + ans += days - last + return ans \ No newline at end of file diff --git a/3356. Zero Array Transformation II.py b/3356. Zero Array Transformation II.py new file mode 100644 index 0000000..2c1470f --- /dev/null +++ b/3356. Zero Array Transformation II.py @@ -0,0 +1,74 @@ +/* + +3356. Zero Array Transformation II + +You are given an integer array nums of length n and a 2D array queries where queries[i] = [li, ri, vali]. + +Each queries[i] represents the following action on nums: + +Decrement the value at each index in the range [li, ri] in nums by at most vali. +The amount by which each value is decremented can be chosen independently for each index. +A Zero Array is an array with all its elements equal to 0. + +Return the minimum possible non-negative value of k, such that after processing the first k queries in sequence, nums becomes a Zero Array. If no such k exists, return -1. + + + +Example 1: + +Input: nums = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]] + +Output: 2 + +Explanation: + +For i = 0 (l = 0, r = 2, val = 1): +Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively. +The array will become [1, 0, 1]. +For i = 1 (l = 0, r = 2, val = 1): +Decrement values at indices [0, 1, 2] by [1, 0, 1] respectively. +The array will become [0, 0, 0], which is a Zero Array. Therefore, the minimum value of k is 2. +Example 2: + +Input: nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]] + +Output: -1 + +Explanation: + +For i = 0 (l = 1, r = 3, val = 2): +Decrement values at indices [1, 2, 3] by [2, 2, 1] respectively. +The array will become [4, 1, 0, 0]. +For i = 1 (l = 0, r = 2, val = 1): +Decrement values at indices [0, 1, 2] by [1, 1, 0] respectively. +The array will become [3, 0, 0, 0], which is not a Zero Array. + + +Constraints: + +1 <= nums.length <= 105 +0 <= nums[i] <= 5 * 105 +1 <= queries.length <= 105 +queries[i].length == 3 +0 <= li <= ri < nums.length +1 <= vali <= 5 + +*/ + +class Solution: + def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int: + def check(k: int) -> bool: + d = [0] * (len(nums) + 1) + for l, r, val in queries[:k]: + d[l] += val + d[r + 1] -= val + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True + + m = len(queries) + l = bisect_left(range(m + 1), True, key=check) + return -1 if l > m else l \ No newline at end of file