码迷,mamicode.com
首页 > 其他好文 > 详细

股票问题

时间:2018-06-17 18:55:07      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:div   ann   new   leetcode   tip   max   必须   计算   val   

leetcode上的几道题,意思是知道股票的走势,怎么获得买卖最大利润,卖的日子必须在买的日子的后面,每一道题的限制不一样,用到的算法也可能不一样。

121. 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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

  Note that you cannot sell a stock before you buy one.

这一道题的意思是只能买一次,求最大的利润。

思路如下:

  (1)假设从第一个开始买

  (2)如果当天的价格大于当前买的价格,计算利润是否大于最大值,如果大于最大值,就更新最大值

  (3)如果当天的价格小于当前买的价格,当前买的价格更新为当天的价格

  (4)重复以上步骤知道遍历完

代码如下:

class Solution {
    public int maxProfit(int[] prices) {
        if(prices.length == 0){
            return 0;
        }
        int max = 0, temp = prices[0];
        for (int i = 1; i < prices.length; i++){
            if (temp < prices[i]){
                max = Math.max(prices[i] - temp, max);
            }else{
                temp = prices[i];
            }
        }
        return max;
    }
}

122. Best Time to Buy and Sell Stock II

题干如下:

  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 (i.e., buy one and sell one share of the stock multiple times).

  这一道题的意思是想买多少次就买多少次,求最大的利润。

思路如下:

  这道题的思路就是遍历每一天,如果后一天的价格大于当天的,就加进去。

代码如下:

class Solution {
    public int maxProfit(int[] prices) {
        int count = 0;
        for (int i = 0; i < prices.length - 1; i++){
            if (prices[i] < prices[i + 1]){
                count += prices[i + 1] - prices[i];
            }
        }
        return count;
    }
}

123. 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.

  这一道题的意思是最多只能买卖两次,求最大的利润。

代码如下:

class Solution {
    public int maxProfit(int[] prices) {
        int firstBuy = Integer.MIN_VALUE, firstSell = 0;
        int secondBuy = Integer.MIN_VALUE, secondSell = 0;

        for (int price : prices){
            if (firstBuy < -price){
                firstBuy = -price;
            }
            if (firstSell < price + firstBuy){
                firstSell = price + firstBuy;
            }
            if (secondBuy < firstSell - price){
                secondBuy = firstSell - price;
            }
            if (secondSell < price + secondBuy){
                secondSell = price + secondBuy;
            }
        }
        return secondSell;
    }
}

188. Best Time to Buy and Sell Stock IV

  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 k transactions.

  这一道题的意思是最多只能买卖k次,求最大的利润。

代码如下:

class Solution {
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        if (k >= n / 2){//见122. Best Time to Buy and Sell Stock II
            int max = 0;
            for (int i = 1; i < n; i++){
                if (prices[i] > prices[i - 1]){
                    max += prices[i] - prices[i - 1];
                }
            }
            return max;
        }
        int [][] maxProfit = new int[k + 1][n];
        for (int i = 1; i <= k; i++){
            int localMax = -prices[0];//localMax是没卖第j个股票时的最小亏损或最大利润 像上一题的secondBuy
            for (int j = 1; j < n; j++){
                maxProfit[i][j] = Math.max(maxProfit[i][j - 1], prices[j] + localMax);
                localMax = Math.max(localMax, maxProfit[i - 1][j - 1] - prices[j]);
            }
        }
        return maxProfit[k][n - 1];
    }
}

股票问题

标签:div   ann   new   leetcode   tip   max   必须   计算   val   

原文地址:https://www.cnblogs.com/gouden/p/9193330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!