Skip to content

Commit bc7bfd1

Browse files
committed
feat: add 122
1 parent 2f2bc61 commit bc7bfd1

File tree

8 files changed

+227
-52
lines changed

8 files changed

+227
-52
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
|118|[Pascal's Triangle][118]|Array|
4040
|119|[Pascal's Triangle II][119]|Array|
4141
|121|[Best Time to Buy and Sell Stock][121]|Array, Dynamic Programmin|
42+
|122|[Best Time to Buy and Sell Stock II][122]|Array, Greedy|
4243

4344

4445
## Medium
@@ -93,6 +94,7 @@
9394
[118]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/118/README.md
9495
[119]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/119/README.md
9596
[121]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/121/README.md
97+
[122]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/122/README.md
9698

9799
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
98100
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md

note/122/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [Best Time to Buy and Sell Stock II][title]
2+
3+
## Description
4+
5+
Say you have an array for which the *i*<sup>th</sup> element is the price of a given stock on day *i*.
6+
7+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
8+
9+
**Tags:** Array, Greedy
10+
11+
12+
## 思路
13+
14+
题意是给出一个数组代表每天的股票金额,在每天只能买或卖的情况下求出收益最高值,这...,这也太简单了吧,把所有相邻递增的值都加起来即可。
15+
16+
``` java
17+
class Solution {
18+
public int maxProfit(int[] prices) {
19+
int max = 0;
20+
for (int i = 1; i < prices.length; ++i) {
21+
if (prices[i] > prices[i - 1]) max += prices[i] - prices[i - 1];
22+
}
23+
return max;
24+
}
25+
}
26+
```
27+
28+
29+
## 结语
30+
31+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
32+
33+
34+
35+
[title]: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii
36+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+135-52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.blankj.easy._121;
2+
3+
4+
/**
5+
* <pre>
6+
* author: Blankj
7+
* blog : http://blankj.com
8+
* time : 2017/10/11
9+
* desc :
10+
* </pre>
11+
*/
12+
public class Solution {
13+
public int maxProfit(int[] prices) {
14+
int max = 0, minPrice = Integer.MAX_VALUE;
15+
for (int i = 0; i < prices.length; ++i) {
16+
if (prices[i] < minPrice) minPrice = prices[i];
17+
int delta = prices[i] - minPrice;
18+
if (delta > max) max = delta;
19+
}
20+
return max;
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
System.out.println(solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
26+
System.out.println(solution.maxProfit(new int[]{7, 6, 4, 3, 1}));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.blankj.easy._122;
2+
3+
4+
/**
5+
* <pre>
6+
* author: Blankj
7+
* blog : http://blankj.com
8+
* time : 2017/10/11
9+
* desc :
10+
* </pre>
11+
*/
12+
public class Solution {
13+
public int maxProfit(int[] prices) {
14+
int max = 0;
15+
for (int i = 1; i < prices.length; ++i) {
16+
if (prices[i] > prices[i - 1]) max += prices[i] - prices[i - 1];
17+
}
18+
return max;
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution solution = new Solution();
23+
System.out.println(solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
24+
System.out.println(solution.maxProfit(new int[]{7, 6, 4, 3, 1}));
25+
}
26+
}

0 commit comments

Comments
 (0)