LeetCode——Best Time to Buy and Sell Stock
Description:
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 (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
只允许买卖一次,求最大利润。
动态规划,从后向前找最大股价,减去当前股价,求利润,找最大利润。O(n).
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if(len == 0) return 0;
int highestPrice = prices[len - 1];
int res = 0;
for(int i=len-1; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
res = max(res, highestPrice-prices[i]);
}
return res;
}
int max(int a, int b) {
return a > b ? a : b;
}
}
LeetCode——Best Time to Buy and Sell Stock的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [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 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
随机推荐
- buildroot 使用本地交叉编译器记录
继上一次glibc bug 事件后,剑锋就说文件系统要用统一的交叉编译器.所以今天就试了一下,便记录一下 在官网下载干净的 buildroot 进入解压后的 buildroot 目录 步骤: make ...
- UTF-8以字节为单位对Unicode进行编码
UTF-8以字节为单位对Unicode进行编码.从Unicode到UTF-8的编码方式如下: Unicode编码(16进制) UTF-8 字节流(二进制) 000000 - 00007F 0xxxxx ...
- style="display:none"隐藏html的标签
隐藏html的标签 <div class="span11 alignment"> <h1>我的虚拟网络</h1> </div> &l ...
- rufus/scheduler
之前先在initializers 文件夹下建立了一个task.rb ,rails服务启动的时候执行,本地没问题,可是服务器上启动了3个进程,会执行3遍,解决方法是 把task.rb 放到 script ...
- 原来java中也有类似goto语句的标签啊--java label标签
http://blog.sina.com.cn/s/blog_6d5354cd0100xjg7.html ——————————————————————————————————————————————— ...
- 上手并过渡到PHP7(3)——Uniform Variable Syntax到底统一了什么
PHP7 up and running 泊学原文链接泊学实操视频 Uniform Variable Syntax 在PHP 7提出Uniform Variable Syntax之前,我们大多数人可能都 ...
- spring配置事务 元素 "tx:annotation-driven" 的前缀 "tx" 未绑定
在进行spring与mybatis整合时,启动项目报错,控制台提示“元素 "tx:annotation-driven" 的前缀 "tx" 未绑定”. 经过查找, ...
- CSS3加载动画
图1 通常我们都使用gif格式的图片或者使用Ajax来实现诸如这类的动态加载条,但是现在CSS3也可以完成,并且灵活性更大. 选1个例子看看怎么实现的吧: 效果图: 图2 代码: 使用1个名为'l ...
- [svn] 在线安装
SVN地址:http://subclipse.tigris.org/update_1.6.x/
- JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对比
1.什么是JVM? JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才能够运行Java程序,JAVA在编译后会生成类似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是, ...