-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathStock.ts
60 lines (56 loc) · 1.73 KB
/
Stock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* 买卖股票的最佳时机 只能买一次
* @param {number[]} prices
* @return {number}
*/
function maxProfitOnce (prices: number[]): number {
// 价格最低点
let minPrice = Infinity
// 可获得的最高利润
let maxProfit = 0
for(let i = 0; i < prices.length; i++) {
// 如果价格更低 将此时价格更改为最低价格
if (prices[i] < minPrice) {
minPrice = prices[i]
} else if (prices[i] - minPrice > maxProfit) {
maxProfit = prices[i] - minPrice
}
}
return maxProfit
}
/**
* 买卖股票的最佳时机II 可以买多次
* @param {number[]} prices
* @return {number}
*/
function maxProfitMore (prices: number[]): number {
// 存储总利润
let profit = 0
// 赚取每一次 == 赚得最多
for(let i = 0; i < prices.length - 1; i++) {
// 只要后面的价格比前面高,就完成一次交易
if (prices[i+1] - prices[i] > 0) {
profit += prices[i+1] - prices[i]
}
}
return profit
}
/**
* 买卖股票的最佳时机 含手续费
* @param {number[]} prices
* @param {number} fee
*/
function maxProfitWithFee (prices: number[], fee: number): number {
// 定义变量存储当我们不持有股票的最大利润
let cash = 0
// 定义变量存储当我们持有股票时最大利润
let hold = -prices[0]
for(let i = 1; i < prices.length; i++) {
// 当天可选择不进行交易或者卖出手头持有股票变现
cash = Math.max(cash, hold + prices[i] - fee)
// 当天可选择不急行交易或者购入股票
hold = Math.max(hold, cash - prices[i])
}
return cash
}
export {maxProfitOnce, maxProfitMore, maxProfitWithFee}