【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 ...
随机推荐
- Vuejs实现轮播图
css: <style type="text/css"> * { margin: 0; padding: 0; list-style: none; } .clearfi ...
- Linux NTP服务配置 for Oracle RAC
安装Oracle 11g RAC时,我们需要配置ntp服务.在使用虚拟机的情况下对于时钟同步方式的配置有很多种方式,可以使用vmware自带的时钟同步功能,也可以直接将本地的一个节点用作时间服务器.本 ...
- Qt绘制文本一
QPainterPath,使用 drawText且设置字体,再使用painter.drawText方式 效果图: void WgtText::paintEvent(QPaintEvent *event ...
- swagger访问api, TypeError: Failed to fetch
用swagger访问https://localhost:44360/api/ads/1, 得到的结果是 TypeError: Failed to fetch.一开始以为是后端代码问题,检查了好久,才发 ...
- sql group by max
SELECT * , REPLACE(TDFG.xdfd,'doc_111','') GBFROM ( SELECT * FROM ...
- 关于Object.prototype.toString.call
slice(8,-1)意思是从第8位开始(包含第8位)到最后一位之前(-1的意思就是最后一位,不包含最后一位): Object.prototype.toString.call(boj)这个是用来判断数 ...
- 读入excle
可以输出到csv(逗号间隔,具体搜索csv格式). csv可以在excel中直接导入. 也可以用system函数调用ssconvert从csv转xlsx:system("ssconvert ...
- QuickStart系列:docker部署之MongoDB
MongoDB[1] 是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB[2] 是一个介于关系数据库和非关系数据库之间的产品, ...
- vue-router-2-动态路由配置
const User = { template: '<div>User{{ $route.params.id }}</div>' } const router = new Vu ...
- iconfont.cn批量加入
iconfont.cn还没有一个批量加入的功能 以下是最新的图标批量加入购物车功能代码. var icons = document.querySelectorAll('.icon-gouwuche1' ...