LeedCode --- Best Time to Buy and Sell Stock
题意: find the maximum positive difference between the price on the ith day and the jth day
附上代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
if (prices.size() == )
return ;
// "minimum" holds the minimum price before the ith day.
// "max_diff" holds the maximum difference between prices[i] and prices[j]
// where 0 <= i < j < prices.size()
int minimum = prices[], max_diff = ;
for (unsigned int i = ; i < prices.size(); i++) {
if (prices[i] - minimum > max_diff) {
max_diff = prices[i] - minimum;
}
if (prices[i] < minimum) {
minimum = prices[i];
}
}
return max_diff;
}
};
LeedCode --- Best Time to Buy and Sell Stock的更多相关文章
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- [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 ...
随机推荐
- [转]一分钟明白 VS manifest 原理
什么是vs 程序的manifest文件 manifest 是VS程序用来标明所依赖的side-by-side组建,如ATL, CRT等的清单. 为什么要有manifest文件 一台pc上,用一组建往往 ...
- LUOGU P2675 《瞿葩的数字游戏》T3-三角圣地
题面 解题思路 手推可以得出,最后每个数字的贡献其实就是第n行杨辉三角数,然后直接卢卡斯直接算(今天才找到lucas定理时间复杂度是log n,log以模数为底).代码略麻烦,不想改了. 代码 #in ...
- Cesium 1.51新功能评测
前言 之前介绍Cesium1.50版本的新功能时,很多人把1.50写成1.5.这两个版本可不一样,之间差了45个小版本号,1.5版本大概是Cesium三年前的版本了. Cesium每月月初的第一个工作 ...
- 数据交换格式之 - XML
XML简介 XML是一种可扩展的标记语言,被设计用来传输和存储数据.传输数据. 需要自定义标签,自我描述性,XML是W3C的推荐标准: XML的特点与作用 特点: xml与操作系统.编程语言的开发平台 ...
- Commons BeanUtils工具包
简介: BeanUtils工具包是由Apache公司所开发,提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理. 我们知道,一个JavaBean通常包含了大 ...
- Django模型中的OneToOneField和ForeignKey有什么区别?
说是ForeignKey是one-to-many的,并举了一个车的例子: 有两个配件表,一个是车轮表,另一个是引擎表.两个表都有一个car字段,表示该配件对应的车. 对于车轮来说,多个对应一个car的 ...
- Redis消息通知
Redis的消息通知通过列表类型实现,分为两种模式:阻塞式.发布/订阅式: 阻塞式 顾名思义,消息生产者负责生产消息,并将消息放到队列的一端,消息的消费者负责消费消息,从队列的另一端取出消息,然后对其 ...
- HDU2896 病毒侵袭 AC自动机模板
各种MLE,这模板感觉有问题,next数组开128也会MLE,实际上可见字符为编号32~126,只用开100就行. #include <iostream> #include <cst ...
- 过滤html标签的一个函数
str_replace(array(' ', '&', '"', ''', '“', '”', '—', '<', '>', '·', '…', '&'), ar ...
- switch...case...之替换方案一
很多时候,当switch中有N个分支,且分支数已达10+,每个分支都是一个不小的方法体,那我们是不是应该考虑换一种方式来实现这个分支. 而我目前所能想到的是会用到如下几种方法. 1.Action 2. ...