leetcode_数据结构_入门_121. 买卖股票的最佳时机

121. 买卖股票的最佳时机

 

 

问题:

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。
设计一个算法来计算你所能获取的最大利润。

返回可以从这笔交易中获取的最大利润,
如果不能获取任何利润,返回 0 。


思路:

我们需要找出给定数组中两个数字之间的最大差值(即,最大利润)★。
此外,第二个数字(卖出价格)必须大于第一个数字(买入价格)。

形式上,对于每组 i 和 j(其中 j > i)
我们需要找出 max(prices[j]−prices[i])。

 

 

 

代码:

 

main函数

复制代码
package DataStructure_start;

public class DS20230113 {

    public static void main(String[] args) {

        int[] prices = {6, 8, 3, 4, 8, 9, 1};
        int[] prices2 = {7, 1, 5, 3, 6, 4};
        int maxProfit = maxProfit(prices);
        int maxProfit2 = maxProfit2(prices2);

        System.out.println(maxProfit);
        System.out.println(maxProfit2);
    }
}
复制代码


方法一:暴力法

思路:
挨个遍历两遍,找到两个数,计算出最大差值

复杂度分析
时间复杂度:O(n^2),循环运行 n (n-1)/2次。
空间复杂度:O(1)只使用了常数个变量。

复制代码
    public static int maxProfit(int[] prices) {

        int maxprofit = 0;

//        第一遍遍历
        for (int i = 0; i < prices.length - 1; i++) {
//            紧跟着遍历第二个数
            for (int j = i + 1; j < prices.length; j++) {

//                设置profit为两数的差值
                int profit = prices[j] - prices[i];
//                如果差值大于“目前的”最大值
                if (profit > maxprofit) {
//                    则将目前的差值赋值给maxfprofit
                    maxprofit = profit;
                }
            }
        }
//        返回最大差值
        return maxprofit;
    }
复制代码


方法二:一次遍历

我们来假设自己来购买股票。
随着时间的推移,每天我们都可以选择出售股票与否。
那么,假设在第 i 天,如果我们要在今天卖股票,那么我们能赚多少钱呢?

显然,如果我们真的在买卖股票,我们肯定会想:
如果我是在历史最低点买的股票就好了!
太好了,在题目中,我们只要用一个变量记录一个历史最低价格 minprice,我们就可以假设自己的股票是在那天买的。
那么我们在第 i 天卖出股票能得到的利润就是 prices[i] - minprice。

因此,我们只需要遍历价格数组一遍,记录历史最低点,然后在每一天考虑这么一个问题:
如果我是在历史最低点买进的,那么我今天卖出能赚多少钱?
当考虑完所有天数之时,我们就得到了最好的答案。


复杂度分析

时间复杂度:O(n),只需要遍历一次。
空间复杂度:O(1),只使用了常数个变量。

复制代码
    public static int maxProfit2(int prices[]) {

        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;

//        遍历一次
        for (int i = 0; i < prices.length; i++) {
//            将小的值赋值给minprice(找最低点)
            if (prices[i] < minprice) {
                minprice = prices[i];
//                如果差值大于maxprofit,则将最大差值赋值给maxprofit
            } else if (prices[i] - minprice > maxprofit) {
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }
复制代码

 

补充:
  Integer.MAX_VALUE:

public final class Integer extends Number implements Comparable<java.lang.Integer> {
*/
/**
* A constant holding the minimum value an {@code int} can
* have, -2<sup>31</sup>.
*//*

@Native
public static final int MIN_VALUE = 0x80000000;

*/
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*//*

@Native public static final int MAX_VALUE = 0x7fffffff;

*/
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since JDK1.1


参考:
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
posted @   yzhone  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示