前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 121. Best Time to Buy and Sell Stock题目Solution

LeetCode 121. Best Time to Buy and Sell Stock题目Solution

作者头像
desperate633
发布2018-08-22 15:35:35
2000
发布2018-08-22 15:35:35
举报
文章被收录于专栏:desperate633desperate633

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. Example 1: Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 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: 0In this case, no transaction is done, i.e. max profit = 0.

题目

假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。 样例 给出一个数组样例 [3,2,3,1,2], 返回 1

Solution

Approach #1 (Brute Force) 暴力搜索

思路很简单,我们就要找出数组中,差值最大的两个数,要求是前一个数小,后一个数大,那么遍历两边即可,找出所有这样的差值,找出其中最大的一个

代码语言:javascript
复制
public 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++) {
                int profit = prices[j] - prices[i];
                if (profit > maxprofit)
                    maxprofit = profit;
            }
        }
        return maxprofit;
    }

Approach #2 (One Pass) Greedy 贪婪法

通过一遍遍历,找出到此为止之前最小的min,并与当前的值求差,保存最大的差值,遍历完,最后的差值即是最大差值。

Paste_Image.png

代码语言:javascript
复制
public int maxProfit(int prices[]) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice)
                minprice = prices[i];
            else if (prices[i] - minprice > maxprofit)
                maxprofit = prices[i] - minprice;
        }
        return maxprofit;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.04.12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • Solution
    • Approach #1 (Brute Force) 暴力搜索
      • Approach #2 (One Pass) Greedy 贪婪法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档