leetcode 121
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0. 求出数列中后面的数与前面的数最大的差值;刚开始就想到了暴力解法,结果超时;随后进一步思考,在一次遍历中完成整个计算。
代码入下:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int m = prices.size();
if(prices.empty())
{
return ;
}
int curMin = prices[];
int profit = ;
for(int i = ; i < m; i ++)
{
curMin = min(curMin, prices[i]);
profit = max(profit, prices[i]-curMin);
}
return profit;
}
};
leetcode 121的更多相关文章
- [LeetCode]121、122、309 买股票的最佳时机系列问题(DP)
121.买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意 ...
- 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 (买卖股票的最好时机)
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_Easy tag: Dynamic Programming
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 股票买卖问题系列
描述: 给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大). 其他三道问题是,如果能买卖无限次,买卖两次,买卖k次. 题一: 实质是求后面一个数减前一个数的 ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- Java for 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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- CSS如何实现数字分页效果
代码实例如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- UIAlertView用法
1. 最简单的用法 UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"这是一个简 ...
- JSON.stringify()的使用--将string转换成json
===========================================================1. ====JSON.stringify()================== ...
- oracle修改列的类型
alter table table_name modify column_name datatype;
- Memcached常用命令及使用说明(转)
一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...
- eclipse设置svn代理
共2个步骤: 1. 找到C:\Documents and Settings\用户名\Application Data\Subversion的servers文件, 将#http-proxy-host和# ...
- 【转】特殊权限控制之SUID、SGID、Sticky
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://soysauce93.blog.51cto.com/7589461/1715583 ...
- (转).net Application.DoEvents()的作用
原文地址:http://blog.csdn.net/weinierbian/article/details/6231589 Application.DoEvents()的作用:处理所有的当前在消息队列 ...
- js的传值,table中tr的遍历,js中动态创建数组
1.这里关键是对页面中的传值,其次是动态的创建一个数组,用来存值 $(val).css("background-color", "rgb(251, 248, 233)&q ...
- OC基础(13)
内存管理简介 引用计数器 dealloc方法 野指针\空指针 *:first-child { margin-top: 0 !important; } body > *:last-child { ...