(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 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.
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int res=prices[1]-prices[0]; //开始是想用定义一个新的数组的,写到一半发现只要2个变量就好了
int curmin=(res<0?prices[1]:prices[0]);
for(int i=2;i<prices.length;i++){
res=Math.max(res,prices[i]-curmin);
curmin=Math.min(prices[i],curmin);
}
return (res>0)?res:0; //注意返回值肯定不是负数
}
}
(Array)121. Best Time to Buy and Sell Stock的更多相关文章
- 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 ...
- 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 ...
- 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@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 (Array;DP)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 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 ...
随机推荐
- 关于超出部分隐藏加省略号的css方法
单行效果:display:block; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 多行效果:width: ...
- css高级
1.复杂选择器 1.作用 匹配 页面的元素 ... ... 2.选择器分类 1.兄弟选择器 1.作用 通过 元素的位置关系匹配元素 位置关系:兄弟关系(平级元素) <div> <p ...
- Java中的抽象类
含有抽象方法的类,抽象方法即用abstract修饰的方法,即父类只知道其子类应该含有该方法,但无法知道子类如何实现这些方法 抽象类限制规定子类必须实现某些方法,但不关注实现细节 抽象类中可以包含普通方 ...
- NOIP2016の遊記
看了cydiater的游记,我更加认识到我有多弱,大神有多强 剩余的时间不多了,NOIP前停的一周课又颓了相当多的时间,感觉NOIP真的药丸 最后一天复习模板,看一下错误,总结做题的经验,现在实力实在 ...
- [llvm] Call the LLVM Jit from c program
stackoverflow: http://stackoverflow.com/questions/1838304/call-the-llvm-jit-from-c-program Another t ...
- iOS_Quartz2D之涂鸦板
响应者对象:继承了UIResponder的对象 触摸事件:一根或多根手指: 开始触摸: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent ...
- SolrCloud分布式集群部署步骤
Solr及SolrCloud简介 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成 ...
- 印刷电路板(PCB)的材料
以玻璃为基础材料的板材可以在高达150℃到250℃的温度下使用.可选的介质材料有: FR4,介电常数ε0为4.6 环氧材料,介电常数ε0为3.9: 聚酰亚胺,介电常数ε0为4.5. 另外,以聚四氟乙烯 ...
- 随机函数的代码(srand、rand)
#include<stdio.h> int main() int counter; for(counter=0;counter<10;counter++) { srand(count ...
- mysql 序列与pg序列的比较
mysql序列(这里只谈innodb引擎): 在使用mysql的AUTO_INCREMENT时,使用AUTO_INCREMENT的字段必须建有索引,也可以为索引的一部分.当没有索引时会报错: ...