best-time-to-buy-and-sell-stock I &&II && III && IVbest-time-to-buy-and-sell-stock-ii

1、买卖股票的最佳时机

假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。

 1 public class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      */
 6     public int maxProfit(int[] prices) {
 7         // write your code here if(prices.length<2)  return 0;
 8         if(prices.length<2)  return 0;
 9         int maxProfit = 0;
10         int minprice = prices[0];
11         for(int i=1;i<prices.length;i++){
12             minprice = Math.min(minprice , prices[i]);
13             maxProfit = Math.max(maxProfit , prices[i]-minprice);
14         }
15         return maxProfit;
16     }
17 }

2、买卖股票的最佳时机 II

假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。

这个也可以算作比较简单的贪心算法,当前的价格如果高于昨日的价格,我们就进行交易,直到遍历完毕。

 1 class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      */
 6     public int maxProfit(int[] prices) {
 7         // write your code here
 8         if(prices.length<2)  return 0;
 9         int maxProfit = 0;
10         for(int i=1;i<prices.length;i++){
11             int diff=prices[i]-prices[i-1];
12             if(diff>0){
13                 maxProfit = maxProfit+diff;
14             }
15         }
16         return maxProfit;
17     }
18 }

3、买卖股票的最佳时机 III

假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格。设计一个算法来找到最大的利润。你最多可以完成两笔交易。

样例

给出一个样例数组 [4,4,6,1,1,4,2,5], 返回 6

注意

你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票)

分析思路:这个问题和第二题的感觉又不一样,使用的是动态规划的思路,可以将这个问题一分为二,既然最多完成两笔交易,假设第一交易发生在第i天,即可将这个问题看成i之前求最大利润和i之后求最大利润,最后两个加和。通过i的遍历,即可找出来整个数组的最佳i,获得最大利润。

class Solution {
    /**
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    public int maxProfit(int[] prices) {
        // write your code here
        if(prices.length < 2)  return 0;
        int n=prices.length;
        int[] profit1=new int [n];
        int[] profit2=new int [n];

        for(int i=0;i<n;i++){
            int minprice=prices[0];
            int maxprofit=0;
            for(int j=0;j<i;j++){
                minprice = Math.min(minprice,prices[j]);
                maxprofit = Math.max(maxprofit,prices[j]-minprice);
            }
            profit1[i]=maxprofit;
            minprice=prices[i];
            maxprofit=0;
            for(int j=i;j<n;j++){
                minprice = Math.min(minprice,prices[j]);
                maxprofit = Math.max(maxprofit,prices[j]-minprice);
            }
            profit2[i]=maxprofit;
        }
        int maxProfit = 0 ;
        int curprofit = 0;
        for(int i=0;i<n;i++){
            curprofit=profit1[i]+profit2[i];
            if(curprofit>maxProfit) maxProfit=curprofit;
        }
        return maxProfit;
    }
};

买卖股票的最佳时机 IV  这道题我还不会写,等写出来再更新

时间: 2024-11-25 13:34:12

best-time-to-buy-and-sell-stock I &&II && III && IVbest-time-to-buy-and-sell-stock-ii的相关文章

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

【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][JAVA] Best Time to Buy and Sell Stock I, II, III

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 algorithm

Best Time to Buy and Sell Stock I,II,III [leetcode]

Best Time to Buy and Sell Stock I 只能作一次操作时:维护preMin记录之前出现的最小值 代码如下: int maxProfit(vector<int> &prices) { if (prices.size() == 0) return 0; int profit = 0; int preMin = prices[0]; for (int i = 1; i < prices.size(); i++) { if (prices[i] < pr

lintcode medium Best Time to Buy and Sell Stock I,II,III

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 algorithm

leetcode--Best Time to Buy and Sell Stock i ii iii

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 algorithm

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: 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), desi

leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is inte

LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV

Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for (int i=1;i<prices.size();++i){ if (prices[i]>prices[i-1]){ res += prices[i]-prices[i-1]; } } return res; } }; Best Time to Buy