File tree Expand file tree Collapse file tree 1 file changed +7
-10
lines changed
solution/0800-0899/0826.Most Profit Assigning Work Expand file tree Collapse file tree 1 file changed +7
-10
lines changed Original file line number Diff line number Diff line change 1
1
function maxProfitAssignment ( difficulty : number [ ] , profit : number [ ] , worker : number [ ] ) : number {
2
- const wMax = Math . max ( ...worker ) ;
3
- const jobs = Array ( wMax + 1 ) . fill ( 0 ) ;
2
+ const m = Math . max ( ...difficulty ) ;
3
+ const f = Array ( m + 1 ) . fill ( 0 ) ;
4
4
const n = profit . length ;
5
-
6
- for ( let i = 0 ; i < n ; i ++ ) {
5
+ for ( let i = 0 ; i < n ; ++ i ) {
7
6
const d = difficulty [ i ] ;
8
- if ( d <= wMax ) jobs [ d ] = Math . max ( jobs [ d ] , profit [ i ] ) ;
7
+ f [ d ] = Math . max ( f [ d ] , profit [ i ] ) ;
9
8
}
10
-
11
- for ( let i = 1 , x = 0 ; i <= wMax ; i ++ ) {
12
- jobs [ i ] = Math . max ( jobs [ i ] , jobs [ i - 1 ] ) ;
9
+ for ( let i = 1 ; i <= m ; ++ i ) {
10
+ f [ i ] = Math . max ( f [ i ] , f [ i - 1 ] ) ;
13
11
}
14
-
15
- return worker . reduce ( ( acc , w ) => acc + jobs [ w ] , 0 ) ;
12
+ return worker . reduce ( ( acc , w ) => acc + f [ Math . min ( w , m ) ] , 0 ) ;
16
13
}
You can’t perform that action at this time.
0 commit comments