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. AndroidAPI

    1词典 百度翻译 http://api.fanyi.baidu.com/api/trans/product/index 百度翻译支持多种语言互相翻译,包含PHP, JS, Python, C, Jav ...

  2. 性能测试工具LoadRunner18-LR之Controller 集合点

    含义 当通过controller虚拟多个用户执行该脚本时.用户的启动或运行步骤不一定是同步的.集合点是在脚本的某处设置一个标记.当有虚拟用户运行到这个标记时,停下等待,直到所有用户都达到这个标记时,再 ...

  3. [转]25个HTML5和JavaScript游戏引擎库

    本文转自:http://www.open-open.com/news/view/27c6ed 1. The GMP JavaScript Game Engine GMP是一个基于精灵2-D游戏,它可以 ...

  4. mac os安装macvim

    1 brew install macvim 安装 macvim 2 alias vim='/usr/local/Cellar/macvim/7.4-73_1/MacVim.app/Contents/M ...

  5. jQuery源代码学习笔记_bind

    一般想到JS的兼容性问题的时候,首先会想到addEventListener与attachEvent这一对冤家,那么我们先来看看它们有什么兼容性问题 addEventListener与attachEve ...

  6. HDU 4364——Matrix operation——————【模拟题】

    Matrix operation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. AJAX重点知识的心得体会

    下面就为大家带来一篇 AJAX重点知识的心得体会.学习还是有点帮助的,给大家做个参考吧. AJAX是什么? 是Asynchronous Javascript And XML的首字母的缩写, 它不是一门 ...

  8. 认识web

    URL详解: URL是Uniform Resource Locator 的简写,统一资源定位符. 一个URL由以下几部分组成: scheme:代表的是访问的协议,一般为http或者https以及ftp ...

  9. xml解析demo使用

    package lianxi; import java.io.FileOutputStream;import java.io.OutputStreamWriter; import javax.xml. ...

  10. sass-基础

    导入: sass的导入(@import)规则和CSS的有所不同,编译时会将@import的scss文件合并进来只生成一个CSS文件. 但是如果你在sass文件中导入css文件如@import 'res ...