Skip to content

Commit

Permalink
Best Time to Buy and Sell Stock II
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Mar 5, 2018
1 parent 358ea8b commit 7e3933c
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/com/bzh/leetcode/array/MaxProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.bzh.leetcode.array;

import org.junit.Assert;
import org.junit.Test;

/**
* https://leetcodechina.com/explore/suan-fa/card/chu-ji-suan-fa/1/di-yi-zhang-jie/22/
* 假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格。
* 设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。
* <p>
* <p>
* <p>
* 单次交易利润 = 低点买入 + 高点卖出
* <p>
* 总利润 = 多次交易利润总和
* <p>
*/
public class MaxProfile {

@Test
public void test() {
Assert.assertEquals(0, maxProfit(new int[]{}));
Assert.assertEquals(0, maxProfit(new int[]{10, 1}));
Assert.assertEquals(9, maxProfit(new int[]{1, 10}));
Assert.assertEquals(9, maxProfit(new int[]{1, 10, 10}));
Assert.assertEquals(9, maxProfit(new int[]{1, 10, 10, 10, 10}));
Assert.assertEquals(9, maxProfit(new int[]{1, 1, 10, 10}));
Assert.assertEquals(18, maxProfit(new int[]{1, 1, 10, 10, 1, 10}));
Assert.assertEquals(9, maxProfit(new int[]{1, 1, 10, 10, 1, 1, 1, 0}));
Assert.assertEquals(17, maxProfit(new int[]{10, 0, 1, 10, 3, 5, 6, 5, 6, 4, 7, 1}));

// 若prices[maxIndex] > prices[i] (转折出现)
// minIndex = i;
// maxIndex = i;
// minIndex <= maxIndex && maxProfit += prices[maxIndex] - prices[minIndex];
// 若prices[minIndex] >= prices[i]
// minIndex = i;
// 若prices[maxIndex] <= prices[i]
// maxIndex = i;
// 最后 minIndex <= maxIndex && maxProfit += prices[maxIndex] - prices[minIndex];
}

public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}

int maxProfit = 0;

int minIndex = 0;
int maxIndex = 0;

for (int i = 0; i < prices.length; i++) {
if (prices[maxIndex] > prices[i]) {
if (minIndex <= maxIndex) {
maxProfit += prices[maxIndex] - prices[minIndex];
}
minIndex = i;
maxIndex = i;
} else if (prices[minIndex] >= prices[i]) {
minIndex = i;
} else if (prices[maxIndex] <= prices[i]) {
maxIndex = i;
}
}

if (minIndex <= maxIndex) {
maxProfit += prices[maxIndex] - prices[minIndex];
}

return maxProfit;
}
}

0 comments on commit 7e3933c

Please sign in to comment.