121.Best Time to Buy and Sell Stock---dp
题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
题目大意:给出一串数组,找到差值最大的差值是多少,要求只能用下标大的减下标小的,例子如下图:

法一(超时):直接两个for循环,进行一一比较,找出差值最大的点,但是超时了,所以这里后台应该规定的是1m的时间,而在1m的时间限制下,复杂度只有10^7左右,或者说不到10^8,这个题的有一个测试用例就超过了一万的数组大小,所以超时,代码如下:
int max = 0;
int length = prices.length;
for(int i = length - 1; i > 0; i--) {
for(int j = i - 1; j >= 0; j--) {
if(max < (prices[i] - prices[j])) {
max = prices[i] - prices[j];
}
}
}
return max;
法二(借鉴):贪心,一次遍历,从数组最左端开始每次都找相对较小的买入价,然后更新买入价,再找相对较大的卖出价,然后更新利润,代码如下:
int min = Integer.MAX_VALUE;
int res = 0;
//第i天的价格可以看作买入价,也可以看作卖出价
for(int i = 0; i < prices.length; i++) {
//找到更低的买入价
if(prices[i] < min) {
//更新买入价
min = prices[i];
}
else if(prices[i] - min > res){
//更新利润
res = prices[i] - min;
}
}
return res;
121.Best Time to Buy and Sell Stock---dp的更多相关文章
- 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 ...
- 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 ...
- (Array)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 ----- java
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- lucence学习系列之一 基本概念
1. Lucence基本概念 Lucence是一个java编写的全文检索类库,使用它可以为一个应用或者站点增加检索功能. 它通过增加内容到一个全文索引来完成检索功能.然后允许你基于这个索引去查询,返回 ...
- BZOJ 1189 紧急疏散(二分+最大流)
求出所有人撤离的最短时间.由于每扇门只能通过一次,所以不能简单用bfs来搞. 显然答案是有单调性的,考虑二分,问题变成了判断时间x所有人能不能撤离. 考虑最大流.对于每扇门,每个时间通过的人数最多为1 ...
- 洛谷P1345 [USACO5.4]奶牛的电信(最小割)
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- ORM框架SQLAlchemy使用学习
参考源:http://blog.csdn.net/fgf00/article/details/52949973 一.ORM介绍 如果写程序用pymysql和程序交互,那是不是要写原生sql语句.如果进 ...
- MySQL5.7初始配置
MySQL5.7初始配置 Windows7 环境安装MySQL5.7配置命令 <<<<<<<<<<<<<<<& ...
- 【Luogu1912】【NOI2009】诗人小G(动态规划)
[Luogu1912][NOI2009]诗人小G(动态规划) 题面 洛谷 题解 原来\(NOI\)这么多神仙题... 考虑一个极其明显的\(dp\) 设\(f[i]\)表示前\(i\)个句子产生的最小 ...
- [ACM][2018南京预赛]Lpl and Energy-saving Lamps
一.题面 样例输入: 5 4 3 10 5 2 7 10 5 1 4 8 7 2 3 6 4 7 样例输出: 4 0 1 1 3 6 5 1 5 1 2 0 3 2 4 4 3 6 5 1 二.思路 ...
- 【loj2033】生成魔咒
Portal --> loj2033 Solution 这题..虽然说好像也是sam的裸题不过既然在智力康复那就强制后缀数组吧qwq (晚点再用sam写一次qwq) 首先如果是要求本质不同的 ...
- JavaScript的变量预解析特性
JavaScript是解释型语言是毋庸置疑的,但它是不是仅在运行时自上往下一句一句地解析的呢?事实上或某种现象证明并不是这样的,通过<JavaScript权威指南>及网上相关资料了解到,J ...
- 使用rabbitmq消息队列
一.前言 在python中本身就是存在队列queue.一个是线程队列queue,另一个是进程multiprocessing中的队列Queue. 线程queue:只用于线程之间的数据交互 进程Queue ...