关闭

【Leetcode】Best Time to Buy and Sell Stock

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

题目链接:https://leetcode.com/problems/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 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.

思路:

当前元素的最大收益只跟前面最小元素有关。

算法:

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


1
0
查看评论

[leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)

Question: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find t...
  • Xiaohei00000
  • Xiaohei00000
  • 2016-03-10 23:02
  • 1753

[LeetCode] Best Time to Buy and Sell Stock IV

Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design an algorithm to find the maximum profit. You may ...
  • u011331383
  • u011331383
  • 2015-02-23 00:08
  • 8761

【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 algorithm to find the maximum prof...
  • u011613729
  • u011613729
  • 2014-07-24 10:11
  • 3437

关于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 you were only permitted to complete at most one tran...
  • zsy112371
  • zsy112371
  • 2016-09-09 13:55
  • 832

【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
  • 21739

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

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最优值,而动态规划是要记录下来达到当前最优解的所有途径,由局部一步步得到全局最优。这几道题都是给你一个...
  • u014251967
  • u014251967
  • 2016-09-12 21:04
  • 796

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown

题目: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may co...
  • u014673347
  • u014673347
  • 2016-02-22 11:40
  • 1172

Best Time to Buy and Sell Stock III -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/  这道题是Best Time to Buy and Sell Stock的扩展,现在我们最多可以进行两次交易。我们仍然使用动态规...
  • linhuanmars
  • linhuanmars
  • 2014-04-09 05:34
  • 27254

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array of integers prices, for which the i-th element is ...
  • Zarlove
  • Zarlove
  • 2017-10-23 21:16
  • 1326

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 which the ithi^{th} element is the price of a given...
  • coder_orz
  • coder_orz
  • 2016-05-28 01:02
  • 982
    个人资料
    • 访问:568281次
    • 积分:8669
    • 等级:
    • 排名:第2676名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论