• 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  这道题我还不会写,等写出来再更新

  • 相关阅读:
    整数转换成字符
    html总结(一)
    ssh服务
    原码反码补码图形化注意
    关于副本机制
    win8设置自动关机
    python制作的翻译器基于爬取百度翻译【笔记思路】
    python多线程扫描爆破网站服务器思路【笔记】
    用python实现多线程爬取影视网站全部视频方法【笔记】
    利用Python3的requests和re库爬取猫眼电影笔记
  • 原文地址:https://www.cnblogs.com/wangnanabuaa/p/4961733.html
Copyright © 2020-2023  润新知