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.

【思路】

我么知道,如果要获得利润,售出的价格必须要高出买入的价格,而且出售的日期要大于买入的日期。因此我们在最低时候买入,在最高的时候卖出。从第一天开始,吧第一天的价格设置为当前最低价格,如果后一天的价格高于最低价,我们就记录此时的价格差,如果此时的利润大于之前的最高利润则把它设置为当前最大利润,然后下标向后移动。如果有一天的价格低于当前最低价格,我们就把当前价格设置为最低价格,然后继续上述过程,直到遍历到数组末尾。这个过程其实是把数组分为一段一段,每一段的开始都是这一段范围的内的最低值。举个例子:[2,24,4,1,3,7]红色的一段中2是最小值,24是最大值。绿色的一段中1是最小值,7是最大值。

代码如下:

  1. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. int min = 0;
  4. int mp = 0;
  5.  
  6. for (int i = 1; i < prices.length; i++) {
  7. if (prices[min] > prices[i]) min = i;
  8. else mp = Math.max(mp, prices[i] - prices[min]);
  9. }
  10.  
  11. return mp;
  12. }
  13. }

LeetCode OJ 121. Best Time to Buy and Sell Stock的更多相关文章

  1. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  2. LeetCode OJ 123. 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 ...

  3. 【刷题-LeetCode】121 Best Time to Buy and Sell Stock

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

  4. 【LeetCode OJ】Best Time to Buy and Sell Stock II

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...

  5. 【LeetCode OJ】Best Time to Buy and Sell Stock

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...

  6. 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock

    # 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...

  7. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  8. LeetCode OJ 122. 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 ...

  9. LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际II)

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

随机推荐

  1. dwr推送技术深入研究

    DWR 工作原理: 是通过动态把 Java 类生成为 Javascript.它的代码就像 Ajax 一样,你感觉调用就像发生在浏览器端,但是实际上代码调用发生在服务器端,DWR 负责数据的传递和转换. ...

  2. zabbix 布署实践【6 使用微信公众号-消息模版推送告警】

    使用这个服务的前提是,你必须要有一个微信订阅号,或者公众号,并且是通过认证的号 因为认证过后的号才有模版消息和获取用户openid等信息的权限 ,如下,登录微信公众号的登录页后,底下有个接口权限的展示 ...

  3. C#基础、基础知识点(新人自我总结,开启java学习之路)

    从2016年12月29开班,开课到现在C#基础已经算是简答的学习了一点,一个为期两周的课程,或多或少对现在学的Java有着一定的帮助吧,我们先从软件入门来接触c#这门语言: 一.软件开发中的常用术语: ...

  4. Chapter 2 Open Book——36

    "That was awful," he groaned. "They all looked exactly the same. You're lucky you had ...

  5. git add -f

    git add -f 添加已被 .gitignore 忽略的文件/文件夹

  6. JavaScript忍者秘籍——驯服线程和定时器

    1.定时器和线程 - 设置和清除定时器 JavaScript提供了两种方式,用于创建定时器以及两个相应的清除方法.这些方法都是window对象上的方法. 方法 格式 描述 setTimeout   i ...

  7. 关于在mfc中cstring转为float和ini

    CString str1,str, str2; GetDlgItemText(IDC_EDIT1, str1); GetDlgItemText(IDC_EDIT2, str2); UINT value ...

  8. 前端自动化部署之gulp

    1.首先需要安装node+npm(这里不再叙述,网上教程一大堆) 2.gulp全局安装:npm install -g gulp 3.cd进入到你的项目目录,这里使用demo文件夹为我的示例项目 4.在 ...

  9. html5的116个标签

    基础   标签 描述 <!DOCTYPE> 定义文档类型. <html> 定义 HTML 文档. <title> 定义文档的标题. <body> 定义文 ...

  10. <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数

     蛇形填数: 在n×n方阵里填入1,2,....,n×n,要求填成蛇形.例如,n = 4 时方阵为:    10  11  12  1   9  16  13  2 8  15  14  3 7   ...