File tree 3 files changed +30
-2
lines changed
solution/2200-2299/2291.Maximum Profit From Trading Stocks
3 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -79,7 +79,16 @@ It can be shown that the maximum profit you can make is 0.
79
79
### ** TypeScript**
80
80
81
81
``` ts
82
-
82
+ function maximumProfit(present : number [], future : number [], budget : number ): number {
83
+ let packet = present .map ((v , i ) => [v , future [i ] - v ]);
84
+ let dp = new Array (budget + 1 ).fill (0 );
85
+ for (let [v, w] of packet ) {
86
+ for (let j = budget ; j >= v ; j -- ) {
87
+ dp [j ] = Math .max (dp [j ], dp [j - v ] + w );
88
+ }
89
+ }
90
+ return dp [budget ];
91
+ };
83
92
```
84
93
85
94
### ** ...**
Original file line number Diff line number Diff line change @@ -71,7 +71,16 @@ It can be shown that the maximum profit you can make is 0.
71
71
### ** TypeScript**
72
72
73
73
``` ts
74
-
74
+ function maximumProfit(present : number [], future : number [], budget : number ): number {
75
+ let packet = present .map ((v , i ) => [v , future [i ] - v ]);
76
+ let dp = new Array (budget + 1 ).fill (0 );
77
+ for (let [v, w] of packet ) {
78
+ for (let j = budget ; j >= v ; j -- ) {
79
+ dp [j ] = Math .max (dp [j ], dp [j - v ] + w );
80
+ }
81
+ }
82
+ return dp [budget ];
83
+ };
75
84
```
76
85
77
86
### ** ...**
Original file line number Diff line number Diff line change
1
+ function maximumProfit ( present : number [ ] , future : number [ ] , budget : number ) : number {
2
+ let packet = present . map ( ( v , i ) => [ v , future [ i ] - v ] ) ;
3
+ let dp = new Array ( budget + 1 ) . fill ( 0 ) ;
4
+ for ( let [ v , w ] of packet ) {
5
+ for ( let j = budget ; j >= v ; j -- ) {
6
+ dp [ j ] = Math . max ( dp [ j ] , dp [ j - v ] + w ) ;
7
+ }
8
+ }
9
+ return dp [ budget ] ;
10
+ } ;
You can’t perform that action at this time.
0 commit comments