LeetCode: 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 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.
Solution 1:
min记录最小买入价
maxProfit记录最大利润
遍历array,不断更新最小买入价,计算更新最大利润
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null) {
return 0;
}
int maxProfit = 0;
int minValue = Integer.MAX_VALUE;
for (int i: prices) {
minValue = Math.min(minValue, i);
maxProfit = Math.max(maxProfit, i - minValue);
}
return maxProfit;
}
}
LeetCode: Best Time to Buy and Sell Stock 解题报告的更多相关文章
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- 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 ...
随机推荐
- 【RS】Matrix Factorization Techniques for Recommender Systems - 推荐系统的矩阵分解技术
[论文标题]Matrix Factorization Techniques for Recommender Systems(2009,Published by the IEEE Computer So ...
- codeblocks编译pthread问题
默认的编译选项是没有pthread的,所以要自己添加: 参考:http://hi.baidu.com/u_soa/item/9d6cc40b7e9d76eb3499024d 错误: undefined ...
- iphone 恢复出厂设置方法
1.下载安装并打开itunes. 2.让手机进入恢复模式: 一.先长按住电源键,出现关机选项时,请滑动关机: 二.随后再按电源键开机,屏幕会出现苹果标志,不要松开电源键: 三.接着再按住主屏 Home ...
- 【Servlet】web.xml中welcome-file-list的作用
今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...
- 免费的UI素材准备
UI素材准备 UI也是一个专业性比较强的一个活啊,不过还好我有强大的百度,强大的百度有各种强大的网站,下面介绍一些UI常用的网站1.阿里巴巴矢量图标库 http://www.iconfont.cn/p ...
- table中background背景图片自动拉伸
<table background="login/image/jiaozhouwan.jpg" style="background-size: 100% 100% ...
- ORA-65179: cannot keep datafiles for a pluggable database that is not unplugged
SQL> drop pluggable database pdb2; drop pluggable database pdb2 * ERROR at line : ORA-: cannot ke ...
- android studio 如何让包名展开
通常我们新建一个包名的时候,会发现他们连在一起,根本无法在创建一个同级的包 工具/原料 电脑,android studio 方法/步骤 1,我们先在包名下建一个包,变成了这样,根本无法在同 ...
- Android适配底部返回键等虚拟键盘的完美解决方案
这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...
- Android数据库安全解决方案,使用SQLCipher
源码:http://files.cnblogs.com/android100/SQLCipherTest.rar 我们都知道,Android系统内置了SQLite数据库,并且提供了一整套的API用于对 ...