Best Time to Buy and Sell Stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
//eg: 5 6 2 3 1 4;
// 记录i之前最小的元素;
int min = INT_MAX;
int pro = ;
int n=prices.size();
for(int i=;i<n;i++){
if(prices[i]-min >pro) pro=prices[i]-min;
else if(prices[i] < min) min = prices[i];
}
return pro;
}
};
Best Time to Buy and Sell Stock的更多相关文章
- [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 ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
随机推荐
- C++学了这么多年,你也许不知道为什么类定义要放在.h文件,类实现放在cpp文件。它们如何关联?
原文 http://blog.csdn.net/ithzhang/article/details/8119286 主题 C++ C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类 ...
- WPF中model属性即时改变
新建一个model作为说明即可,以便查阅. 添加引用:using System.ComponentModel ; public class Test:INotifyPropertyChanged { ...
- 重新拷贝 新项目 发现不显示 原来是 paramiko 没有装
proxy pass 端口换成 另一个 跟原来的不冲突 [root@ayibang-server soft_ware]# cp s10day11/demo.* s10ops/[root@ayibang ...
- Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- C# ?和??运算表达式
1.单问号(?) 作用:用于给变量设初化的时候,给变量(int类型)赋为null值,而不是0. 例子: public int a; //默认值为0 public int ?b; //默认值为null ...
- npm 介绍
安装NPM NPM的全称是Node Package Manager,如果你熟悉ruby的gem,Python的PyPL.setuptools,PHP的pear,那么你就知道NPM的作用是什么了.没 错 ...
- PostgreSQL 中定义自己需要的数据类型
PostgreSQL解决某系数据库中的tinyint数据类型问题,创建自己需要的数据类型如下: CREATE DOMAIN tinyint AS smallint CONSTRAINT tinyint ...
- PostgreSQL 系统的基本体系结构
PostgreSQL 使用客户机/服务器(C/S)的模式提供服务,一个PostgreSQL会话由下列相关的进程(程序)组成: (1)一个服务器端进程.该进程管理数据库文件,接受客户端与数据库的连接,且 ...
- navicat的简单应用
首先 创建连接 主机名 : 可以不写名称随意 主机名/IP地址:localhost或者127.0.0.1 都是本机的意思 端口:默认3306 尽量不要改怕与其余端口重复,如有重名端口系统会报错 ...
- 树形DP(Rebuilding Roads poj1947)
题意:给出一颗树,求要形成一颗元素个数是p的子树,最少要去掉多少边 #include"stdio.h" #include"string.h" #include& ...