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 ...
随机推荐
- 用socket操作redis
代码: $cmd = "*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nbar\r\n"; // set foo bar $socket = socke ...
- studio-引入外来包
参考: http://stackoverflow.com/questions/16588064/how-do-i-add-a-library-project-to-the-android-studio ...
- [转载]socket下server端支持多客户端并发访问简单实现
/*Author: wainiwann *Source: 博客园 http://www.cnblogs.com/wainiwann *Remarks: 转载请说明出处!!! */ 感觉很不错,可以学 ...
- Mac 上SVN上传.a文件
SVN默认是忽略.a文件,所以修改配置文件去掉忽略配置行的 *.a 通过终端打开配置文件: open ~/.subversion/config 把下面两行(也可能是一行)中的注释和*.a去掉, #gl ...
- Python进阶07 函数对象
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...
- $(document).ready() 与 window.onload的区别
1.执行时间 window.onload 必须等到页面内所有元素(包括图片 css js等)加载完毕后才会执行. $(document).ready() 是DOM结构绘制完毕后就执行,不必等到所有元素 ...
- 详解Java GC的工作原理
JVM学习笔记之JVM内存管理和JVM垃圾回收的概念,JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,另外JVM分别对新生代和旧生代采用不同的垃圾回收机制. 首先来看一下JVM内存结构,它是由堆 ...
- [ActionScript 3.0] AS3 绘制12面体
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; im ...
- oracle插入主键数据、sequence和触发器
一.创建表: id number;并设为主键 name VARCHAR2(20 BYTE) 二. 插入数据 2.1 insert into addservice.test_table (id,na ...
- python 调用内部类的两种方法
class Car:#外部类 class Door:#内部类 def open(self): print('open door') class Wheel: def run(self): print( ...