java 动态规划判断股票最大盈利问题

来源:互联网 发布:配电网数据采集与监控 编辑:程序博客网 时间:2024/04/30 14:35

题目:

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.

解题思路:

这道题非常直观也非常简单。想要通过一次买卖得到的利润,就是要找到一组 i 和 j ,使得 prices[j] - prices[i] 最大,并且满足 i < j 。因为第二个约束条件,我们不会傻到找一个最大值和最小值并且返回它们的差。

假设 f[i] 为到第 i 天为止可以拿到的最大利润。对于第 i 天,有两种选择,即在当天卖掉股票,或者在第 i 天之前已经卖掉了。那么 f[i] 就是这两种选择中的最大值。如果在第 i 天卖掉股票,那么问题就是在哪天买股票,只要维护一个到第 i 天为止股价的最小值 minPrice 就可以了,此时 f[i] = prices[i] - minPrice ;如果在第 i 天股票已经卖出,则 f[i] = f[i-1] 。

综上所述,令 f[i] 表示到第 i 天为止可以拿到的最大利润。状态转移方程为 f[i] = max(f[i-1], prices[i] - minPrice) ,其中 minPrice 表示到第 i 天为止的最低股价,并且有 minPrice = min(minPrice, prices[i]) 。边界条件为 f[0] = 0, minPrice = prices[0] 。最终结果为 f[n-1] 。时间复杂度与空间复杂度均为 O(n) 。

观察状态方程可以发现, f[i] 的值只与 f 数组中的 f[i-1] 有关。也就是说,在计算 f[i] 时,只要保留 f[i-1] 的值就好了,其他的值都可以不保存。据此可以优化空间复杂度。

令 profit 表示到第 i 天为止可以拿到的最大利润。状态转移方程为 profit = max(profit, prices[i] - minPrice) 。 profit 初始化为 0 。最终结果即保存在 profit 中。时间复杂度为 O(n) ,空间复杂度为 O(1) 。

解法如下:

public class Solution {    public int maxProfit(int[] prices) {        if(prices==null||prices.length==0){            return 0;        }        int max = 0;        int[] f = new int[prices.length];        int minPrice = prices[0];        f[0] = 0;        for(int i=1;i<f.length;i++){            f[i] = max(f[i-1],prices[i]-minPrice);            minPrice = min(minPrice,prices[i]);            if(f[i]>max)                max = f[i];        }        return max;    }    public int max(int a,int b){        return a>b?a:b;    }    public int min(int a,int b){        return a<b?a:b;    }}
1 0
原创粉丝点击