[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 ...
随机推荐
- google浏览器如何导出书签
首先打开浏览器点右侧的自定义及控制Google chrome. 点击书签-书签管理器 打开书签管理器界面中· 点击书签管理器的整理 最下面的将书签导出到html文件.. 弹出另存为对话 ...
- python 读写TXT,安装pandas模块。
今天需要用python读TXT 文件,发现pandas库好用,所以就去下载,没想pythoncharm中的setting中下载失败,所以去下源文件,安装pandas 是提示得先装numpy库,于是又去 ...
- [ /* 和 / 的区别 ] Difference between / and /* in servlet mapping url pattern
<url-pattern>/*</url-pattern> The /* on a servlet overrides all other servlets, includin ...
- 停止学习框架(Stop Learning Frameworks)
https://www.cnblogs.com/strick/p/10161733.html
- LeetCode--414--第三大的数
问题描述: 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1 ...
- Jupyter notebook 转 pdf [完整转换]
- layui 表格图片放大
1. 表格塞图片 ,{title: '图片', width:120, templet: function(d) { return '<div onclick="show_img(thi ...
- 『TensorFlow』分布式训练_其三_多机分布式
本节中的代码大量使用『TensorFlow』分布式训练_其一_逻辑梳理中介绍的概念,是成熟的多机分布式训练样例 一.基本概念 Cluster.Job.task概念:三者可以简单的看成是层次关系,tas ...
- 5月17 AJAX返回类型-------JSON和XML
ajax返回类型有TEXT,JSON,XML 一.TEXT 查看之前的练习 二.JSON var js = { aa:{code:"p001",name:"张三" ...
- 创建springboot的聚合工程(二)
前篇已经成功创建了springboot的聚合工程并成功访问,下面就要开始子工程木块之间的调用: springboot项目的特点,一个工程下面的类必须要放在启动类下面的子目录下面,否则,启动的时候会报错 ...