You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -192,28 +192,120 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
192
192
193
193
<!-- solution:start -->
194
194
195
-
### Solution 2: Memoization
195
+
### Solution 2: Dynamic Programming
196
+
197
+
Let's denote $m = \max(\text{difficulty})$ and define an array $f$ of length $m + 1$, where $f[i]$ represents the maximum profit among jobs with difficulty less than or equal to $i$, initially $f[i] = 0$.
198
+
199
+
Then, we iterate over the jobs, and for each job $(d, p)$, if $d \leq m$, we update $f[d] = \max(f[d], p)$.
200
+
201
+
Next, we iterate from $1$ to $m$, and for each $i$, we update $f[i] = \max(f[i], f[i - 1])$.
202
+
203
+
Finally, we iterate over the workers, and for each worker $w$, we add $f[w]$ to the answer.
204
+
205
+
The time complexity is $O(n + M)$, and the space complexity is $O(M)$. Here, $n$ is the length of the `profit` array, and $M$ is the maximum value in the `difficulty` array.
0 commit comments