[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 ...
随机推荐
- C++ 输出文件编码控制
c++ 读写文件需要包含fstream头文件. 读文件声明形如: ifstream fin("路径"): 写文件声明形如:ofstream fout("路径" ...
- Memcached安装&启动
安装 *Linux系统安装memcached,首先要先安装libevent库,安装请指定 --with--libevent=PATH(若安装过程中出现configure: error : no acc ...
- 初探nginx负载均衡配置
只简单说一下upstream的配置,如何进行负载均衡后续还需要多了解 1.另准备一个配置文件命名为nginx_test.conf 为了不污染原有的nginx.conf,提前复制一份配置文件做试验,然后 ...
- every day a practice —— morning(2)
Two years at sea have fostered a close relationship between the two fellow sailors as they cross the ...
- 20170923xlVBA_UpdateClientDetailSQL_Dictionary
Sub UpdateClientDetailWGQ() Dim Wb As Workbook Dim Sht As Worksheet Dim Rng As Range Dim Arr As Vari ...
- Practical Node.js (2018版) 第4章: 模版引擎
Template Engines: Pug and Handlebars 一个模版引擎是一个库或框架.它用一些rules/languages来解释data和渲染views. web app中,view ...
- TP5中的小知识
在TP5中如果想用select 查询后,变成数组,用toArray()这个函数的话,必须在连接数据库中把 数据集返回类型变成 'resultset_type'=>'\think\Collecti ...
- windows系统文件和linux系统文件
windows系统文件和linux系统文件 1.单用户操作系统和多用户操作系统 单用户操作系统:指一台计算机在同一时间 只能由一个用户 使用,一个用户独自享用系统的全部硬件和软件资源 Windows ...
- mac 配置homebrew
1.终端下输入export PATH=/usr/local/bin:$PATH 2.echo $PATH 3.安装homebrew 地址:ruby -e "$(curl -fsSL htt ...
- 第一阶段——站立会议总结DAY08
补发:因为第八次也就是第八天,那天有一个更重要的东西,看懂一个电商的系统.所以,未有进展.