股票问题(二)

一、 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 (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

eg:尽可能多的交易来保证利润最大

Q:you must sell the stock before you buy again?
      我的理解就是当天同一时间不能卖完再买! 这样可以省掉许多的加法操作。

Example:
     1 3 5 6    返回 0 +(6-1)而不是 0 + 3-1 + 5-3 + 6-5(当天卖完又可以买)实际上结果是没有变化的
     1 2 3 1 5 返回 3-1 + 5 -1。 当前不能同一时间买卖。

我的理解:
    不能在同一天卖出和买入,意思是下次购买前,你必须全部卖出你手上的股票。
    1 2 3 :如果1买入,2卖出则不能再2买入,3抛出了,会损失一个利润点。正确的是1买入,3卖出。

 另一种考虑:其实只要后面的大于前面的就可以买卖了,典型的贪婪算法。
(其实这两种算法都能AC = =  !!! )

public static int maxProfit(int[] prices){
// 贪婪方法,如果不允许同一天买卖的话,那么就不能应用    
        int total = 0;
        for(int i = 0; i < prices.length - 1; i ++){
            if(prices[i + 1] > prices[i]) total += prices[i+1] - prices[i];
        }
        return total;
}
public static int maxProfit(int[] prices){
//不用贪婪,当两者结果是一样的
        int profit = 0, i = 0;
        while(i < prices.length){
            //找局部最小值
            while(i < prices.length - 1 && prices[i + 1] <= prices[i]) i++;
            //找到了就开始下一个
            int min = prices[i++];
            //找写一个局部最大值
            while(i < prices.length - 1 && prices[i + 1] >= prices[i]) i++;
            profit = profit + (i < prices.length ? prices[i++] - min : 0);
        }
        return profit;
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
股票经纪人问题是一种经典的贪心算法问题,它可以用Java来实现。下面是一个基本的Java代码实现: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); int[] prices = new int[n]; for (int i = 0; i < n; i++) { prices[i] = scanner.nextInt(); } System.out.println(maxProfit(prices, k)); } private static int maxProfit(int[] prices, int k) { if (prices == null || prices.length <= 1 || k <= 0) { return 0; } if (k >= prices.length / 2) { int maxProfit = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; } int[][] dp = new int[k + 1][prices.length]; for (int i = 1; i <= k; i++) { int maxDiff = -prices[0]; for (int j = 1; j < prices.length; j++) { dp[i][j] = Math.max(dp[i][j - 1], prices[j] + maxDiff); maxDiff = Math.max(maxDiff, dp[i - 1][j - 1] - prices[j]); } } return dp[k][prices.length - 1]; } } ``` 其中,maxProfit方法实现了股票经纪人问题的解法。该方法使用动态规划算法,时间复杂度为O(kn),其中n是价格数组的长度,k是允许的最大交易次数。具体来说,该方法使用一个维数组dp来保存状态,其中dp[i][j]表示在前j个价格中进行i次交易的最大利润。在每次更新dp[i][j]时,需要枚举第j个价格卖出的时机,即在前j-1个价格中买入,然后在第j个价格卖出。同时,还需要维护一个变量maxDiff,用于计算在前j-1个价格中进行i-1次交易的最大利润减去第j个价格的差值,以此来更新dp[i][j]。最终,dp[k][n-1]即为问题的解。 在main方法中,首先读入价格数组和允许的最大交易次数,然后调用maxProfit方法求解并输出结果。需要注意的是,如果允许的最大交易次数大于等于价格数组长度的一半,那么问题可以转化为多次买卖,最大利润即为所有相邻价格差的和。否则,使用动态规划算法求解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值