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. 数据结构---Java---HashSet

    1.概述 1.1 HashSet不是线程安全的: 1.2 当向HashSet存入元素时,调用该对象的hashCode()值,根据hashCode()值来决定元素的存储位置: 如果hashCode()值 ...

  2. 解决 eclipse cdt 运行时控制台乱码解决

    1 点击黑色 倒三角 按钮 选择 run configurations 2 2.1 点击new 添加 LANG = en_US 2.2 选择 replace native environment wi ...

  3. 《nginx 三》实现nginx的动态负载均衡——实战

    Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...

  4. kindEditor完整认识 PHP上调用并上传图片说明

    最近又重新捣鼓了下kindeditor,之前写的一篇文章http://hi.baidu.com/yanghbmail/blog/item/c681be015755160b1d9583e7.html感觉 ...

  5. Windows 入门杂乱无章版

    windows入门 /users/username/appdata/roaming/microsoft/windows/start menu/startup目录中的应用程序是在该用户登录时启动的 /w ...

  6. npm 包下载很慢的解决办法

    原因: 国内访问外网都很慢,甚至不能访问!安装Node时自带的npm地址默认是:http://registry.npmjs.org  三种方法: 1.通过config命令 npm config set ...

  7. 三、gridView增删改查

    1. 新增或修改一条数据 1.1 添加 新增或修改按钮 <div style="width: 120px; float: right; padding-top: 3px"&g ...

  8. HihoCoder#1513 : 小Hi的烦恼(五维数点 bitset 分块)

    题意 题目链接 Sol 五位数点问题,写个cdq分治套cdq分治套cdq分治套cdq分析就完了 可以用bitset搞 对于每一科开\(n\)个bitset,其中\(b[i]\)表示的排名为\(1 - ...

  9. thinkphp中怎么判断是手机端访问还是pc端访问?

    function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) r ...

  10. <Android 基础(十六)> Toast

    介绍 A toast provides simple feedback about an operation in a small popup. It only fills the amount of ...