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 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.
思路:注意不能简单地将最大值-最小值。卖必须发生在买之后,最小值得在最大值之前。记录下到当前为止最小值。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int maxProfit = ;
int min = INT_MAX;
int profit; for(vector<int>::iterator it = prices.begin(); it < prices.end(); it ++)
{
if ((*it)<min)
{
min = *it;
}
else
{
profit = *it - min;
if(profit > maxProfit)
{
maxProfit = profit;
}
}
}
return maxProfit;
}
};
121. Best Time to Buy and Sell Stock (Array;DP)的更多相关文章
- 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 ...
- 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【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 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
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- (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 ...
随机推荐
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...
- class<T>和 class<?>类型 有什么区别
平时看java源代码的时候,如果碰到泛型的话,我想? T K V E这些是经常出现的,但是有时想不起来代表什么意思,今天整理下: ? 表示不确定的java类型. T 表示java类型. K V 分别代 ...
- call和apply,bind的区别专讲
可以干什么? 改变函数内的this指向: 什么时候使用? 构造函数使用this 为什么使用? 为了生成对象 类(函数名不可以带括号).call() 因为this指向对象,所以call的第一个 ...
- width:100%和width:auto区别
在div父元素是body时 1.先看没有width限制的div <div style="border:1px solid red; margin-left:50px; margin-r ...
- php 数组随机取值
array_rand()在你想从数组中取出一个或多个随机的单元时相当有用.它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1. a ...
- Linux入门之vi
How to Use the vi Editor* *Copyright 1991 by Simon Fraser University. Reprinted with permission. The ...
- (6/24) 插件配置:轻松配置JS文件压缩
实际开发中,在项目上线之前,我们编写的js代码是需要进行压缩的,我们可以采取压缩软件或者在线进行压缩,这不是我们的重点,在webpack中实现JS代码的压缩才是本节的核心. 通过webpack中可实现 ...
- CentOs - 使用ssh key远程登录
环境: 服务器端CentOs,本地OS X 服务器端: 1. 安装openssl使实现ssl协议 2. 将本地的pub key加入信任列表 本地: 1. 生成pub key 2. 配置ssh别名使登陆 ...
- [ilink32 Error] Fatal: Unable to open file 'RM_COMMON.OBJ'
[ilink32 Error] Fatal: Unable to open file 'RM_COMMON.OBJ' [ilink32 Error] Fatal: Unable to open fil ...
- SQL 2012 分页取数据
,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...