LeetCode - Best Time to Buy and Sell 2

这道题我一开始想到用递归方法,可以把规模大的问题变成规模小的问题,但是觉得递归的时间复杂度很高,因为它会把相同的问题进行重复计算,然后我想是不是有什么down-up的方法,先把所有的子问题的结果保存起来,但是发现问题的最优解并不能由子问题的最优解推导出来。最后就想到买股票的时候,我们在一个局部极小的时候买进,然后在一个局部极大的时候卖出,这样就可以通过多次买进卖出交易获得最大收益。接下来的问题就是,确定什么时候是极小值,什么时候是极大值。

下面是AC代码:


 1     /**
2 * Design an algorithm to find the maximum profit. You may complete as many transactions as you like
3 * (ie, buy one and sell one share of the stock multiple times).
4 * However, you may not engage in multiple transactions at the same time
5 * (ie, you must sell the stock before you buy again).
6 * @param prices
7 * @return
8 */
9 public int maxProfit2(int[] prices){
10 if(prices==null || prices.length <= 1)
11 return 0;
12 int profit = 0;
13 int len = prices.length;
14 //the local minimum is marked as -1;the local maximum is marked as 1; otherwise is 0
15 int[] mark = new int[len];
16 int[] left = new int[len];//mark whether the left is <= or >=
17 int[] right = new int[len];//mark whether the right is <= or >=
18 // the first number is initialized as 0
19 left[0] = 0;
20 //get the left array, by one traversal
21 int i=1;
22 while(i<len){
23 if(prices[i-1]<prices[i])
24 left[i] = -1;
25 else if(prices[i-1]>prices[i])
26 left[i] = 1;
27 else
28 left[i] = left[i-1];
29 i++;
30 }
31 //initialize the last element
32 right[len-1] = 0;
33 i = len-2;
34 while(i>=0){
35 if(prices[i+1]<prices[i])
36 right[i] = -1;
37 else if(prices[i+1]>prices[i])
38 right[i] = 1;
39 else
40 right[i] = right[i+1];
41 i--;
42 }
43 //get the mark
44 i = 0;
45 while(i<len){
46 if(left[i] == right[i]){
47 mark[i] = left[i]*(-1);
48 }else {
49 if(left[i] == 0){
50 mark[i] = right[i]*(-1);
51 }
52 else if(right[i] == 0)
53 mark[i] = left[i]*(-1);
54 else
55 mark[i] = 0;
56 }
57 i++;
58 }
59 int buy=0 ,sell = 0;
60 boolean f1 = false, f2 = false;
61 for(i=0;i<len;i++){
62 if(f1==false && mark[i] == -1)//at the minimum time buy.
63 {
64 buy = prices[i];
65 f1 = true;
66 }
67 if(f2 == false && f1 == true && mark[i] == 1)//at the maximum time sell, by the condition that we have bought the stock;
68 {
69 sell = prices[i];
70 f2 = true;
71 }
72 if(f1 && f2)
73 {
74 profit += (sell - buy);
75 f1 = false;
76 f2 = false;
77 }
78 }
79 return profit;
80 }

LeetCode - Best Time to Buy and Sell 2,码迷,mamicode.com

时间: 2024-08-08 09:29:43

LeetCode - Best Time to Buy and Sell 2的相关文章

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode: Best Time to Buy and Sell Stock II [122]

[题目] 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

LeetCode: Best Time to Buy and Sell Stock III [123]

[题目] 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 at most two transactions. Note: You may not engage in multiple transactions at the same time

[leetcode]_Best Time to Buy and Sell Stock I &amp;&amp; II

一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换.要求只买卖一次,获得最大收益. 思路:一开始我认为是寻找最大.最小值,但由于最大值不一定总是出现在最小值的后面,因此WA. 参考思路:DP.对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比. 代码: 1 public int max

LeetCode - Best Time to Buy and Sell

这道题要求的一个min和一个max,只是这个min所在的位置要在max所在位置的左边. 有一种做法是采用蛮力算法,也就是通过从左往右遍历,把每一个元素都当做min,然后再在这个元素的右边找一个最大值,这样得到一个profit,最后求得所有情况中profit的最大值即刻.但是这种做法的时间复杂度是O(n^2); 另一种做法是通过时间换空间的方法,我先用两个数组:数组1和数组2,数组1[i]表示从prices的第0个数到第i个数的最小值,数组2[i]表示从prices的第[len-1]个数到第i个数

LeetCode: Best Time to Buy and Sell Stock [121]

[题目] Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

LeetCode——Best Time to Buy and Sell Stock III

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 at most two transactions. Note: You may not engage in multiple transactions at the same time (ie,

LeetCode Best Time to Buy and Sell Stock with Cooldown

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目: 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

[LeetCode] Best Time to Buy and Sell Stock 合集

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 给出股票每一天的价格,求解只做一次交易的最佳收益方案. 思路是首先建立一个存储股票每天差价的数组 diff[],其中 diff[i] = prices[i] - prices[i - 1],这样原问题就变成了最大连续子数组和的问题. int maxProfit(int* prices, int pricesSize) { if (!pricesSize) return 0