Skip to content

Commit 449a2bf

Browse files
committed
feat: add c++ solution to lc problem: No.2291
No.2291.Maximum Profit From Trading Stocks
1 parent c914d71 commit 449a2bf

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ function maximumProfit(present: number[], future: number[], budget: number): num
9191
};
9292
```
9393

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
100+
int n = present.size();
101+
vector<int>dp(budget + 1, 0);
102+
for (int i = 0; i < n; i++) {
103+
for (int j = budget; j >= present[i]; j--) {
104+
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
105+
}
106+
}
107+
return dp.back();
108+
}
109+
};
110+
```
111+
94112
### **...**
95113
96114
```

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

+18
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ function maximumProfit(present: number[], future: number[], budget: number): num
8383
};
8484
```
8585

86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
92+
int n = present.size();
93+
vector<int>dp(budget + 1, 0);
94+
for (int i = 0; i < n; i++) {
95+
for (int j = budget; j >= present[i]; j--) {
96+
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
97+
}
98+
}
99+
return dp.back();
100+
}
101+
};
102+
```
103+
86104
### **...**
87105
88106
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
4+
int n = present.size();
5+
vector<int>dp(budget + 1, 0);
6+
for (int i = 0; i < n; i++) {
7+
for (int j = budget; j >= present[i]; j--) {
8+
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
9+
}
10+
}
11+
return dp.back();
12+
}
13+
};

0 commit comments

Comments
 (0)