2071. Maximum-Number-Of-Tasks-You-Can-Assign

Question Link

Difficulty: Hard

WHY IS IT A HARD TO START OFF THE MONTHS RAAAAAAH?!?!?

They have no intention of getting people on that daily streak or what.

Anyway, the solution to this problem is way too hard, I need to go back and digest this one…

class Solution(object):
    def maxTaskAssign(self, tasks, workers, pills, strength):
        """
        :type tasks: List[int]
        :type workers: List[int]
        :type pills: int
        :type strength: int
        :rtype: int
        """
        n, m = len(tasks), len(workers)
        tasks.sort()
        workers.sort()

        def check(mid):
            p = pills
            # Ordered set of workers
            ws = SortedList(workers[m - mid :])
            # Enumerate each task from largest to smallest
            for i in range(mid - 1, -1, -1):
                # If the largest element in the ordered set is greater than or equal to tasks[i]
                if ws[-1] >= tasks[i]:
                    ws.pop()
                else:
                    if p == 0:
                        return False
                    rep = ws.bisect_left(tasks[i] - strength)
                    if rep == len(ws):
                        return False
                    p -= 1
                    ws.pop(rep)
            return True

        left, right, ans = 1, min(m, n), 0
        while left <= right:
            mid = (left + right) // 2
            if check(mid):
                ans = mid
                left = mid + 1
            else:
                right = mid - 1

        return ans

Time Complexity: O(n log n + m log m + min(m, n) log^2 min(m, n))

Space Complexity: O(log n + log m + min(m, n))

Time Taken: did not complete