121. Best Time to Buy and Sell Stock

121. Best Time to Buy and Sell Stock

从今天开始正式切换到英文版leetcode,一开始其实我是在英文版刷题的,但最后选择中文版leetcode网站,
一是因为国内版,网站浏览和代码提交更流畅,不用提交后等半天才能出结果;
二是主要原因,我发现国内版里面有针对国内互联网企业的专题训练,然而英文版的专题训练是针对国外的互联网公司,我希望刷刷国内大公司的题目,提高自己的算法能力。

现在仍是在中文版网站刷题,但会把题目切换成全英文。为什么做这个决定?明天就是四六级考试,虽然我大一下就把四级考过了,但六级考了三次也没有过,主要还是自己上大学后太懒,平常不注重积累,每次都是考试前一个星期临时抱佛脚,所以,,,虽然题目单词大部分很简单,但日积月累,也能多认识几个专业术语。

注:国内版由于刚创建不久,所以用户量不多,所以题目社区分享几乎没有,如果你想要看看题目的优质解答,可以直接去英文版看看讨论区,反正我自己是这么做的,这样可以提高阅读编码的能力,同时也可以加深自己的题目的理解,而不是遇到没有思路的,就直接上百度找博客,看别人图文并茂缕的一清二楚的分析思路,然后不假思索的把代码直接拷贝提交,这样只是自欺欺人,希望大家能共同成长,最后拿到梦想的offer。

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 (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.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

2. 题目分析
要获得最大利润,即,从当天购入的价值,往后找,与购入的价值相差最大的股票值。

  • 暴力破解的方法:对每一天的股票值,然后跟后面的天数比较,从中找到最大利润。
  • 动态更新最小值,然后,找与最小值相差最大的价值。
    如: [7,1,5,3,6,4]
    初始化:i=0,min = 7 , max = 0
    第一次:i=1,min>1 , min = 1 , max = 0
    第二次:i=2,min < 5 ,min =1 , max = max(0,5-1)=4
    第三次:i=3,min < 3 ,min =1 , max = max(4,3-1)=4
    第四次:i=4,min < 6 ,min =1 , max = max(4,6-1)=5
    第五次:i=5,min < 4 ,min =1 , max = max(5,4-1)=5
    所以最大利润是:5

3. 解决思路

使用一次循环,动态更新min,接着遍历找到比min更大的值,然后相减后与前面获得的最大利润相比,更大,就更新最大利润值。

4. 代码实现(java)

  • 暴力破解(两次循环)
public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2){
            return 0;
        }
        int maxProfit = 0;
        int len = prices.length;
        int temp;
        for (int i = 0; i < len; i++) {
            for (int j = i+1; j < len; j++) {
                temp = prices[j] - prices[i];
                if (temp > maxProfit){
                    maxProfit = temp;
                }
            }
        }
        return maxProfit;
    }
  • 优化(一次循环)
/**
     * 优化,一次循环
     * @param prices
     * @return
     */
    public int maxProfit_optimization(int[] prices) {
        if (prices == null || prices.length < 2){
            return 0;
        }
        int len = prices.length;
        int min = prices[0];
        int maxProfit = 0;
        for (int i = 1; i < len; i++) {
            if (prices[i] < min){
                min = prices[i];
            }
            if (prices[i] > min){
                maxProfit = Math.max(maxProfit, prices[i]-min);
            }
        }
        return maxProfit;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值