Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62e468a

Browse files
feat: add typescript solution to lc problem: No.2291
No.2291.Maximum Profit From Trading Stocks
1 parent 57e0f6c commit 62e468a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

‎solution/2200-2299/2291.Maximum Profit From Trading Stocks/README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ It can be shown that the maximum profit you can make is 0.
7979
### **TypeScript**
8080

8181
```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+
};
8392
```
8493

8594
### **...**

‎solution/2200-2299/2291.Maximum Profit From Trading Stocks/README_EN.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,16 @@ It can be shown that the maximum profit you can make is 0.
7171
### **TypeScript**
7272

7373
```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+
};
7584
```
7685

7786
### **...**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /