best-time-to-buy-and-sell-stock leetcode C++
Say you have an array for which the i th 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.
C++
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
if (prices[i] < curMin)
curMin = prices[i];
else
maxPro = max(prices[i] - curMin,maxPro);
}
return maxPro;
}
int maxProfit4(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
int cur = prices[i];
if (cur < curMin)
curMin = cur;
else{
int curPro = cur - curMin;
if (curPro > maxPro)
maxPro = curPro;
}
}
return maxPro;
}
int maxProfit2(vector<int> &prices) {
if(prices.size() < 2) return 0;
int maxPro = 0;
int curMin = prices[0];
for(int i =1; i < prices.size();i++){
if (prices[i] < curMin)
curMin = prices[i];
else
if (prices[i] - curMin > maxPro)
maxPro = prices[i] - curMin;
}
return maxPro;
}
};
best-time-to-buy-and-sell-stock leetcode C++的更多相关文章
- Best Time to Buy and Sell Stock - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...
- Best Time to Buy and Sell Stock——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Best Time to Buy and Sell Stock leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 121. Best Time to Buy and Sell Stock——Leetcode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode
LeetCode 我们有一个股票的数组,数组是每时间的钱,我们只能买入一次和卖出一次,求我们的最大收益. 我们知道了一个数组,那么我们可以在低价买入,然后高价卖出,但是需要知道我们的低价需要在高价之前 ...
- [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 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 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 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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- composer 包 slim使用案例,一个简单的路由解决方案
nginx配置文件修改 location / { try_files $uri /index.php$is_args$args; } 设置好nginx伪静态,把所有的请求方式都转向到index.php ...
- 关于buildroot移植的思考
buildroot是一个成熟的SDK框架,基于它有了openwrt. 曾经有一个项目,需要将原有的OpenWrt SDK改造,并且将软件框架重新定义.尝试精简原来的OpenWrt,并且删除所有的软件包 ...
- MySql分区、分表和分库
MySql分区.分表和分库 数据库的数据量达到一定程度之后,为避免带来系统性能上的瓶颈.需要进行数据的处理,采用的手段是分区.分片.分库.分表. 一些问题的解释: 1.为什么要分表和分区? 日常开发中 ...
- Mybatis中使用级联查询,一对多的查询
一.需求描述 自己在开发一个小程序的过程中,需要做的一个查询是稍微比较复杂的查询,根据用户信息去查询用户所对应的宠物信息. 一个用户可能对应多个宠物,所以在用户和宠物信息的对应关系就是一对多的关系. ...
- Java项目常用的统一返回跟统一异常处理
先创建一个crud的项目. controller调用service调用mapper 以下以简单代码代替 controller @GetMapping("/getUserById") ...
- http报文常见的请求头、响应头
http报文常见的请求头 1.Accept Accept: text/html 浏览器可以接受服务器回发的类型为 text/html. Accept: / 代表浏览器可以处理所有类型,(一般浏览 ...
- 博客调网易云歌单JS
<!--音乐--> <link rel="stylesheet" href="https://blog-static.cnblogs.com/files ...
- Go语言之数组与切片基础
一.数组 数组是同一类型元素的集合,可以放多个值,但是类型一致,内存中连续存储 Go 语言中不允许混合不同类型的元素,而且数组的大小,在定义阶段就确定了,不能更改 1.数组的定义 // 定义一个大小为 ...
- Python:raschii库计算任意阶数Stokes波
Stokes五阶波 最近发现一个很有用的Stokes波计算Python库,raschii官方说明,可以计算任意阶数,不同水深下的Stokes波,简单做了下测试,测试结果与脚本如下 Python 脚本 ...
- Pandas高级教程之:时间处理
目录 简介 时间分类 Timestamp DatetimeIndex date_range 和 bdate_range origin 格式化 Period DateOffset 作为index 切片和 ...