121. Best Time to Buy and Sell Stock——Leetcode
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的更多相关文章
- 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 ...
- 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 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 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 ...
- Best Time to Buy and Sell Stock - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...
- 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 ...
- [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 ...
- 【刷题-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 ...
- 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 ...
随机推荐
- 加解密---Base64
1.算法实现: 1.1 JDK提供: package com.exiuge.mytest; import sun.misc.BASE64Decoder; import sun.misc.BASE64E ...
- oracle_expdp_help
[oracle@ctp ~]$ expdp -help Export: Release 11.2.0.3.0 - Production on Thu Feb 28 13:52:15 2019 Copy ...
- 在使用clone()时id保持一致
大家都知道,同一个HTML页面中,不宜出现1个以上相同名称的id.但有时候需要使用jQuery框架的clone()来复制相同内容(附带样式),假如是使用了id号的获取方式,即$(‘#***’) 那么复 ...
- [转]jquery的ajax交付时“加载中”提示的处理方法
本文转自:http://www.educity.cn/wenda/77121.html jquery的ajax提交时“加载中”提示的处理方法 方法1:使用ajaxStart方法定义一个全局的“加 ...
- bootstrap3-dialog:更强大、更灵活的模态框(封装好的模态框)
用过bootstrap框架的同学们都知道,bootstrap自带的模态框用起来很不灵活,可谓鸡肋的很.但nakupanda开源作者封装了一个更强大.更灵活的模态框——bootstrap3-dialog ...
- 服务间调用--feign跟ribbon
微服务一般来说当然是多服务多实例的,那么这些服务之间如何相互调用呢?spring cloud之前我们用dubbo,把服务开放出来,在xml配好后就可以像调用本地service一样调用其它模块的服务了: ...
- Docker学习笔记(2)-docker镜像操作
本节将会涉及Docker的镜像操作. 1. 获取镜像 如何获取Docker Hub上的镜像?可通过docker pull命令获取,其格式为: docker pull [选项] [Docker Regi ...
- MVC 模型过滤
通用返回模型 /// <summary> /// WebApi返回数据的泛型类 /// </summary> /// <typeparam name="T&qu ...
- Model对象嵌套list赋值方式(备忘)
首先定义Model对象:var deliveryInfoModel = new DeliveryInfo(); 第二步定义嵌套的list对象:var list = new List<Delive ...
- ios 你必须了解的系统定义宏使用
1. UNAVAILABLE_ATTRIBUTE __attribute__((unavailable)) - (instancetype)init UNAVAILABLE_ATTRIBUTE; 告诉 ...