[leetcode121]股票买卖 Best Time to Buy and Sell Kadane算法
【题目】
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.
【思路】
Kadane's Algorithm差值
【代码】
class Solution {
public int maxProfit(int[] prices) {
int cur=0;
int sum=0;
for(int i=1;i<prices.length;i++){
cur+=prices[i]-prices[i-1];
sum=Math.max(sum,cur);
cur=cur>0?cur:0;
}
return sum;
}
}
[leetcode121]股票买卖 Best Time to Buy and Sell Kadane算法的更多相关文章
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 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 ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LeetCode] 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 ...
- [LintCode] 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 ...
- [LintCode] 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 ...
随机推荐
- SSD固态硬盘是会掉速的。
也没什么好的办法. 只是自己不再疑神疑鬼,总觉得中病毒了. 下面的文章还是挺有参考意义的. http://diy.pconline.com.cn/627/6271636_all.html SSD变慢了 ...
- Spring Boot入门第三天:配置日志系统和Druid数据库连接池。
原文链接 一.日志管理 1.在application.properties文件中加入如下内容: logging.level.root=WARN logging.level.org.springfram ...
- cmd下可以启动java,输入javac提示 不“存在”
方法:手动把JDK安装目录的bin目录配置到PATH环境变量里
- CentOS6.8下实现配置配额
CentOS6.8下实现配置配额 Linux系统是支持多用户的,即允许多个用户同时使用linux系统,普通用户在/home/目录下均有自己的家目录,在默认状态下,各个用户可以在自己的家目录下任意创建文 ...
- Codeforces 1151F Sonya and Informatics (概率dp)
大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...
- http认证方式,工程部分实现
学习过程中,被boss批评,要求去复习http协议,因此找了相关资料做成一个系列:对于http认证方式不清楚的可以参考我的上一篇文章 http认证方式https://www.cnblogs.com/j ...
- spring中集成shiro
Shiro的组件都是JavaBean/POJO式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web应用的集成. 在示 ...
- (待解决,效率低下)47. Permutations II C++回溯法
思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的v ...
- Visual Studio 类模板的修改
总共有三个步骤: No1:找到类文件模板路径 我的安装盘在D盘 visual studio 2010: D:\Program Files (x86)\Microsoft Visual Studio 1 ...
- 时间序列八: 以NASA之名: 卡尔曼滤波器
目录 以NASA之名: 卡尔曼滤波器 引言 荣耀骑士 卡尔曼滤波器* 参考文献: 以NASA之名: 卡尔曼滤波器 'That's one small step for man,one giant le ...