Skip to content

Commit f3bf24f

Browse files
committedJun 7, 2022
feat: add solutions to lc problem: No.2291
No.2291.Maximum Profit From Trading Stocks
1 parent 19b8a93 commit f3bf24f

File tree

6 files changed

+164
-7
lines changed

6 files changed

+164
-7
lines changed
 

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

+59-3
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,50 @@ It can be shown that the maximum profit you can make is 0.
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:动态规划**
62+
63+
$0-1$ 背包问题。
64+
6165
<!-- tabs:start -->
6266

6367
### **Python3**
6468

6569
<!-- 这里可写当前语言的特殊实现逻辑 -->
6670

6771
```python
68-
72+
class Solution:
73+
def maximumProfit(self, present: List[int], future: List[int], budget: int) -> int:
74+
arr = [(a, b - a) for a, b in zip(present, future) if b > a]
75+
dp = [0] * (budget + 1)
76+
for v, w in arr:
77+
for j in range(budget, v - 1, -1):
78+
dp[j] = max(dp[j], dp[j - v] + w)
79+
return dp[-1]
6980
```
7081

7182
### **Java**
7283

7384
<!-- 这里可写当前语言的特殊实现逻辑 -->
7485

7586
```java
76-
87+
class Solution {
88+
public int maximumProfit(int[] present, int[] future, int budget) {
89+
List<int[]> arr = new ArrayList<>();
90+
for (int i = 0; i < present.length; ++i) {
91+
if (future[i] > present[i]) {
92+
arr.add(new int[]{present[i], future[i] - present[i]});
93+
}
94+
}
95+
int[] dp = new int[budget + 1];
96+
for (int[] e : arr) {
97+
int v = e[0], w = e[1];
98+
for (int j = budget; j >= v; --j) {
99+
dp[j] = Math.max(dp[j], dp[j - v] + w);
100+
}
101+
}
102+
return dp[budget];
103+
}
104+
}
77105
```
78106

79107
### **TypeScript**
@@ -98,7 +126,7 @@ class Solution {
98126
public:
99127
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
100128
int n = present.size();
101-
vector<int>dp(budget + 1, 0);
129+
vector<int>dp(budget + 1);
102130
for (int i = 0; i < n; i++) {
103131
for (int j = budget; j >= present[i]; j--) {
104132
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
@@ -109,6 +137,34 @@ public:
109137
};
110138
```
111139
140+
### **Go**
141+
142+
```go
143+
func maximumProfit(present []int, future []int, budget int) int {
144+
arr := [][]int{}
145+
for i, v := range present {
146+
if future[i] > v {
147+
arr = append(arr, []int{v, future[i] - v})
148+
}
149+
}
150+
dp := make([]int, budget+1)
151+
for _, e := range arr {
152+
v, w := e[0], e[1]
153+
for j := budget; j >= v; j-- {
154+
dp[j] = max(dp[j], dp[j-v]+w)
155+
}
156+
}
157+
return dp[budget]
158+
}
159+
160+
func max(a, b int) int {
161+
if a > b {
162+
return a
163+
}
164+
return b
165+
}
166+
```
167+
112168
### **...**
113169

114170
```

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

+55-3
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,37 @@ It can be shown that the maximum profit you can make is 0.
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def maximumProfit(self, present: List[int], future: List[int], budget: int) -> int:
64+
arr = [(a, b - a) for a, b in zip(present, future) if b > a]
65+
dp = [0] * (budget + 1)
66+
for v, w in arr:
67+
for j in range(budget, v - 1, -1):
68+
dp[j] = max(dp[j], dp[j - v] + w)
69+
return dp[-1]
6370
```
6471

6572
### **Java**
6673

6774
```java
68-
75+
class Solution {
76+
public int maximumProfit(int[] present, int[] future, int budget) {
77+
List<int[]> arr = new ArrayList<>();
78+
for (int i = 0; i < present.length; ++i) {
79+
if (future[i] > present[i]) {
80+
arr.add(new int[]{present[i], future[i] - present[i]});
81+
}
82+
}
83+
int[] dp = new int[budget + 1];
84+
for (int[] e : arr) {
85+
int v = e[0], w = e[1];
86+
for (int j = budget; j >= v; --j) {
87+
dp[j] = Math.max(dp[j], dp[j - v] + w);
88+
}
89+
}
90+
return dp[budget];
91+
}
92+
}
6993
```
7094

7195
### **TypeScript**
@@ -90,7 +114,7 @@ class Solution {
90114
public:
91115
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
92116
int n = present.size();
93-
vector<int>dp(budget + 1, 0);
117+
vector<int>dp(budget + 1);
94118
for (int i = 0; i < n; i++) {
95119
for (int j = budget; j >= present[i]; j--) {
96120
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
@@ -101,6 +125,34 @@ public:
101125
};
102126
```
103127
128+
### **Go**
129+
130+
```go
131+
func maximumProfit(present []int, future []int, budget int) int {
132+
arr := [][]int{}
133+
for i, v := range present {
134+
if future[i] > v {
135+
arr = append(arr, []int{v, future[i] - v})
136+
}
137+
}
138+
dp := make([]int, budget+1)
139+
for _, e := range arr {
140+
v, w := e[0], e[1]
141+
for j := budget; j >= v; j-- {
142+
dp[j] = max(dp[j], dp[j-v]+w)
143+
}
144+
}
145+
return dp[budget]
146+
}
147+
148+
func max(a, b int) int {
149+
if a > b {
150+
return a
151+
}
152+
return b
153+
}
154+
```
155+
104156
### **...**
105157

106158
```

‎solution/2200-2299/2291.Maximum Profit From Trading Stocks/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
44
int n = present.size();
5-
vector<int>dp(budget + 1, 0);
5+
vector<int>dp(budget + 1);
66
for (int i = 0; i < n; i++) {
77
for (int j = budget; j >= present[i]; j--) {
88
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func maximumProfit(present []int, future []int, budget int) int {
2+
arr := [][]int{}
3+
for i, v := range present {
4+
if future[i] > v {
5+
arr = append(arr, []int{v, future[i] - v})
6+
}
7+
}
8+
dp := make([]int, budget+1)
9+
for _, e := range arr {
10+
v, w := e[0], e[1]
11+
for j := budget; j >= v; j-- {
12+
dp[j] = max(dp[j], dp[j-v]+w)
13+
}
14+
}
15+
return dp[budget]
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maximumProfit(int[] present, int[] future, int budget) {
3+
List<int[]> arr = new ArrayList<>();
4+
for (int i = 0; i < present.length; ++i) {
5+
if (future[i] > present[i]) {
6+
arr.add(new int[]{present[i], future[i] - present[i]});
7+
}
8+
}
9+
int[] dp = new int[budget + 1];
10+
for (int[] e : arr) {
11+
int v = e[0], w = e[1];
12+
for (int j = budget; j >= v; --j) {
13+
dp[j] = Math.max(dp[j], dp[j - v] + w);
14+
}
15+
}
16+
return dp[budget];
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maximumProfit(self, present: List[int], future: List[int], budget: int) -> int:
3+
arr = [(a, b - a) for a, b in zip(present, future) if b > a]
4+
dp = [0] * (budget + 1)
5+
for v, w in arr:
6+
for j in range(budget, v - 1, -1):
7+
dp[j] = max(dp[j], dp[j - v] + w)
8+
return dp[-1]

0 commit comments

Comments
 (0)