【leetcode】121-Best Time to Buy and Sell Stock
problem
121. Best Time to Buy and Sell Stock
code
class Solution {
public:
int maxProfit(vector<int>& prices) {
int res = ;
for(int i=; i<prices.size(); i++)
{
int buy_price = prices[i];
for(int j=i+; j<prices.size(); j++)
{
int sell_price = prices[j];
int tmp = sell_price - buy_price;
if(tmp>res) res = tmp;
}
}
return res; }
};
one pass
class Solution {
public:
int maxProfit(vector<int>& prices) {
int min_price = INT_MAX;
int max_profit = ;
for(int i=; i<prices.size(); i++)
{
if(min_price> prices[i]) min_price = prices[i];
else if(prices[i]-min_price>max_profit) max_profit = prices[i]-min_price;
}
return max_profit;
}
};
参考
1. Leetcode_Best Time to Buy and Sell Stock;
完
【leetcode】121-Best Time to Buy and Sell Stock的更多相关文章
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- 【刷题-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 ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 【LeetCode】数独
判断一个数独是否合法,未填的空格用字符 ' . ' 表示.该数独有解并不是必要的. e.g. 如图合法数独,输入 ["53..7....","6..195..." ...
- Java序列化的作用和反序列化
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object states,但 ...
- Spring控制反转(依赖注入)的最简单说明
1.常规方式实现实例化 1.1已有角色如下: 一个接口Interface,两个接口实现类InstatnceA.InstanceB,一个调用类User 1.2当前实例化InstanceA如下: Inte ...
- sql group by max
SELECT * , REPLACE(TDFG.xdfd,'doc_111','') GBFROM ( SELECT * FROM ...
- HTTP上传数据 :表单,二进制数据(multipart/form-data application/octet-stream boundary)
使用WinINet 一个较简单的例子:上传头像 void CBackstageManager::UpdateAvatarThreadProc(LPVOID params) { stForThread* ...
- linux操作系统及命令Part 2
cat 命令 cat .txt .txt .txt > Ta.txt 将左边三个文件纵向合并为Ta文件 cat .txt>> Ta.txt 将左边文件的内容添加到Ta文件中 tar ...
- nginx:负载均衡实战(二) keepalived入门
1.keepalived介绍 顾名思义,keepalived就是保持网络在线的,用来保证集群高可用HA的服务软件.主要防止出现单点故障(坏了一个点导致整个系统架构不可用) 2.详解keepalived ...
- Oracle shrink space
一.开启表的行迁移 alter table table_name enable row movement; select 'alter table '||s.owner||'.'||s.table_n ...
- ubuntu查看占用某端口的程序
查看端口使用情况,使用netstat命令. 查看已经连接的服务端口(ESTABLISHED netstat -a 查看所有的服务端口(LISTEN,ESTABLISHED) netstat -ap 查 ...
- 九. Python基础(9)--命名空间, 作用域
九. Python基础(9)--命名空间, 作用域 1 ● !a 与 not a 注意, C/C++可以用if !a表示if a == 0, 但是Python中只能用if not a来表示同样的意义. ...