121. Best Time to Buy and Sell Stock

本文介绍了一个简单的算法来解决股票交易问题,即在最多进行一次买卖的情况下,如何通过跟踪最低股价和计算最大利润来实现最优交易决策。算法的时间复杂度为O(n),空间复杂度为O(1),适用于理解如何利用动态变化的数据集进行决策。

题目:

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.

链接: http://leetcode.com/problems/best-time-to-buy-and-sell-stock/

题解:

维护一个localMin和一个globalMax。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0)
            return 0;
        int maxProfix = Integer.MIN_VALUE, localMin = Integer.MAX_VALUE; 
        
        for(int i = 0; i < prices.length; i ++){
            if(prices[i] < localMin)
                localMin = prices[i];
            maxProfix = Math.max(maxProfix, prices[i] - localMin);
        }
        
        return maxProfix;
    }
}

Update:

public class Solution {
    public int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0)
            return 0;
        int localMin = Integer.MAX_VALUE, maxProfit = 0;
        
        for(int i = 0; i < prices.length; i++) {
            maxProfit = Math.max(maxProfit, prices[i] - localMin);
            if(prices[i] < localMin)
                localMin = prices[i];
        }
        
        return maxProfit;
    }
}

 

二刷:

这里主要维护一个localMin,来保存在当前 i 左边的股票最小值。我们的最大profit是指在一个递增的sequence里最大值与最小值的差。 假如整个prices数组是递减的,那么我们的profit为0。 

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null) return 0;
        int leftMin = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            maxProfit = Math.max(maxProfit, prices[i] - leftMin);
            leftMin = Math.min(leftMin, prices[i]);
        }
        return maxProfit;
    }
}

 

三刷:

不用每次都更新localMin和maxProfit。可以使用if else来减少更新的频率,加快速度。这里当localMin < prices的时候,我们才需要尝试更新maxProfit。

Java:

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int localMin = Integer.MAX_VALUE;
        int maxProfit = 0;
        
        for (int i = 0; i < prices.length; i++) {
            if (localMin > prices[i]) {
                localMin = prices[i];
            } else if (maxProfit < prices[i] - localMin) {
                maxProfit = prices[i] - localMin;
            }
        }
        
        return maxProfit;
    }
}

 

 

测试:

Case 1:  { 1 }         Res: 0

Case 2:  { 1, 2 }     Res: 1

Case 3:  { 2, 1 }     Res: 0

转载于:https://www.cnblogs.com/yrbbest/p/4438458.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值