Skip to content

Commit 7a7d91f

Browse files
committed
feat: add ts solution to lc problem: No.0826
1 parent f46367c commit 7a7d91f

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

solution/0800-0899/0826.Most Profit Assigning Work/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,35 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
190190

191191
<!-- solution:end -->
192192

193+
<!-- solution:start -->
194+
195+
### Solution 2: Memoization
196+
197+
<!-- tabs:start -->
198+
199+
#### TypeScript
200+
201+
```ts
202+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
203+
const wMax = Math.max(...worker);
204+
const jobs = Array(wMax + 1).fill(0);
205+
const n = profit.length;
206+
207+
for (let i = 0; i < n; i++) {
208+
const d = difficulty[i];
209+
if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]);
210+
}
211+
212+
for (let i = 1, x = 0; i <= wMax; i++) {
213+
jobs[i] = Math.max(jobs[i], jobs[i - 1]);
214+
}
215+
216+
return worker.reduce((acc, w) => acc + jobs[w], 0);
217+
}
218+
```
219+
220+
<!-- tabs:end -->
221+
222+
<!-- solution:end -->
223+
193224
<!-- problem:end -->

solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,35 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
190190

191191
<!-- solution:end -->
192192

193+
<!-- solution:start -->
194+
195+
### Solution 2: Memoization
196+
197+
<!-- tabs:start -->
198+
199+
#### TypeScript
200+
201+
```ts
202+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
203+
const wMax = Math.max(...worker);
204+
const jobs = Array(wMax + 1).fill(0);
205+
const n = profit.length;
206+
207+
for (let i = 0; i < n; i++) {
208+
const d = difficulty[i];
209+
if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]);
210+
}
211+
212+
for (let i = 1, x = 0; i <= wMax; i++) {
213+
jobs[i] = Math.max(jobs[i], jobs[i - 1]);
214+
}
215+
216+
return worker.reduce((acc, w) => acc + jobs[w], 0);
217+
}
218+
```
219+
220+
<!-- tabs:end -->
221+
222+
<!-- solution:end -->
223+
193224
<!-- problem:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
2+
const wMax = Math.max(...worker);
3+
const jobs = Array(wMax + 1).fill(0);
4+
const n = profit.length;
5+
6+
for (let i = 0; i < n; i++) {
7+
const d = difficulty[i];
8+
if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]);
9+
}
10+
11+
for (let i = 1, x = 0; i <= wMax; i++) {
12+
jobs[i] = Math.max(jobs[i], jobs[i - 1]);
13+
}
14+
15+
return worker.reduce((acc, w) => acc + jobs[w], 0);
16+
}

0 commit comments

Comments
 (0)