149. Best Time to Buy and Sell Stock【medium】
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.
Given array [3,2,3,1,2]
, return 1
.
题意
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。
解法一:
class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
if (prices.empty()) {
return ;
}
int i = prices.size() - ;
int ans = ;
int maxp = prices[i];
for (--i; i >= ; --i){
ans = max(ans, maxp - prices[i]);
maxp = max(maxp, prices[i]);
}
return ans;
}
};
参考@NineChapter 的代码
解法二:
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
} int min = Integer.MAX_VALUE; //just remember the smallest price
int profit = 0;
for (int i : prices) {
min = i < min ? i : min;
profit = (i - min) > profit ? i - min : profit;
} return profit;
}
}
参考@NineChapter 的代码
149. Best Time to Buy and Sell Stock【medium】的更多相关文章
- 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] 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 ...
- [LintCode] 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 ...
- [LintCode] 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 (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- PHP Mysql-连接
PHP 连接 MySQL PHP 5 及以上版本建议使用以下方式连接 MySQL : MySQLi extension ("i" 意为 improved) PDO (PHP Dat ...
- "高可用方案工具包" high availability toolkit 1.2 公布了。version 1.2 新增了 负载均衡 load balance 的技术实现
"高可用方案工具包" high availability toolkit 1.2 公布了. version 1.2 新增了 负载均衡 load balance 的技术实现. 项目 ...
- 【面试笔试】Java常见面试笔试总结
Java 基础 1.有哪些数据类型 Java定义了8种简单类型:byte.short.int.long.char.float.double和boolean. 2.面向对象的语言特征 封装.继承.多态 ...
- Windows下如何配置tomcat环境变量
Apache Tomcat 是一款 Java Servlet 和 JavaServer Pages 技术的开源软件实现,可以作为测试 Servlet 的独立服务器,而且可以集成到 Apache Web ...
- HTTP 协议中的 Transfer-Encoding
HTTP 协议中的 Transfer-Encoding 文章目录 Persistent Connection Content-Length Transfer-Encoding: chunked 本文作 ...
- 关于 C# HttpClient的 请求
Efficiently Streaming Large HTTP Responses With HttpClient Downloading large files with HttpClient a ...
- opencv-3.0.0-beta和opencv2版本号的差别
我的机器:64位系统 第一步: opencv官网下载opencv3.0.0-beta版本号.解压到自己的目录,我的目录是E:\,解压后在E盘出现名为opencv的目录.该目录下有两个子目录 第二步:配 ...
- Ubuntu14.04 mount远程服务器上的目录
备忘用. 一,远程服务器设置: 1,在/etc/exports中添加如下配置: /home/xxx *(insecure,rw,sync,no_root_squash,anonuid=123,anon ...
- mysql数据库的数据类型及约束
本文转自:http://www.cnblogs.com/zbseoag/archive/2013/03/19/2970004.html 1.整型 MySQL数据类型 含义(有符号) tinyint( ...
- java测试Unicode编码以及数组的运用(初学篇)
/*第二章第四小题*/ /* * (1)编写一个应用程序,给出汉字“你” ,“我”,“他”在Unicode 表中的位置 * (2)编写一个java应用程序,输出全部的希腊字母 */ public cl ...