Skip to content

Commit fb22d19

Browse files
committed
stock fee
1 parent 1b0c41b commit fb22d19

22 files changed

+2228
-1848
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
M
2+
1531728802
3+
tags: Array, DP, Greedy, Sequence DP, Status DP
4+
time: O(n)
5+
space: O(n), O(1) rolling array
6+
7+
跟Stock II 一样, 买卖无限, 需先买在卖. 附加条件: 每个sell transaction要加一笔fee.
8+
9+
#### Sequence DP
10+
- 与StockII一样, dp[i]: represents 前i天的最大profit.
11+
- sell 的时候, 才完成了一次transaction, 需要扣fee; 而买入不扣fee.
12+
- model sell on dp[i] day (which depends on dp[i-1]) and each day can be sell/buy => add status to dp[i][status]
13+
- status[0] buy on this day, status[1] sell on this day
14+
- dp[i][0] = Math.max(dp[i-1][0], dp[i - 1][0] - prices[i]);
15+
- dp[i][1] = Math.max(dp[i-1][1], dp[i - 1][1] + prices[i] - fee);
16+
- init: dp[0][0,1] = 0; dp[1][1] = 0; dp[1][0] = - prices;
17+
- return dp[n][1]
18+
19+
```
20+
/*
21+
Your are given an array of integers prices,
22+
for which the i-th element is the price of a given stock on day i;
23+
and a non-negative integer fee representing a transaction fee.
24+
25+
You may complete as many transactions as you like,
26+
but you need to pay the transaction fee for each transaction.
27+
You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)
28+
29+
Return the maximum profit you can make.
30+
31+
Example 1:
32+
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
33+
Output: 8
34+
Explanation: The maximum profit can be achieved by:
35+
Buying at prices[0] = 1
36+
Selling at prices[3] = 8
37+
Buying at prices[4] = 4
38+
Selling at prices[5] = 9
39+
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
40+
Note:
41+
42+
0 < prices.length <= 50000.
43+
0 < prices[i] < 50000.
44+
0 <= fee < 50000.
45+
*/
46+
47+
/*
48+
- sequence dp. dp[i] represents max profit for first i days.
49+
- model sell on dp[i] day (which depends on dp[i-1]) and each day can be sell/buy => add status to dp[i][status]
50+
- status[0] buy on this day, status[1] sell on this day
51+
- dp[i][0] = Math.max(dp[i-1][0], dp[i - 1][0] - prices[i]);
52+
- dp[i][1] = Math.max(dp[i-1][1], dp[i - 1][1] + prices[i] - fee);
53+
- init: dp[0][0,1] = 0; dp[1][1] = 0; dp[1][0] = - prices;
54+
return dp[n][1]
55+
*/
56+
class Solution {
57+
public int maxProfit(int[] prices, int fee) {
58+
if (prices == null || prices.length <= 1) return 0;
59+
// init dp[][] with n+1
60+
int n = prices.length;
61+
int[][] dp = new int[n + 1][2];
62+
dp[0][0] = dp[0][1] = 0;
63+
dp[1][1] = 0;
64+
dp[1][0] = - prices[0];
65+
66+
// calculate dp
67+
for (int i = 2; i <= n; i++) {
68+
dp[i][0] = Math.max(dp[i-1][0], dp[i - 1][1] - prices[i - 1]);
69+
dp[i][1] = Math.max(dp[i-1][1], dp[i - 1][0] + prices[i - 1] - fee);
70+
}
71+
72+
return dp[n][1];
73+
}
74+
}
75+
76+
// Rolling array
77+
class Solution {
78+
public int maxProfit(int[] prices, int fee) {
79+
if (prices == null || prices.length <= 1) return 0;
80+
// init dp[][] with n+1
81+
int n = prices.length;
82+
int[][] dp = new int[2][2];
83+
dp[0][0] = dp[0][1] = 0;
84+
dp[1][1] = 0;
85+
dp[1][0] = - prices[0];
86+
87+
// calculate dp
88+
for (int i = 2; i <= n; i++) {
89+
dp[i%2][0] = Math.max(dp[(i - 1)%2][0], dp[(i - 1)%2][1] - prices[i - 1]);
90+
dp[i%2][1] = Math.max(dp[(i - 1)%2][1], dp[(i - 1)%2][0] + prices[i - 1] - fee);
91+
}
92+
93+
return dp[n%2][1];
94+
}
95+
}
96+
```

Java/Longest Increasing Subsequence.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
M
2-
1516863852
3-
tags: Binary Search, DP, Sequence DP, Coordinate DP, Memoization
2+
1531724912
3+
tags: Binary Search, DP, Coordinate DP, Memoization
4+
time: O(n^2) dp, O(nLogN) binary search
5+
space: O(n)
46

57
无序数组, 找最长的上升(不需要连续)数组 的长度. 先做O(n^2), 然后可否O(nLogN)?
68

79
#### DP, double for loop, O(n^2)
8-
- 考虑nums[i]结尾的时候, [0, i) 里count有多少小于nums[i]
9-
- 对于所有 i in [0, n), 最常的increasing序列有多少length?
10+
- 找subsequence: 不需要continous, 可以skip candidate
11+
- 考虑nums[i]结尾的时候, [0, i), dp[i - 1] 里count有多少小于nums[i]
12+
- dp[i]: 到i为止 (对于所有 j in [0, i], 记录max length of increasing subsequence
1013
- max需要在全局维护: nums是无序的, nums[i]也可能是一个很小的值, 所以末尾dp[i]并不是全局的max, 而只是对于nums[i]的max.
1114
- 正因此, 每个nums[i]都要和每个nums[j] 作比较, j < i.
1215
- dp[i] = Maht.max(dp[i], dp[j] + 1); j = [0 , i - 1]
1316
- 时间复杂度 O(n^2)
1417

15-
1618
#### O(nLogN)
1719
- 维持一个list of increasing sequence
1820
- 这个list其实是一个base-line, 记录着最低的increasing sequence.

Java/Maximum Subarray.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
E
22
1525331164
33
tags: DP, Sequence DP, Array, Divide and Conquer, DFS, PreSum
4+
time: O(n)
5+
space: O(n), O(1) rolling array
46

57
给一串数组, 找数组中间 subarray 数字之和的最大值
68

79
#### Sequence DP
8-
- dp[i]: 前i个element, 包括element i 在内的 continous subsequence 的最大sum是多少?
10+
- dp[i]: 前i个element,包括 last element (i-1), 可能组成的 subarray 的最大sum.
911
- init: dp = int[n + 1], dp[0]: first 0 items, does not have any sum
1012
- 因为continous sequence, 所以不满足条件的时候, 会断. That is: need to take curr num, regardless => can drop prev max in dp[i]
1113
- track overall max
@@ -77,7 +79,7 @@ public int maxSubArray(int[] nums) {
7779
dp[0] = 0;
7880
int max = Integer.MIN_VALUE;
7981
for (int i = 1; i <= n; i++) {
80-
dp[i] = Math.max(dp[i - 1] + nums[i - 1], nums[i - 1]);
82+
dp[i] = Math.max(dp[i - 1] + nums[i - 1], nums[i - 1]); // contious, or start from nums[i-1]
8183
max = Math.max(max, dp[i]);
8284
}
8385

KnowledgeHash.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,8 @@ private class PathSum {
14161416

14171417
# Pain Point
14181418
- For any array access, make sure to check the boundary!!!
1419+
- subsequence: not continuous, can skip candidate!
1420+
- subarray: continous!
14191421

14201422
## NP-Complete problems
14211423
### wiki

0 commit comments

Comments
 (0)