Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 71a9081

Browse files
committed
docs: 新增No121题解
1 parent e32635f commit 71a9081

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

leetcode刷题/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
1717
- [No.98 验证二叉搜索树](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No98_is-valid-BST.md)
1818
- [No.104 二叉树的最大深度](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No104_max-depth.md)
19+
- [No.121 买卖股票的最佳时机](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No121_max-profit.md)
1920
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
2021
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
2122
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# No.121 买卖股票的最佳时机
2+
3+
难度:`easy`
4+
5+
6+
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
7+
8+
如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
9+
10+
注意你不能在买入股票前卖出股票。
11+
12+
## 示例
13+
14+
示例 1:
15+
16+
```
17+
输入: [7,1,5,3,6,4]
18+
输出: 5
19+
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
20+
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
21+
```
22+
23+
示例 2:
24+
```
25+
输入: [7,6,4,3,1]
26+
输出: 0
27+
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
28+
```
29+
30+
## 解题思路
31+
32+
**解题思路1:**
33+
34+
暴力法,两重循环,找到利润最大。
35+
36+
代码如下:
37+
38+
```javascript
39+
/**
40+
* @param {number[]} prices
41+
* @return {number}
42+
*/
43+
var maxProfit = function(prices) {
44+
let maxProfit = 0;
45+
// 暴力法
46+
for (let i = 0, n = prices.length; i < n; i++) {
47+
for (let j = i+1; j < n; j++) {
48+
let profit = prices[j] - prices[i];
49+
if (profit > maxProfit)
50+
maxProfit = profit;
51+
}
52+
}
53+
return maxProfit;
54+
};
55+
```
56+
57+
**解题思路二:**
58+
59+
一遍循环,循环的时候就计算出当前最大的利润,最后取利润的最大值。
60+
61+
代码如下:
62+
63+
```javascript
64+
/**
65+
* @param {number[]} prices
66+
* @return {number}
67+
*/
68+
var maxProfit = function(prices) {
69+
let maxProfit = 0;
70+
let lastPrice = prices[0];
71+
let profitArr = [];
72+
for (let i = 1, n = prices.length; i < n; i++) {
73+
if (prices[i] > lastPrice) {
74+
profitArr.push(prices[i]-lastPrice);
75+
} else {
76+
lastPrice = prices[i];
77+
}
78+
}
79+
console.log(profitArr);
80+
return profitArr.length == 0 ? 0 : Math.max(...profitArr);
81+
};
82+
```

0 commit comments

Comments
 (0)