Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: a = [(s, e) for s, e in zip(speed, efficiency)] if k == 1: return max([s * e for s, e in a]) a.sort(key=lambda x:-x[1]) ret = 0 q = [] s_sum = 0 for s, e in a: ret = max(ret, (s_sum + s) * e) if len(q) >= k - 1: if q[0] < s: x = heapq.heappop(q) s_sum = s_sum + s - x heapq.heappush(q, s) else: heapq.heappush(q, s) s_sum += s return ret % (10 ** 9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: pairs = list(zip(efficiency, speed)) pairs.sort(key = lambda x: (-x[0], -x[1])) arr = [] res = 0 sum_spd = 0 for eff, spd in pairs: if len(arr) < k: sum_spd += spd heapq.heappush(arr, spd) else: if spd > arr[0]: sum_spd += spd - heapq.heappushpop(arr, spd) res = max(res, sum_spd*eff) return res % (10**9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from sortedcontainers import SortedList class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: workers, MOD = SortedList(), 10 ** 9 + 7 ret = ss = 0 for e, s in sorted(zip(efficiency, speed), reverse=True): workers.add(s) ss += s if len(workers) > k: ss -= workers.pop(0) ret = max(ret, e * ss) return ret % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: q =[] worker = [] for s,e in zip(speed, efficiency): worker.append([s,e]) ans, speeds, minEff = 0,0,float('inf') for s,e in sorted(worker, key=lambda x: -x[1]): if len(q) < k: heapq.heappush(q, [s,e]) speeds += s minEff = min(minEff, e) elif s > q[0][0]: ts,te = heapq.heappop(q) speeds = speeds - ts + s minEff = e heapq.heappush(q, [s,e]) ans = max(ans, speeds*minEff) return ans%(10**9+7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: heap = [] total = 0 pairs = list(zip(speed, efficiency)) pairs.sort(key = lambda x: -x[1]) res = 0 MOD = 10 ** 9 + 7 for i, (s, e) in enumerate(pairs): res = max(e * (total + s), res) if len(heap) < k - 1: heapq.heappush(heap, s) total += s elif heap and s > heap[0]: total -= heapq.heappop(heap) heapq.heappush(heap, s) total += s return res % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from queue import PriorityQueue class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 if k == 1: return max([speed[i] * efficiency[i] for i in range(n)]) % mod engs = sorted([(efficiency[i], speed[i]) for i in range(n)]) others = PriorityQueue() maxi = 0 otherSum = 0 for i in range(n - 1, -1, -1): maxi = max(maxi, ((engs[i][1] + otherSum) * engs[i][0])) otherSum += engs[i][1] others.put(engs[i][1]) while others.qsize() > k - 1: otherSum -= others.get() return maxi % mod
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: unit = sorted([(s, e) for (s, e) in zip(speed, efficiency)], key=lambda x:x[1], reverse=True) max_res = -1 curr_sum = 0 MOD = 10**9 + 7 heap = [] for s, e in unit: curr_sum += s max_res = max(max_res, curr_sum * e) heapq.heappush(heap, s) if len(heap) >= k: curr_sum -= heapq.heappop(heap) return max_res % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: arr = list(zip(efficiency, speed)) arr.sort(reverse=True) counter = 0 curr_max = 0 heap = [] for i in arr: curr_max = max(curr_max, i[0] * (counter + i[1])) heapq.heappush(heap, i[1]) counter += i[1] if len(heap) > k - 1: counter -= heapq.heappop(heap) return (curr_max) % ((10 ** 9) + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from queue import PriorityQueue class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 if k == 1: return max([speed[i] * efficiency[i] for i in range(n)]) % mod engs = sorted([(efficiency[i] % mod, speed[i] % mod) for i in range(n)]) maxi = 0 others = PriorityQueue() otherSum = 0 for i in range(n - 1, -1, -1): maxi = max(maxi, ((engs[i][1] + otherSum) * engs[i][0]) ) otherSum += engs[i][1] others.put(engs[i][1]) while others.qsize() > k - 1: otherSum -= others.get() return maxi % mod
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from queue import PriorityQueue class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 if k == 1: return max([speed[i] * efficiency[i] for i in range(n)]) % mod engs = sorted([(efficiency[i] % mod, speed[i] % mod) for i in range(n)]) others = PriorityQueue() maxi = 0 otherSum = 0 for i in range(n - 1, -1, -1): maxi = max(maxi, ((engs[i][1] + otherSum) * engs[i][0])) otherSum += engs[i][1] others.put(engs[i][1]) while others.qsize() > k - 1: otherSum -= others.get() return maxi % mod
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: pairs = list(zip(efficiency, speed)) pairs.sort(key = lambda x: (-x[0], -x[1])) arr = [] res = 0 sum_spd = 0 for eff, spd in pairs: if len(arr) < k: sum_spd += spd heapq.heappush(arr, spd) else: sum_spd += spd - heapq.heappushpop(arr, spd) res = max(res, sum_spd*eff) return res % (10**9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, S: List[int], E: List[int], k: int) -> int: tmp = [] for x, y in zip(S, E): tmp.append([y, x]) tmp.sort(reverse=True) stack = [] sums = 0 res = 0 for i in range(n): nk = tmp[i][0] sums += tmp[i][1] - (heapq.heappop(stack) if len(stack) == k else 0) heapq.heappush(stack, tmp[i][1]) res = max(res, nk * sums ) return res % 1000000007
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 eff_speed = sorted(zip(efficiency, speed), reverse=True) n = len(eff_speed) i = 0 speed_h = [] speed_sum = 0 max_perf = 0 start = 0 while i < n: while i == start or (i < n and eff_speed[i][0] == eff_speed[i - 1][0]): heapq.heappush(speed_h, eff_speed[i][1]) speed_sum += eff_speed[i][1] if len(speed_h) > k: speed_sum -= heapq.heappop(speed_h) i += 1 start = i cur_efficiency = eff_speed[i - 1][0] max_perf = max(max_perf, cur_efficiency * speed_sum) return max_perf % mod def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 eff_speed = sorted(zip(efficiency, speed), reverse=True) n = len(eff_speed) speed_h = [] speed_sum = 0 max_perf = 0 for e, s in eff_speed: heapq.heappush(speed_h, s) speed_sum += s if len(speed_h) > k: speed_sum -= heapq.heappop(speed_h) max_perf = max(max_perf, speed_sum * e) return max_perf % mod
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: arr = zip(efficiency, speed) arr = sorted(arr, key=lambda x: (-x[0],-x[1])) pq = [] maxSoFar, currSum = 0, 0 for eff,sp in arr: currSum += sp heapq.heappush(pq,sp) if len(pq) == k+1: currSum -= heapq.heappop(pq) maxSoFar = max(maxSoFar, currSum * eff) return maxSoFar % 1000000007
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: # O(nlogn + 2nlogk) time, no unnecessary max comparison, O(n) space def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 eff_speed = sorted(zip(efficiency, speed), reverse=True) n = len(eff_speed) i = 0 speed_h = [] speed_sum = 0 max_perf = 0 start = 0 while i < n: while i == start or (i < n and eff_speed[i][0] == eff_speed[i - 1][0]): heapq.heappush(speed_h, eff_speed[i][1]) speed_sum += eff_speed[i][1] if len(speed_h) > k: speed_sum -= heapq.heappop(speed_h) i += 1 start = i cur_efficiency = eff_speed[i - 1][0] max_perf = max(max_perf, cur_efficiency * speed_sum) return max_perf % mod # O(nlogn + 2nlogk) time, O(n) space def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10 ** 9 + 7 eff_speed = sorted(zip(efficiency, speed), reverse=True) n = len(eff_speed) speed_h = [] speed_sum = 0 max_perf = 0 for e, s in eff_speed: heapq.heappush(speed_h, s) speed_sum += s if len(speed_h) > k: speed_sum -= heapq.heappop(speed_h) max_perf = max(max_perf, speed_sum * e) return max_perf % mod
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: M = 10 ** 9 + 7 heap = [] ans = t = 0 for e, s in sorted(zip(efficiency, speed), reverse=True): t += s heapq.heappush(heap, s) if len(heap) == k+1: t -= heapq.heappop(heap) ans = max(ans, t * e) return ans % M
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from heapq import heappush, heappop class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: speed_heap = [] speed_sum = 0 max_performance = 0 for cur_efficiency, cur_speed in sorted(zip(efficiency, speed), reverse=True): heappush(speed_heap, cur_speed) speed_sum += cur_speed if len(speed_heap) > k: speed_sum -= heappop(speed_heap) max_performance = max(max_performance, speed_sum * cur_efficiency) return max_performance % (10 ** 9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: if k < 1: return 0 best = 0 heap = [] cur_sum = 0 data = sorted(list(zip(speed, efficiency)), key=lambda x: -x[1]) for s, e in data: if len(heap) < k: # always add this eng. cur_sum += s heapq.heappush(heap, s) if cur_sum * e > best: best = cur_sum * e else: if (cur_sum - heap[0] + s) * e > best: best = (cur_sum - heap[0] + s) * e if s > heap[0]: cur_sum += (s - heap[0]) heapq.heappush(heap, s) heapq.heappop(heap) return best % 1000000007
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: arr = zip(efficiency, speed) arr = sorted(arr, key=lambda x: (-x[0],-x[1])) pq = [] maxSoFar, currSum = 0, 0 for eff,sp in arr: currSum += sp heapq.heappush(pq,sp) if len(pq) == k+1: currSum -= heapq.heappop(pq) maxSoFar = max(maxSoFar, currSum * eff) return maxSoFar % ((10**9) + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: q = [] sum_speed = 0 res = 0 for e,s in sorted(zip(efficiency, speed), reverse=True): sum_speed += s heapq.heappush(q, s) while len(q) > k: sum_speed -= heapq.heappop(q) res = max(res, e * sum_speed) return res % (10 ** 9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: MOD = 10**9 + 7 pq = [] engineers = list(zip(speed, efficiency)) engineers.sort(key=lambda tup:-tup[1]) # sort by efficiency in decreasing order performance = 0 tot_speed = 0 for engineer in engineers: min_efficiency = engineer[1] if len(pq) == k: if pq[0] < engineer[0]: # if there's an engineer with higher speed, there's a good chance to get higher performance bad_speed = heapq.heappop(pq) # fire the guy with the lowest speed heapq.heappush(pq, engineer[0]) tot_speed = tot_speed - bad_speed + engineer[0] else: heapq.heappush(pq, engineer[0]) tot_speed += engineer[0] performance = max(performance, tot_speed*min_efficiency) return performance % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
from heapq import heappop, heappush class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: hh = [[efficiency[ii],speed[ii]] for ii in range(n)] hh.sort(reverse=True) heap =[] cursum = 0; cureff = hh[0][0] ans = 0; for ih,[e,s] in enumerate(hh): heappush(heap,(s,ih)) (tops,topind) = heap[0] cursum = cursum +s if len(heap)>k: heappop(heap) cursum = cursum-hh[topind][1] cureff = e ans = max(ans,cureff*cursum) return ans%(10**9+7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: MOD = 10 ** 9 + 7 es = sorted(zip(efficiency, speed)) result = 0 heap = [0] s = 0 for i in range(n - 1, -1, -1): if len(heap) < k: heapq.heappush(heap, es[i][1]) s = (s + es[i][1]) elif es[i][1] > heap[0]: s = (s + es[i][1] - heapq.heappushpop(heap, es[i][1])) p = es[i][0] * s result = max(result, p) return result % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n, speed, efficiency, k): h = [] res = sSum = 0 for e, s in sorted(zip(efficiency, speed), reverse=1): heapq.heappush(h, s) sSum += s if len(h) > k: sSum -= heapq.heappop(h) res = max(res, sSum * e) return res % (10**9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: MOD = 10**9 + 7 combo = [] for i in range(n): heapq.heappush(combo, (-efficiency[i], -speed[i])) tmp = [] ans, currsum = 0, 0 while combo: curre, currs = heapq.heappop(combo) heapq.heappush(tmp, -currs) currsum -= currs if len(tmp) > k: currsum -= heapq.heappop(tmp) ans = max(ans, -currsum * curre) return ans % MOD
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
import heapq class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: pairs = [(-efficiency[i], speed[i]) for i in range(n)] heapq.heapify(pairs) t = 0 res = 0 usedSpeeds = [] cur = 0 while pairs: e, s = heapq.heappop(pairs) cur += s t += 1 if t <= k: res = max(res, -1 * e * cur) elif t > k: cur -= heapq.heappop(usedSpeeds) res = max(res, -1 * e * cur ) heapq.heappush(usedSpeeds, s) return res % (10 ** 9 + 7)
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7. The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.    Example 1: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2 Output: 60 Explanation: We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60. Example 2: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3 Output: 68 Explanation: This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68. Example 3: Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4 Output: 72   Constraints: 1 <= n <= 10^5 speed.length == n efficiency.length == n 1 <= speed[i] <= 10^5 1 <= efficiency[i] <= 10^8 1 <= k <= n
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: vals = [(speed[i], efficiency[i]) for i in range(n)] vals.sort(reverse = True, key = lambda x: x[1]) speed = 0 ans = 0 pq = [] for i in range(n): heapq.heappush(pq, vals[i][0]) speed += vals[i][0] if len(pq) > k: speed -= heapq.heappop(pq) ans = max(ans, speed*vals[i][1]) return ans % (10**9 + 7)
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: ws = sorted(worker, reverse=True) dp = sorted(zip(difficulty, profit), key=lambda x: x[1], reverse=True) # print(list(dp)) i = 0 total = 0 for w in ws: while dp[i][0] > w: i = i + 1 if i >= len(dp): return total total = total + dp[i][1] return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: job = sorted(zip(difficulty, profit)) best = 0 i = 0 profit = 0 for work in sorted(worker): while (i < len(difficulty) and job[i][0] <= work): best = max(best, job[i][1]) i += 1 profit += best return profit
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: rel = zip(difficulty, profit) rel = sorted(rel) output = 0 worker.sort() mx = 0 j = 0 for i in worker: while j < len(rel) and i >= rel[j][0]: mx = max(mx, rel[j][1]) j += 1 print(i, mx) output += mx return output
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
# 6:12 -> 6:25 | 7:23 -> # Find mapping of workers to jobs such that profit is maximized # Greedy approach: take highest profit job, assign it to most highly skilled worker from queue import PriorityQueue class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: # print('difficulty', difficulty) # print('profit', profit) # print('worker', worker) pq = PriorityQueue() worker.sort() for i, job in enumerate(profit): pq.put( (-job, i) ) profit = 0 while worker: skill = worker.pop() cash, diff_index = pq.get() if not pq.empty() else (0, -1) while not pq.empty() and diff_index > -1 and skill < difficulty[diff_index]: cash, diff_index = pq.get() if skill < difficulty[diff_index]: cash, diff_index = 0, -1 if cash != 0: pq.put( (cash, diff_index) ) cash *= -1 profit += cash # print(profit, cash, skill) return profit
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = [[0,0]] + sorted([[difficulty[i], profit[i]] for i in range(len(profit))]) for i in range(1, len(jobs)): jobs[i][1] = max(jobs[i-1][1], jobs[i][1]) res, workerCounts = 0, collections.Counter(worker) def binarySearch(n): l, r = 0, len(jobs)-1 while l < r-1: mid = (l+r)//2 if jobs[mid][0] > n: r = mid - 1 else: l = mid return jobs[l][1] if jobs[r][0] > n else jobs[r][1] for work, count in list(workerCounts.items()): res += binarySearch(work)*count return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: temp = sorted( zip(difficulty, profit) ) d = collections.defaultdict(int) for u, v in temp: d[u] = v maxdifficulty = max(worker) for i in range(1, maxdifficulty + 1): d[i] = max(d[i], d[i - 1]) # print (d) ans = 0 for i in worker: # print (i, d[i]) ans += d[i] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: d = sorted([(difficulty[i],profit[i]) for i in range(len(difficulty))]) c = [0] * 100005 # print(d) t = 0 for i in range(1,100005): c[i] = c[i-1] while t < len(d) and i == d[t][0]: c[i] = max(c[i],d[t][1]) t += 1 # print(c[0:101]) # print([c[i] for i in worker]) return sum([c[i] for i in worker])
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: max_v = max(max(difficulty), max(worker)) n = len(difficulty) best = [0] * (max_v + 1) for i in range(n): d = difficulty[i] p = profit[i] best[d] = max(best[d], p) for i in range (1, max_v + 1): best[i] = max(best[i - 1], best[i]) s = 0 for d in worker: s += best[d] return s
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: arr=[0 for _ in range(10**5+1)] for i in range(len(profit)): arr[difficulty[i]]=max(profit[i],arr[difficulty[i]]) for i in range(1,10**5+1): arr[i]=max(arr[i-1],arr[i]) ans=0 # print(arr[:101]) for i in worker: ans+=arr[i] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: output=0 job=sorted(zip(difficulty,profit),reverse=True,key=lambda x:x[1]) worker=sorted(worker,reverse=True) jobIndex=0 for workerCab in worker: # print(workerCab) while jobIndex<len(job): # print(jobIndex) if job[jobIndex][0]<=workerCab: # print('find') break else: jobIndex+=1 if jobIndex<len(job): output=output+job[jobIndex][1] # print(output,job[jobIndex][1]) # print(job) return output
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: for i in range(len(difficulty)): difficulty[i] = (difficulty[i], profit[i]) difficulty.sort(key = lambda x:x[0]) # O(NlogN) i, L = 0, len(difficulty) ans, most = 0, 0 for wker in sorted(worker): while i < L and difficulty[i][0] <= wker: most = max(most, difficulty[i][1]) i += 1 ans += most return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = [] total = 0 for i in range(len(profit)): jobs.append([difficulty[i], profit[i]]) print(jobs) worker.sort() jobs.sort() i = 0 best = 0 for work in worker: while i < len(jobs) and work >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 total += best return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit)) worker.sort() m = len(worker) n = len(jobs) j = 0 max_profit = 0 result = 0 for i in range(m): ability = worker[i] while j < n and jobs[j][0] <= ability: max_profit = max(max_profit, jobs[j][1]) j += 1 result += max_profit return result
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: best = 0 tot = 0 worker.sort() for d, p in sorted(zip(difficulty, profit)): while worker and d > worker[0]: tot += best worker.pop(0) best = max(best, p) tot += best * len(worker) return tot
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: l = sorted(zip(profit,difficulty), reverse = True) worker.sort(reverse = True) p = 0 for w in worker: while l and w < l[0][1]: l.pop(0) if not l: break p+=l[0][0] return p
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
import bisect class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: m,n={},len(difficulty) for ind, val in enumerate(difficulty): m[val]=max(m.get(val,0),profit[ind]) difficulty.sort() for ind, val in enumerate(difficulty): if ind>0: m[val]=max(m[val],m[difficulty[ind-1]]) ans = 0 for ind, val in enumerate(worker): d_ind = bisect.bisect_left(difficulty, val) if d_ind<n: if difficulty[d_ind]<=val: ans+=m[difficulty[d_ind]] else: ans+=m[difficulty[d_ind-1]] if d_ind>0 else 0 else: ans+=m[difficulty[d_ind-1]] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: difficulty, profit = zip(*sorted(zip(difficulty, profit))) profit_table = {} # maps difficulty to profit for i, (d, p) in enumerate(zip(difficulty, profit)): if i > 0: profit_table[d] = max(p, profit_table[difficulty[i-1]]) else: profit_table[d] = p value = 0 d_index = len(difficulty) - 1 for w in reversed(sorted(worker)): while d_index >= 0 and difficulty[d_index] > w: d_index -= 1 if d_index >= 0: value += profit_table[difficulty[d_index]] return value
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit)) profit = i = maxprofits = 0 for ability in sorted(worker): while i < len(jobs) and ability >= jobs[i][0]: profit = max(jobs[i][1], profit) i += 1 maxprofits += profit return maxprofits
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
import bisect class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: if not difficulty: return 0 indices = sorted(list(range(len(difficulty))), key=lambda i: difficulty[i]) difficulty = [difficulty[i] for i in indices] profit = [profit[i] for i in indices] max_profit = [profit[0]] for p in profit[1:]: max_profit.append(max(max_profit[-1], p)) total = 0 for ability in worker: idx = bisect.bisect_right(difficulty, ability) if idx > 0: total += max_profit[idx-1] return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: if difficulty is None: return 0 thislist = [] for i in range(0, len(difficulty)): thislist.append((difficulty[i], profit[i])) thislist.sort() worker.sort() res = 0 curr_max = 0 worker_index = 0 list_index = 0 while worker_index < len(worker) and list_index < len(thislist): if thislist[list_index][0] <= worker[worker_index]: curr_max = max(curr_max, thislist[list_index][1]) list_index += 1 else: res += curr_max worker_index += 1 if worker_index < len(worker): res += curr_max*(len(worker)-worker_index) return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
import bisect class Solution: def solve(self, diff, profit, worker): mp = {} for i in range(len(diff)): d = diff[i] p = profit[i] if d not in mp: mp[d] = 0 mp[d] = max(mp[d], p) diff2 = sorted(mp.keys()) ln = len(diff2) mx = [0]*ln for i in range(ln): d = diff2[i] p = mp[d] if i == 0: mx[i] = p else: mx[i] = max(mx[i-1], p) total = 0 # print(diff2) # print(profit2) # print(mx) # print('---') for w in worker: if w < diff2[0]: continue i = bisect.bisect_left(diff2, w) if i >= len(diff2): i = len(diff2)-1 elif diff2[i] > w: i -= 1 if i < 0: continue max_profit = mx[i] # print(w, i, mx[i]) total += max_profit return total def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: return self.solve(difficulty, profit, worker)
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: combined = sorted(zip(difficulty,profit)) worker.sort() n = len(worker) start = 0 net_profit = 0 current_profit = 0 for i in range(n): while start< len(difficulty) and combined[start][0]<=worker[i]: current_profit = max(combined[start][1],current_profit) start += 1 net_profit += current_profit return net_profit
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: value = list(zip(difficulty, profit)) value.sort() print(value) res = 0 i = 0 best = 0 for person in sorted(worker): while i < len(value) and person >= value[i][0]: best = max(best, value[i][1]) i += 1 res += best return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: pairs = [(a, b) for a, b in zip(difficulty, profit)] sorted_pairs = sorted(pairs, key=lambda p: p[0]) benifit = [] current_max = 0 for diff, pro in sorted_pairs: current_max = max(pro, current_max) benifit.append((diff, current_max)) sorted_workers = sorted(worker) ans = 0 current_difficulty_index = worker_index = 0 while worker_index < len(worker): if sorted_workers[worker_index] < benifit[current_difficulty_index][0]: worker_index += 1 continue while ( current_difficulty_index + 1 < len(worker) and benifit[current_difficulty_index + 1][0] <= sorted_workers[worker_index] ): current_difficulty_index += 1 # print( # f\"{worker_index=} {current_difficulty_index=} {benifit[worker_index][1]=}\" # ) ans += benifit[current_difficulty_index][1] worker_index += 1 return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: worker.sort() arr=[] for i in range(len(profit)): arr.append([difficulty[i],profit[i]]) arr.sort(key=lambda x:x[0]) for i in range(len(arr)): difficulty[i] = arr[i][0] profit[i] = arr[i][1] # print(difficulty) # print(profit) # print(worker) prev = [profit[0]] for i in range(1,len(profit)): prev.append(max(profit[i],prev[-1])) i = 0 p=0 j = 0 while(j<len(worker)): if(i==len(difficulty)): p+=prev[i-1] j+=1 continue if(difficulty[i]<=worker[j]): i+=1 # elif(difficulty[i]==worker[j]): # # p+=prev[i] # i+=1 # # j+=1 else: x=i-1 while(x>=0 and difficulty[x]>worker[j]): x-=1 if(x>=0): p+=prev[x] j+=1 return p
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
from bisect import bisect class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: maxp = 0 maxpa = [] for difficulty,profit in sorted(zip(difficulty,profit),key=lambda x:x[0]): maxp = max(maxp,profit) maxpa.append((difficulty,maxp)) total = 0 for w in worker: ind = bisect(maxpa,(w,float('inf'))) if ind>0: total += maxpa[ind-1][1] return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: job = {} for cost, gain in zip(difficulty, profit): if cost in job: job[cost] = max(job[cost], gain) else: job[cost] = gain job = [(k, v) for k, v in list(job.items())] + [(0, 0)] job.sort() useless = set() global_max = -float('inf') for i, (c, g) in enumerate(job): if g < global_max: useless.add(i) else: global_max = g job = [job[i] for i in range(len(job)) if i not in useless] print(job) worker.sort() i, j = 0, 0 ans = 0 while j < len(worker): while job[i][0] < worker[j] and i < len(job) - 1: i += 1 ans += job[i][1] if job[i][0] <= worker[j] else job[i- 1][1] j += 1 return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty, profit, worker): data = sorted([(d,p) for d,p in zip(difficulty, profit)]) worker.sort() i, n, money = 0, len(data), 0 res = 0 for w in worker: while i < n and w >= data[i][0]: money = max(money, data[i][1]) i += 1 res += money return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: mDifficulty = {} for i in range(len(difficulty)): d = difficulty[i] if difficulty[i] not in mDifficulty: mDifficulty[d] = 0 mDifficulty[d] = max(mDifficulty[d], profit[i]) jobs = [] for key, value in mDifficulty.items(): jobs.append([key, value]) jobs.sort(key=lambda job:job[0]) for i in range(1, len(jobs)): jobs[i][1] = max(jobs[i][1], jobs[i - 1][1]) worker.sort() start = len(jobs) - 1 count = 0 for i in range(len(worker) - 1, - 1, -1): while worker[i] < jobs[start][0] and start >= 0: start -= 1 if start < 0: break count += jobs[start][1] return count
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution(object): def maxProfitAssignment(self, difficulty, profit, worker): jobs = zip(difficulty, profit) jobs = sorted(jobs) ans = i = best = 0 for skill in sorted(worker): while i < len(jobs) and skill >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty, profit, worker): jobs = sorted(zip(difficulty, profit)) ans = i = best = 0 for skill in sorted(worker): while i < len(jobs) and skill >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: arr = [] for i in range(len(difficulty)): arr.append((difficulty[i], profit[i])) arr = sorted(arr, key=lambda x: x[0]) maxProfit, maxProfits = 0, [] for _, gain in arr: maxProfit = max(maxProfit, gain) maxProfits.append(maxProfit) total = 0 for person in worker: idx = self.uppperBsearch(arr, person) - 1 if idx > -1: total += maxProfits[idx] return total def uppperBsearch(self, arr, target): left = 0 right = len(arr) while left < right: mid = (left + right)//2 if target >= arr[mid][0]: left = mid + 1 else: right = mid return right
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: ls = sorted(range(len(difficulty)), key= lambda x : difficulty[x]) workers = sorted(worker) idx = 0 resp = 0 best = 0 for worker in workers: while idx < len(ls) and worker >= difficulty[ls[idx]]: best = max(best, profit[ls[idx]]) idx += 1 resp += best return resp
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit),key=lambda t:t[0]) ans = i = best = 0 for w in sorted(worker): while i < len(jobs) and w >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: tup=[] dic = {} for i in range(0,len(profit)): tup.append((profit[i],difficulty[i])) worker.sort(reverse=True) tup.sort(reverse=True) indx=0 t_indx=0 res=0 while(indx<len(worker)): while(t_indx < len(profit) and worker[indx]<tup[t_indx][1]): t_indx+=1 if(t_indx < len(profit)): res+=tup[t_indx][0] indx+=1 return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: worker = sorted(worker) jobs = sorted([(-1,0)]+[(difficulty[i],profit[i])for i in range(len(difficulty))]) job_nums = len(jobs) maxProfit = 0 job_index = 0 res = 0 for ability in worker: while job_index < job_nums-1 and ability>= jobs[job_index+1][0]: job_index+=1 maxProfit = max(maxProfit,jobs[job_index][1]) res+=maxProfit return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution(object): def maxProfitAssignment(self, difficulty, profit, worker): arr = [] for d, p in sorted(zip(difficulty, profit)): if arr and arr[-1][1]>=p: continue arr.append([d, p]) N = len(arr) res, i, best = 0, 0, 0 for w in sorted(worker): while i<N and w>=arr[i][0]: best = arr[i][1] i += 1 res += best return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: tup_list = sorted(list(zip(difficulty, profit))) filtered_list = [] best = 0 for d, p in tup_list: if p > best: best = p filtered_list.append((d, p)) worker = sorted(worker) profit = 0 dix = 0 for w in worker: if w < filtered_list[dix][0]: continue while dix+1 < len(filtered_list) and filtered_list[dix+1][0] <= w: dix += 1 profit += filtered_list[dix][1] return profit
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: val_dict = dict() for i in range(len(difficulty)): val_dict[difficulty[i]] = max(val_dict.get(difficulty[i], 0), profit[i]) key_list = list(val_dict.keys()) key_list = sorted(key_list) max_val = 0 max_list = [0] * len(key_list) for i in range(len(key_list)): max_val = max(max_val, val_dict[key_list[i]]) max_list[i] = max_val total = 0 #print(key_list, max_list) for w in worker: #print(w, self.bisect(w, key_list, max_list)) total += self.bisect(w, key_list, max_list) return total def bisect(self, w, key_list, max_list): left, right = 0, len(key_list)-1 if w < key_list[0]: return 0 if w >= key_list[right]: return max_list[right] while left != right - 1: mid = (left + right) // 2 if w > key_list[mid]: left = mid elif w < key_list[mid]: right = mid else: return max_list[mid] return max_list[left]
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: # 8:49 9/26/20 def find_difficulty(d, w): n = len(d) lt, rt = 0, n while lt < rt: mid = lt + (rt - lt) // 2 if d[mid] == w: return mid elif d[mid] > w: rt = mid else: lt = mid + 1 return rt-1 difficulty1 = sorted(difficulty) difficulty = list(zip(difficulty, profit)) difficulty.sort(key = lambda x: (x[0])) profit = {} max_profit = 0 for i in range(len(difficulty)): if difficulty[i][0] not in profit or profit[difficulty[i][0]] < difficulty[i][1]: profit[difficulty[i][0]] = difficulty[i][1] max_profit = max(max_profit, profit[difficulty[i][0]]) profit[difficulty[i][0]] = max_profit total = 0 pre = -1 for i in range(len(worker)): # if i > 0 and worker[i] == worker[i-1] and pre != -1: # total += profit[pre] # continue pre = find_difficulty(difficulty1, worker[i]) if pre != -1: total += profit[difficulty1[pre]] return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = list(zip(difficulty, profit)) jobs.sort(key=lambda x:x[1], reverse=True) jobs.sort(key=lambda x:x[0]) bestjobs=[] res = 0 for a,b in jobs: if not bestjobs: bestjobs.append((a,b)) elif bestjobs[-1][1]<b: bestjobs.append((a,b)) print(bestjobs) for wo in worker: lo, hi = 0, len(bestjobs)-1 while lo<hi: mid = (lo+hi+1)//2 if bestjobs[mid][0]>wo: hi = mid-1 else: lo = mid if bestjobs[lo][0]<=wo: res+=bestjobs[lo][1] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: m = len(difficulty) difficulty, profit = zip(*sorted(zip(difficulty, profit))) difficulty = list(difficulty) profit = list(profit) best_profit = m * [0] best_profit[0] = profit[0] ind = 0 prev_v = difficulty[0] for i in range(1, m): if prev_v != difficulty[i]: ind += 1 difficulty[ind] = prev_v = difficulty[i] best_profit[ind] = best_profit[ind - 1] best_profit[ind] = max(best_profit[ind], profit[i]) # print(best_profit) # print(difficulty) m = ind + 1 def bins(target): start = 0 end = m - 1 bsres = 0 while start <= end: middle = start + (end - start) // 2 if difficulty[middle] == target: return best_profit[middle] if difficulty[middle] < target: bsres = best_profit[middle] start = middle + 1 else: end = middle - 1 return bsres res = 0 h = dict() for w in worker: if w not in h: h[w] = bins(w) res += h[w] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: m = len(difficulty) difficulty, profit = zip(*sorted(zip(difficulty, profit))) difficulty = list(difficulty) profit = list(profit) best_profit = m * [0] best_profit[0] = profit[0] ind = 0 prev_v = difficulty[0] for i in range(1, m): if prev_v != difficulty[i]: ind += 1 difficulty[ind] = prev_v = difficulty[i] best_profit[ind] = best_profit[ind - 1] best_profit[ind] = max(best_profit[ind], profit[i]) # print(best_profit) # print(difficulty) m = ind + 1 def bins(target): start = 0 end = m - 1 bsres = 0 while start <= end: middle = start + (end - start) // 2 if difficulty[middle] == target: return best_profit[middle] if difficulty[middle] < target: bsres = best_profit[middle] start = middle + 1 else: end = middle - 1 return bsres res = 0 h = dict() for w in worker: # if w not in h: # h[w] = bins(w) res += bins(w) return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
# 20201007 1:23 am ref class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit)) profit = i = maxprofits = 0 for ability in sorted(worker): while i < len(jobs) and ability >= jobs[i][0]: profit = max(jobs[i][1], profit) i += 1 maxprofits += profit return maxprofits
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
from heapq import * class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: worker.sort() difficulty_min_heap = [] profit_max_heap = [] total = 0 for i in range(len(profit)): heappush(difficulty_min_heap, (difficulty[i], i)) for w in worker: while difficulty_min_heap and difficulty_min_heap[0][0] <= w: current_difficulty, job_id = heappop(difficulty_min_heap) heappush(profit_max_heap, (-profit[job_id], job_id)) if profit_max_heap: total += -profit_max_heap[0][0] return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: def binary_search(arr, target): l, r = 0, len(arr) - 1 res = -1 while l <= r: mid = (l + r) // 2 if arr[mid][0] <= target: res = mid l = mid + 1 else: r = mid - 1 return res jobs = sorted([d, p] for d,p in zip(difficulty, profit)) for i in range(1,len(jobs)): jobs[i][1] = max(jobs[i][1], jobs[i - 1][1]) res = 0 for w in worker: i = binary_search(jobs, w) if i == -1: continue res += jobs[i][1] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: for i in range(len(profit)): difficulty[i] = [difficulty[i], profit[i]] difficulty.sort() for i in range(1, len(profit)): difficulty[i][1] = max(difficulty[i-1][1], difficulty[i][1]) def search(d): if d < difficulty[0][0]: return -1 l = 0 r = len(profit)-1 while l < r: m = (l+r+1)//2 if difficulty[m][0] <= d: l = m else: r = m-1 return l res = 0 for w in worker: i = search(w) if i >= 0: res += difficulty[i][1] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = [[difficulty[i],profit[i]] for i in range(len(difficulty))] jobs.sort() worker.sort() ans, best ,i = 0, 0,0 for skill in worker: while i < (len(jobs)) and skill >= jobs[i][0]: best = max(best,jobs[i][1]) i += 1 ans += best return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: n = len(worker) jobs = sorted(zip(difficulty, profit)) total = 0 best = 0 i = 0 for ability in sorted(worker): while i < len(jobs) and ability >= jobs[i][0]: best = max(jobs[i][1], best) i += 1 total += best return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, d: List[int], p: List[int], work: List[int]) -> int: N, maxd = len(p), max(d) cmb = [(d[i], p[i]) for i in range(N)] cmb.sort() dd = [0]*(maxd+1) val = j = 0 for i in range(maxd+1): while j<N and i >= cmb[j][0]: val = max(val, cmb[j][1]) j += 1 dd[i] = val # print(dd) ans = 0 for w in work: if w>maxd: ans += dd[maxd] else: ans += dd[w] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: _max=max(worker)+1 make=[0]*_max num=[(difficulty[i],profit[i])for i in range(len(profit)) if difficulty[i]<_max] num.sort(key=lambda x:x[0]) p_max=0 for i in range(len(num)): if num[i][1]>=p_max: make[num[i][0]]=num[i][1] p_max=num[i][1] pre=-1 for i in range(len(make)): if make[i]!=0: pre=make[i] elif pre!=-1: make[i]=pre ans=0 for w in worker: ans+=make[w] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: dp = {} for i, d in enumerate(difficulty): dp[d] = max(profit[i], dp.get(d, 0)) dp = sorted([ (d,p) for d, p in dp.items() ]) d2p = [] pre = 0 for d, p in dp: pre = max(pre, p) d2p.append((d, pre)) def getjob(work, pv, pw, pi): l, r = 0, len(d2p) - 1 if d2p[l][0] > work: return 0, 0 if d2p[r][0] <= work: return d2p[r][1], r l = pi while l <= r: i = (l + r) // 2 if d2p[i][0] == work: return max(d2p[i][1], pv), i if d2p[i][0] > work: r = i - 1 else: l = i + 1 if i == r: return max(d2p[i][1], pv), i return max(d2p[i-1][1], pv), i - 1 rs = 0 pv = 0 pw = 0 pi = 0 for w in sorted(worker): pv, pi = getjob(w, pv, pw, pi) pw = w rs += pv return rs
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: dp = {} for i, d in enumerate(difficulty): dp[d] = max(profit[i], dp.get(d, 0)) dp = sorted([ (d,p) for d, p in dp.items() ]) d2p = [] pre = 0 for d, p in dp: pre = max(pre, p) d2p.append((d, pre)) def getjob(work): l, r = 0, len(d2p) - 1 if d2p[l][0] > work: return 0 if d2p[r][0] <= work: return d2p[r][1] while l <= r: i = (l + r) // 2 if d2p[i][0] == work: return d2p[i][1] if d2p[i][0] > work: r = i - 1 else: l = i + 1 if i == r: return d2p[i][1] return d2p[i-1][1] rs = 0 for w in worker: rs += getjob(w) return rs
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: _max=max(worker)+1 make=[0]*_max num=[(difficulty[i],profit[i])for i in range(len(profit)) if difficulty[i]<_max] num.sort(key=lambda x:x[0]) ## x=0 ## while x<len(num): ## if x+1<len(num): ## if num[x][1]>num[x+1][1]: ## num.pop(x+1) ## else:x+=1 ## else:break p_max=0 for i in range(len(num)): p_max=max(p_max,num[i][1]) if num[i][1]>=p_max:make[num[i][0]]=num[i][1] pre=-1 ## print(make) for i in range(len(make)): if make[i]!=0: pre=make[i] elif pre!=-1: make[i]=pre ans=0 for w in worker: ans+=make[w] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted([(difficulty[i], profit[i]) for i in range(len(difficulty))], key=lambda x: x[0]) worker = sorted(worker) jobPointer = 0 workerPointer = 0 maxProfit = 0 totalProfit = 0 while True: if workerPointer >= len(worker): break talent = worker[workerPointer] if jobPointer >= len(jobs) or jobs[jobPointer][0] > talent: totalProfit += maxProfit workerPointer += 1 else: if jobs[jobPointer][1] > maxProfit: maxProfit = jobs[jobPointer][1] jobPointer += 1 return totalProfit
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: _max=max(worker)+1 make=[0]*_max num=[(difficulty[i],profit[i])for i in range(len(profit)) if difficulty[i]<_max] num.sort(key=lambda x:x[0]) x=0 while x<len(num): if x+1<len(num): if num[x][1]>num[x+1][1]: num.pop(x+1) else:x+=1 else:break for i in range(len(num)): make[num[i][0]]=num[i][1] pre=-1 for i in range(len(make)): if make[i]!=0: pre=make[i] elif pre!=-1: make[i]=pre ans=0 for w in worker: ans+=make[w] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, d: List[int], p: List[int], work: List[int]) -> int: maxd = max(d) cmb1 = defaultdict(int) for i in range(len(p)): cmb1[d[i]] = max(cmb1[d[i]], p[i]) cmb = sorted(cmb1.items(), key=lambda x: x[0]) NN = len(cmb) dd = [0]*(maxd+1) val = j = 0 for i in range(maxd+1): if j<NN and i == cmb[j][0]: val = max(val, cmb[j][1]) j += 1 dd[i] = val ans = 0 for w in work: if w>maxd: ans += dd[maxd] else: ans += dd[w] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit), key = lambda x: x[0]) ans = i = best = 0 for skill in sorted(worker): while i < len(jobs) and skill >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: def findProfit(ppl, goodJobs): l=0 r=len(goodJobs)-1 while l<r-1: test=int((l+r)/2) if goodJobs[test][1]>ppl: l=test else: r=test if goodJobs[r][1]>ppl: return 0 elif goodJobs[l][1]>ppl: return goodJobs[r][0] return goodJobs[l][0] goodJobs=[] jobs=[(profit[i], difficulty[i]) for i in range(len(difficulty))] jobs=sorted(jobs, key=lambda job:job[0], reverse=True) goodJobs.append(jobs[0]) for i in range(1,len(jobs)): if jobs[i][1]<goodJobs[-1][1]: goodJobs.append(jobs[i]) ans=0 print((goodJobs, worker)) for ppl in worker: ans=ans+findProfit(ppl, goodJobs) print(ans) return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = list(zip(difficulty, profit)) jobs = sorted(jobs, key=lambda x: x[0]) worker.sort() idx = 0 ans = 0 p = 0 for skill in worker: while idx < len(jobs) and jobs[idx][0] <= skill: p = max(p, jobs[idx][1]) idx += 1 ans += p return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: worker.sort() difprof = [(difficulty[i], profit[i]) for i in range(len(difficulty))] difprof.sort() total = 0 index = 0 maxProfit = 0 for w in worker: while index < len(difprof) and w >= difprof[index][0]: maxProfit = max(maxProfit, difprof[index][1]) index += 1 total += maxProfit return total # n = len(worker) # dp = [0 for i in range(worker[n - 1]+1)] # index = 0 # for i in range(1, len(dp)): # if index < len(difficulty) and i == difficulty[index]: # dp[i] = max(dp[i-1], profit[index]) # index += 1 # else: # dp[i] = dp[i-1] # total = 0 # for w in worker: # total += dp[w] # return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: profit = [i[1] for i in sorted(zip(difficulty, profit))] difficulty.sort() for i in range(1, len(profit)): profit[i] = max(profit[i], profit[i-1]) difficulty.append(float('inf')) tot = 0 for w in range(len(worker)): low = 0 high = len(difficulty)-1 while low < high: mid = low + (high-low)//2 if difficulty[mid] > worker[w]: high = mid else: low = mid+1 low -= 1 if low < 0: continue tot += profit[low] return tot
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: jobs = sorted(zip(difficulty, profit)) res = i = best = 0 for ability in sorted(worker): while i < len(jobs) and ability >= jobs[i][0]: best = max(jobs[i][1], best) i += 1 res += best return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: mapProfit = dict() for i in range(len(difficulty)): if difficulty[i] in mapProfit: mapProfit[difficulty[i]] = max(mapProfit[difficulty[i]], profit[i]) else: mapProfit[difficulty[i]] = profit[i] l = list() maxDiff = max(difficulty) for i in range(maxDiff + 1): if i in mapProfit: l.append(mapProfit[i]) else: l.append(0) maxFromBegin = 0 for i in range(len(l)): if l[i] < maxFromBegin: l[i] = maxFromBegin else: maxFromBegin = l[i] # print(l) res = 0 for work in worker: if work > maxDiff: res += l[-1] else: res += l[work] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: n=len(profit) maxm=max(difficulty) dp=[0]*(1+maxm) for i in range(n): dp[difficulty[i]]=max(dp[difficulty[i]],profit[i]) for i in range(1,maxm+1): dp[i]=max(dp[i],dp[i-1]) res=0 for x in worker: if x>maxm: res+=dp[maxm] else: res+=dp[x] return res
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: # Max profits for each level of difficulty. max_diff = max(difficulty) max_profits = [0] * (max_diff + 1) # Set the initial values for diff, profit in zip(difficulty, profit): max_profits[diff] = max(max_profits[diff], profit) # Consider the profit from lower difficulty (ie. diff 1 profit 5 might be better than diff 5 profit 1) for i in range(1, max_diff + 1): max_profits[i] = max(max_profits[i], max_profits[i - 1]) # Figure out max profits. pft = 0 for wskill in worker: pft += max_profits[max_diff if wskill > max_diff else wskill] return pft
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
from bisect import bisect_right class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: dp = [(d, p) for d, p in zip(difficulty, profit)] dp.sort() G = [] C = [] for x, y in dp: if not G or G[-1] < x: G.append(x) C.append(y) continue if G[-1] == x: C[-1] = y for y in range(1, len(C)): if C[y-1] > C[y]: C[y] = C[y-1] ans = 0 for w in worker: idx = bisect_right(G, w)-1 if idx >= 0: ans += C[idx] return ans
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
def maximum_profit_for_difficulty(diff_job, difficulty, profit): return max([profit[job] for job in diff_job[difficulty]]) class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: min_difficulty = min(difficulty) max_difficulty = max(difficulty) least_difficult_job = difficulty.index(min_difficulty) diff_job = defaultdict(list) for i, diff in enumerate(difficulty): diff_job[diff].append(i) max_profit = [maximum_profit_for_difficulty(diff_job, min_difficulty, profit)] for i in range(1, max_difficulty-min_difficulty+1): current_difficulty = i + min_difficulty if current_difficulty in diff_job: current_job_profit = maximum_profit_for_difficulty(diff_job, current_difficulty, profit) max_profit.append(max(max_profit[i-1], current_job_profit)) else: max_profit.append(max_profit[i-1]) result = 0 for ability in worker: if ability >= max_difficulty: result += max_profit[-1] elif ability < min_difficulty: result += 0 else: result += max_profit[ability-min_difficulty] return result
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
# # Fisrt solution # def createListOfProfitDiff(profit, difficulty): # profit_diff = [] # for i in range(len(profit)): # profit_diff.append((profit[i], difficulty[i])) # return profit_diff # class Solution: # def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: # profit_diff = createListOfProfitDiff(profit, difficulty) # profit_diff.sort(key=lambda x:x[0]) # worker2 = sorted(worker) # maxprofit = 0 # for i in range(len(profit_diff)-1, -1, -1): # for j in range(len(worker2) -1, -1, -1): # if profit_diff[i][1]> worker2[j]: # break # else: # maxprofit += profit_diff[i][0] # worker2.pop(j) # return maxprofit # Second solution def createListOfProfitDiff(profit, difficulty): profit_diff = [] for i in range(len(profit)): profit_diff.append((profit[i], difficulty[i])) return profit_diff # class Solution: # def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: # profit_diff = createListOfProfitDiff(profit, difficulty) # profit_diff.sort(key=lambda x:x[0]) # worker2 = sorted(worker) # maxprofit = 0 # i = len(profit_diff) -1 # j = len(worker2)-1 # while i<len(profit_diff) and j <len(worker2) and i>-1 and j>-1: # if profit_diff[i][1]> worker2[j]: # i-=1 # else: # maxprofit += profit_diff[i][0] # worker2.pop(j) # j-=1 # return maxprofit class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: w = sorted(worker) w.reverse() dp = [] for i in range(len(profit)): dp.append((profit[i], difficulty[i])) dp.sort(key = lambda x:x[0]) dp.reverse() i = 0 count = 0 for p in dp: while i<len(w) and p[1] <= w[i]: count += p[0] i+=1 return count
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
from itertools import chain class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: li = list(chain(zip(profit, difficulty))) li.sort() cutoffs = [li[-1][1]] profits = [li[-1][0]] for i in range(len(profit)-2, -1, -1): if li[i][1] < cutoffs[0]: cutoffs.insert(0, li[i][1]) profits.insert(0, li[i][0]) cutoffs.insert(0, 0) profits.insert(0, 0) ans = 0 n = len(cutoffs) for ability in worker: l = 0 r = n-1 while(r >= l): mid = (l + r) // 2 if ability == cutoffs[mid]: break elif ability > cutoffs[mid]: l = mid + 1 else: r = mid - 1 mid = mid - 1 for i in range(3): if mid+1 < n: if ability >= cutoffs[mid+1]: mid += 1 ans += profits[mid] return(ans)
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: combined = [] for i in range(len(difficulty)): combined.append((difficulty[i], profit[i])) combined = sorted(combined, key=lambda x: x[0]) total = 0 i = 0 best_profit = 0 for w in sorted(worker): while i < len(combined) and w >= combined[i][0]: best_profit = max(best_profit, combined[i][1]) i += 1 # best_profit is the most profit you can get for current worker, if the next worker has the same workload capability, he will do add to the total doing with the same best_profit. total += best_profit return total
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: profit = [i[1] for i in sorted(zip(difficulty, profit))] difficulty.sort() for i in range(1, len(profit)): profit[i] = max(profit[i], profit[i-1]) difficulty.append(float('inf')) worker.sort() tot = 0 low = 0 for w in range(len(worker)): low = 0 high = len(difficulty)-1 while low < high: mid = low + (high-low)//2 if difficulty[mid] > worker[w]: high = mid else: low = mid+1 low -= 1 if low < 0: low = 0 continue tot += profit[low] return tot
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
def maximum_profit_for_difficulty(diff_job, difficulty, profit): return max([profit[job] for job in diff_job[difficulty]]) class Solution: def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int: min_difficulty = min(difficulty) max_difficulty = max(difficulty) diff_job = defaultdict(list) for i, diff in enumerate(difficulty): diff_job[diff].append(i) max_profit = [maximum_profit_for_difficulty(diff_job, min_difficulty, profit)] for i in range(1, max_difficulty-min_difficulty+1): current_difficulty = i + min_difficulty if current_difficulty in diff_job: current_job_profit = maximum_profit_for_difficulty(diff_job, current_difficulty, profit) max_profit.append(max(max_profit[i-1], current_job_profit)) else: max_profit.append(max_profit[i-1]) result = 0 for ability in worker: if ability >= max_difficulty: result += max_profit[-1] elif ability < min_difficulty: result += 0 else: result += max_profit[ability-min_difficulty] return result