LeetCode-Best Time to Buy and Sell Stock I&&II

第一:题意是一个数组里保存着某支股票的股价情况。第i个为第i天的价格。求最大收益。允许买卖一次

这道题就是求最大的差值。可以记录下最小的值,然后基于最小值,找出当前的最大差值。

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length==0||prices.length==1)
        {
            return 0;
        }
        int low=prices[0],ans=0;
        for(int i=1;i<prices.length;i++)
        {
            if(low>prices[i]) low=prices[i];    //记录最小值
            else
            {
                if(prices[i]-low>ans)
                {
                    ans = prices[i]-low;
                }
            }
        }
        return ans;
    }
}

第二:题意在第一题的基础上允许多次买卖。但是必须先买再卖,不能连续买或者连续卖。

这道题就是求所有差值相加。可以想象将股价想象为一个折线统计图。将所有上升的段的差值相加即为结果。

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length==0||prices.length==1)
        {
            return 0;
        }
        int low = prices[0];
        int high = 0;
        int sum = 0;
        for(int i = 1;i<prices.length;i++)
        {
            if(low>prices[i])   //若当前股价低于最低值,进行低吸
            {
                low=prices[i];
            }
            else
            {
                if((i<prices.length-1)&&(prices[i+1]>prices[i]))  //若后面的值比当前值还大(还处于上升阶段)
                {
                    continue;
                }
                else     //若当前为一个顶峰点,则卖出股票
                {
                    sum = sum+prices[i]-low;
                    low = prices[i];
                }

            }
        }
        return sum;
    }
}
时间: 2024-10-12 20:02:42

LeetCode-Best Time to Buy and Sell Stock I&&II的相关文章

[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 Stock I,II,III,IV

三种股票交易算法 一.交易次数没有限制 使用贪心策略,找最长递增序列,同时累加相应利润. 二.只有一次交易 使用动态规划算法,从前往后,依次记记录相应时间节点前面的最小price,同时获得在这个节点的最大利润,同时更新最小price 三.最多两次 使用两次动态规划 1.从左向右,记录在相应的时间节点卖出的最大利润,实际上就是问题二 2.从右向左,记录在相应的时间节点买入的最大利润,因此这里需要记录最大的price. 将each 的left[index]+ right[index]曲最大值. 四.

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I 题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益. 分析:动态规划法.从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. 代码:时间O(n),空间O(1). Best Time to Buy and Sell Stock II 题目:用一

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 [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,

Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III

题目1:Best Time to Buy and Sell Stock 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 algori

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a