forked from cherryljr/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBest Time to Buy and Sell Stock II.java
42 lines (34 loc) · 1.27 KB
/
Best Time to Buy and Sell Stock II.java
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
和Stock I 的区别:可以买卖多次,求总和的最大盈利。
思路:
本题中买卖次数无限制,所以为了利润最大化我们要找到所有的 波峰 和 波谷。
故遍历整个数组,只要前后两个数之间的差值为正数,则说明可以获利。
将 profit 加上二者的差值即可。
/*
Say you have an array for which the ith element is the price of a given stock on day i.
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).
Example
Given an example [2,1,2,0,1], return 2
Tags Expand
Greedy Enumeration Array
*/
public class Solution {
/*
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profit = 0;
for (int i = 0; i < prices.length - 1; i++) {
int diff = prices[i + 1] - prices[i];
if (diff > 0) {
profit += diff;
}
}
return profit;
}
}