(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 ...
随机推荐
- shell单引号中输出参数值
因为在shell的单引号中,所有的特殊字符和变量都会变成文本,那么如果需要在字符串中输出变量值怎么办呢? 这里记录以下两种方法: 使用双引号 shell> X='parameter' shell ...
- CSS3的calc()使用
CSS3的calc()使用 calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度.比如说,你可以使用calc()给元素的border.margin.pad ...
- javascript学习笔记全记录
js的初步了解 1.就是用来修改样式的,修改的是行内样式.任何样式都能够修改. 2.css里面怎么写js就怎么写. 3.任何元素都能加事件:事件都要小写 js的三大 ...
- 什么是Ajax
AJAX不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的Web应用程序的技术. 使用Javascript向服务器提出请求并处理响应而不阻塞用户!核心对象XMLHTTPRequest.通过 ...
- 百度地图helloworld程序问题
按照百度开发者平台[http://developer.baidu.com/map/index.php?title=androidsdk/guide/retrieval]的开发指南,完整编写代码ok之后 ...
- 为windows应用程序提供托盘图标
1>包含头文件 #include "Shellapi.h" 2>相关结构体和函数: NOTIFYICONDATA WINSHELLAPI BOOL ...
- *** missing separator. Stop.
在make命令后出现这种错误提示,是提示第2行没有分隔符. 例如: 1 target:prerequisites 2 command -- 改为: 1 target:prerequisites 2 ...
- JQuery 阻止js事件冒泡 阻止浏览器默认操作
//阻止事件冒泡 event.stopPropagation(); //阻止浏览器默认操作 event.preventDefault(); 代码不一定能执行,写给自己看的. 事件冒泡: <a h ...
- MR跑百分27不动引发的问题
今天跑MR跑到百分27就卡住不懂,查看JOB history也没看到MR,日志也没看到异常.50030端口页面不知道为什么打不开.由于MR里面设计Hbase就去查了下hbase的表.发现hbase l ...
- 程序员写的东西出了bug,造成了损失谁来承担?
这是个持续多年的话题了,很多大公司,尤其是牛逼的独立分包公司(开发公司)都会有代码审核和严格QA程序,一般的公司就很难说咯,在法律上目前还没有完全支持处罚程序员bug经济损失的判例(国内如此),国外也 ...