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 ...
随机推荐
- 系统负载load
一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在苹果公司的Mac电脑上也适用 ...
- 【NOIP2018模拟11.01】树
题目 描述 题目大意 维护一个序列,支持三种操作: 1.修改一段区间,将这段区间内的所有数都andandand一个数. 2.询问区间和. 3.询问区间两两相加的平方和. N≤10000N\leq 10 ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--08--06CMDB测试Linux系统采集硬件数据的命令03
https://www.virtualbox.org/wiki/Downloads https://mirrors.aliyun.com/centos/7/isos/x86_64/ http://ww ...
- CentOS设置打开终端快捷键
- 关于mapreduce 开发环境部署和jar包拷贝问题
1.mapreduce开发应当在linux里面的eclipse不然容易出现问题. 2.把eclipse拷贝到linux环境中,然后需要拷贝hadoop-eclipse-plugin-2.3.0.jar ...
- 使用 windows 批处理指令(BAT文件)进行压缩文件(zip)解压操作
以下指令包括文件删除.复制.zip文件解压操作.使用7z指令指令进行解压操作前,需要确保 windows 的 path 系统环境变量中存在7z的安装路径. 7z的下载地址:https://www.7- ...
- Spring学习:程序的耦合和解耦的思路分析
程序的耦合 耦合:程序间的依赖关系 包括: 类之间的依赖 方法间的依赖 解耦: 降低程序间的依赖关系 在实际开发中: 应该做到,编译期不依赖,运行时才依赖 解耦思路: 第一步:使用反射来创建对象,而避 ...
- T2485 汉诺塔升级版(普及)(递归)
https://www.luogu.org/problem/show?pid=T2485 题目背景 汉诺塔升级了 题目描述 现在我们有N个圆盘和N个柱子,每个圆盘大小都不一样,大的圆盘不能放在小的圆盘 ...
- Java中移动文件或目录的方法盘点
本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:Java中移动文件或目录的方法盘点: import org.apache.commons.io.FileUtils; import jav ...
- Gradle:gradle下载插件
https://github.com/michel-kraemer/gradle-download-task 用法在readme中已经讲的很清楚了,我主要介绍下注意事项吧. 我用这个插件的目的是为了让 ...