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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 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
Explanation: In this case, no transaction is done, i.e. max profit = 0.

题目大意:给定一组股票的价格,买卖一次,求最大的收益。

思路:扫一遍,记录最小值,当前价格减去最小值就是当前可获得的最大收益,从这些可能的收益值中取最大的。

public int solution(int[] arr) {
if(arr == null || arr.length == 0) {
return 0;
}
int[] dp = new int[arr.length];
int min = arr[0];
for(int i = 1;i<arr.length;i++) {
dp[i] = Math.max(dp[i-1], arr[i] - min);
if (arr[i] < min) {
min = arr[i];
}
}
return dp[arr.length - 1];
}

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

  1. 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

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

  2. 30. leetcode 121. Best Time to Buy and Sell Stock

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

  3. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  4. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  5. Best Time to Buy and Sell Stock - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...

  6. 121. Best Time to Buy and Sell Stock@python

    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] 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 were ...

  8. 【刷题-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 ...

  9. 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 were ...

随机推荐

  1. Python数据操作

    列表操作 保存matrix或者保存ndarray 数据类型转换 读取CSV某列 numpy数组写入到csv pandas to_csv 最左边 多一列 的问题 DataFrame对象操作

  2. 命令行模式运行jmeter,主从方式运行jmeter

    jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script.jmx -R 10.6.5.31,10.6.5 ...

  3. android Activity启动过程(一)从startActivty开始说起

    从启动startActivity开始说起 MainActivity.startActivity() Activity.startActivity() Activity.startActivityFor ...

  4. 关于老教授之家项目的思考 && 中国互联网+大赛培训

    最近在做中国互联网+竞赛相关的项目,有一点思考在这里记录下来,算是一份经历,日后可以再回顾,这也是我真正参加的一个大型比赛,作为技术人员可能更多的是从事技术,但是在其他方面能贡献自己的一份力量也是不错 ...

  5. fileupload NPOI导入EXECL数据

    fileupload JS @section scripts{ <script src="~/Content/js/fileupload/vendor/jquery.ui.widget ...

  6. C# 将外部exe程序 嵌入到自己的窗体界面

    将别人开发的exe程序,放到自己的窗体里面来运行. 1.基本功能实现 首先,在自己的窗体后面加上代码: [DllImport("User32.dll", EntryPoint = ...

  7. Java入门之Tomcat安装及环境变量配置

    一.Tomcat下载 地址:http://tomcat.apache.org/download-80.cgi#8.0.39 本人用的是Tomcat/8.0.37免安装版,解压到一个目录,本人用的是:D ...

  8. phpstorm 配置 webserver ,配置根目录

    原文链接    http://blog.csdn.net/pony_maggie/article/details/52367093 phpstorm自带了一个web server,我们可以直接在IDE ...

  9. Debug Diagnostics Tool创建.Net异常转储并用Windbg分析异常

    当我们要在IIS PRD环境下分析异常,并且对问题毫无头绪,又没有权限直接上打Log的代码.这个时候就是Debug Diagnostics Tool & Windbg大显神威的时候了. Deb ...

  10. springmvc+spring+mybatis+sqlserver----查询sqlserver----有返回参数

    <resultMap type="java.util.HashMap" id="resultMap"> <result column=&quo ...