121. Best Time to Buy and Sell Stock

5人阅读 评论(0) 收藏 举报
分类:

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: 5

max. 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: 0

In this case, no transaction is done, i.e. max profit = 0.


class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()<2) return 0;
        int maxProfit = 0;
        int minPrice = prices[0];
        for(int i=1; i<prices.size(); i++)
        {
            if(prices[i]>prices[i-1])
                maxProfit = std::max(maxProfit, prices[i]-minPrice);
            else
                minPrice = std::min(minPrice, prices[i]);
                
        } 
        return maxProfit;
    }
};

查看评论

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最...
  • u014251967
  • u014251967
  • 2016-09-12 21:04:35
  • 1148

LeetCode(121)Best Time to Buy and Sell Stock

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If you...
  • feliciafay
  • feliciafay
  • 2014-01-21 02:39:12
  • 2695

121. Best Time to Buy and Sell Stock [easy] (Python)

题目链接https://leetcode.com/problems/best-time-to-buy-and-sell-stock/题目原文 Say you have an array for w...
  • coder_orz
  • coder_orz
  • 2016-05-28 01:02:17
  • 1127

LeetCode(121) Best Time to Buy and Sell Stock (Java)

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If yo...
  • feliciafay
  • feliciafay
  • 2015-04-19 13:15:43
  • 1551

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If yo...
  • zsy112371
  • zsy112371
  • 2016-09-09 13:55:36
  • 1333

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an...
  • u011613729
  • u011613729
  • 2014-07-24 10:11:44
  • 3572

leetcode-java-121. Best Time to Buy and Sell Stock

思路: 要获得最大利益,其实就是找到相差最大的两个数。 所以用动态规划,找出当前数组中最小的值和当前最大利益public class Solution { public int maxPr...
  • github_34514750
  • github_34514750
  • 2016-06-05 10:32:24
  • 337

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015-04-06 11:10:34
  • 22523

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have a...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-14 06:16:32
  • 7948

leetcode_c++:Best Time to Buy and Sell Stock II(122)

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-06-10 23:19:48
  • 350
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 1万+
    积分: 1368
    排名: 3万+
    博客专栏
    最新评论