【Best Time to Buy and Sell Stock】cpp
题目:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
代码:
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size()==) return ;
int min_price = prices[], max_profit = ;
for ( size_t i = ; i < prices.size(); ++i )
{
min_price = std::min(min_price, prices[i]);
max_profit = std::max(max_profit, prices[i]-min_price);
}
return max_profit;
}
};
tips:
Greedy
核心在于维护两个变量:
min_price: 算上当前元素的最小值
max_profit:算上当前元素的最大利润
走完所有元素,可以获得max_profit
================================================
第二次过这道题,犹豫了一下才想起来思路。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int globalProfit = ;
if ( prices.empty() ) return globalProfit;
int minPrice = prices[];
for ( int i=; i<prices.size(); ++i )
{
minPrice = min(minPrice, prices[i]);
globalProfit = max(globalProfit, prices[i]-minPrice);
}
return globalProfit;
}
};
【Best Time to Buy and Sell Stock】cpp的更多相关文章
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
思路: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- leetcode 【 Best Time to Buy and Sell Stock II 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【Best Time to Buy and Sell Stock III 】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【Best Time to Buy and Sell Stock II】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- leetcode 【 Best Time to Buy and Sell Stock III 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- redis空间键详解
前言 redis的空间键通知是在2.8.0版本以后加入的,客户端通过发布订阅的方式,订阅某个频道,接收通过某种方式影响redis中数据的事件. 目录: 1.空间键事件分类 2.如何启用redis的空间 ...
- ArcGIS中合并空间有压盖关系的要素属性
1.前言 在客户单位, 被客户问道这样一个问题“如何合并两个有压盖关系图层的属性信息?” 在工具箱里面可以使用以下工具解决: 2.处理过程 (1)在工具箱中选择Spatial Join工具,并设置相关 ...
- Redis基础数据结构
Redis数据库中每个键值对都是由对象( c 的结构体对象)组成的. 数据库键总是一个字符串对象(string object) 数据库键的值可以使字符串对象.列表对象(list object).哈希对 ...
- bzoj2600 [Ioi2011]ricehub
Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...
- 3219: 求最高同学位置—C语言版
3219: 求最高同学位置—C语言版 时间限制: 1 Sec 内存限制: 128 MB提交: 207 解决: 115[提交][状态][讨论版][命题人:smallgyy] 题目描述 设一维数组存放 ...
- Python 之继承
概要 如果要修改现有类的行为,我们不必再从头建一个新的类,可以直接利用继承这一功能.下面将以实例相结合介绍继承的用法. 新建一个基类 代码如下: class Marvel(object): num ...
- Python监控日志中经常访问的ip
一.需求:每分钟检查一次日志文件,如果这一分钟内同一个ip请求次数超过200次,加入黑名单 1.日志文件中,每一行的格式为:XXX.XXX.XXX.XXX - - [04/Jun/2017:05:25 ...
- 浏览器 DNS缓存与DNS prefetch (DNS预解析)
浏览器 DNS缓存 浏览器DNS缓存的时间跟DNS服务器返回的TTL值无关. 注:TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间. 浏览器在获取网站域名的实际IP地 ...
- [未完] term.js 记录遇到的问题
参考博文:https://www.cnblogs.com/zhenfei-jiang/p/7065038.html 按照网上查找的资料敲了代码 term.on('data', function(dat ...
- Uva 填充正方形
暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...