题目及测试用例
package pid122;
//买卖股票的最佳时机 II
//
//给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
//
//设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
//
//注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
//
//示例 1:
//
//输入: [7,1,5,3,6,4]
//输出: 7
//解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
// 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
//
//示例 2:
//
//输入: [1,2,3,4,5]
//输出: 4
//解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
// 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
// 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
//
//示例 3:
//
//输入: [7,6,4,3,1]
//输出: 0
//解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
public class main {
public static void main(String[] args) {
int[][] testTable = {{7,1,5,3,6,4},{1,2,3,4,5},{7,6,4,3,1},{1,6,2,7}};
for (int[] ito : testTable) {
test(ito);
}
}
private static void test(int[] ito) {
Solution solution = new Solution();
int rtn;
long begin = System.currentTimeMillis();
for (int i = 0; i < ito.length; i++) {
System.out.print(ito[i]+" ");
}
System.out.println();
//开始时打印数组
rtn = solution.maxProfit(ito);//执行程序
long end = System.currentTimeMillis();
//System.out.println(ito + ": rtn=" + rtn);
System.out.println(ito + ": rtn=" +rtn);
// for (int i = 0; i < ito.length; i++) {
// System.out.print(ito[i]+" ");
// }//打印结果几数组
System.out.println();
System.out.println("耗时:" + (end - begin) + "ms");
System.out.println("-------------------");
}
}
解答1(成功,7ms,很慢)
package pid122;
public class Solution {
public int maxProfit(int[] prices) {
int buy=0;
int sell=0;
int next;
int now;
int length=prices.length;
boolean wantBuy=true;
int profit;
int sum=0;
if(length==0||length==1){
return 0; //异常情况
}
for(int i=0;i<length-1;i++){ //从第二个价格开始
now=prices[i];
next=prices[i+1];
if(wantBuy==true){
if(now<next){
buy=now;
wantBuy=false;
}
}
else{
if(now>next){
sell=now;
profit=sell-buy;
sum=sum+profit;
wantBuy=true;
//System.out.println("buy="+buy+" sell="+sell+" profit="+profit);
}
}
}
if(wantBuy==false){
sell=prices[length-1];
profit=sell-buy;
sum=sum+profit;
wantBuy=true;
System.out.println("buy="+buy+" sell="+sell+" profit="+profit);
}
return sum;
}
}
解答2(成功,2ms,很快,优化过的代码,附带思路)
package pid122;
public class Solution {
/* 算法思想1:
基本思想为贪婪算法,在每个阶段得到最大的利润,得到每个上升坡道的利润
设置标志,是否要买,初始为true
从第一个循环到最后一个
标志为true,要买
如果这个比后面的那个小,则买入
标志为false,要卖
如果这个比后面的大,则卖出
*/
public int maxProfit(int[] prices) {
int buy=0;
int sell=0;
int next;
int now;
int length=prices.length;
boolean wantBuy=true;
int profit;
int sum=0;
if(length==0||length==1){
return 0; //异常情况
}
next=prices[0];
for(int i=0;i<length-1;i++){ //从第二个价格开始
now=next;
next=prices[i+1];
if(wantBuy==true){
if(now<next){
buy=now;
wantBuy=false;
}
}
else{
if(now>next){
sell=now;
profit=sell-buy;
sum=sum+profit;
wantBuy=true;
//System.out.println("buy="+buy+" sell="+sell+" profit="+profit);
}
}
}
if(wantBuy==false){
sell=next;
profit=sell-buy;
sum=sum+profit;
wantBuy=true;
//System.out.println("buy="+buy+" sell="+sell+" profit="+profit);
}
return sum;
}
}
解答3(成功,别人的,精妙的做法)
具体思路为从后往前减,能减多少减多少
package leetCode;
/**
* 2018.7.19 买卖股票的最佳时机II
* 不要想的太复杂
* @author dhc
*
*/
public class OneHundredAndTwentyTwo {
public static int maxProfit(int[] prices) {
int sum = 0;
for(int i = prices.length - 1;i>0;i--) {
if(prices[i] <= prices[i-1]) {
continue;
} else {
sum += prices[i] - prices[i-1];
}
}
return sum;
}
public static void main(String[] args) {
int[] nums = new int[] {7,6,5,9,1,3};
System.out.println(maxProfit(nums));
}
}