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.

[Thought]

Using greedy algorithm to solve this problem . Scan from left to right , and keep track the minimum price . If the difference between the minimum value and price is bigger than the profit , replace it.

  1. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. if(prices.length == 0)
  4. return 0;
  5.  
  6. int profit = 0;
  7. int min = prices[0];
  8. for(int i=0;i<prices.length;i++){
  9. if(prices[i]<min){
  10. min = prices[i];//keep track the minimum value
  11. }else{
  12. if(prices[i] - min > profit){//if the difference between price[i] and minimum price is bigger than profit , replace it.
  13. profit = prices[i]-min;
  14. }
  15. }
  16. }
  17. return profit;
  18. }
  19. }

[LeetCode] Best Time to Buy and Sell Stock Solution的更多相关文章

  1. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  2. [LeetCode] 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 al ...

  3. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] 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 al ...

  6. [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 ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  9. [LeetCode] 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 the price of a given stock ...

随机推荐

  1. 再谈PHP、Python与Ruby

    一句话总结 简单地总结: 假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成为一个高效工程师,推荐 Python. 假如你想让他爱上他的工作,推荐 Ruby. 语言的选择 编程语言非常重要 ...

  2. Flink资料(2)-- 数据流容错机制

    数据流容错机制 该文档翻译自Data Streaming Fault Tolerance,文档描述flink在流式数据流图上的容错机制. ------------------------------- ...

  3. SilverlightLoader使用托管代码创建自定义载入界面及动态加载XAP

    Silverlight实现动态加载xap和Splash Screen.收藏! 内容来自 http://silverlightchina.net/html/tips/2010/0115/588.html

  4. MongoDB存储时间

    之前一篇博客C++的时间中提到了MongoDB保存时间类型数据可以使用timestamp类型. 不过在实际编程过程中,发现保存timestamp容易,读取难.MongoDB C++的这方面的例子还没有 ...

  5. 有哪些适合学生参与的 C++,网络编程方面的开源项目?

    有哪些适合学生参与的 C++,网络编程方面的开源项目?   Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通 ...

  6. Java Scoket之java.io.EOFException解决方案

    Java Scoket之java.io.EOFException解决方案   Socket接收数据的时候,常常会抛出java.io.EOFException异常,也没有明确的原因和提示,在网上搜搜,很 ...

  7. Windows Azure 社区新闻综述(#70 版)

    欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 ·   如何选择 No ...

  8. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(一)

    目的: 结合现在比较流行的技术,通过一个demo 展示一个全栈式设计的各种技能. 一个全栈式的工程师,应该能设计通过verilog/VHDL做logical设计.能写内核驱动,能架站. 要熟悉veri ...

  9. DRP总结

    DRP终于结束了,战线有点长了.记得刚开始听说DRP的时候,感觉这个名词很专业,再加上视频一共有300集,顿时感觉这是一个大项目,很正规.很专业的项目.虽然后来知道DRP知识ERP的一个分支,项目规模 ...

  10. 轻轻谈一下seaJs——模块化开发的利器

    "仅做一件事,做好一件事." 这个应该就是seaJs的精髓了. 我在自己的一些项目中使用过seaJs.对其算是了解一二.如今就班门弄斧.轻轻地谈一下. 首先上一段度娘的话: &qu ...