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 ...
随机推荐
- Azure 实践(4)- CI/CD .netcore项目Docker构建及部署
上篇已介绍了.netcore项目构建的相关步骤,本篇继续完善 1.什么是CI/CD CI/CD 中的"CI"始终指持续集成,它属于开发人员的自动化流程.成功的 CI 意味着应用代码 ...
- easyx小游戏
#include "stdafx.h" int main(){ srand(time(NULL)); initgraph(640,480); int user_x=20,user_ ...
- 关于在.H文件中定义变量
KEIL中,在".H"文件定义变量. 如果该".H"文件同时被两个".C"文件调用,则会出现重复定义错误(*** ERROR L104: M ...
- Nginx系列(3)- 负载均衡
负载均衡 Nginx提供的负载均衡策略有两种: 内置策略为轮询.加权轮询.ip hash 扩展策略,就天马行空了,只有你想不到的没有它做不到的 轮询 加权轮询(根据权重来) iphash对客户端请求 ...
- Appium WebView控件定位
背景 移动应用可以粗分为三种:原生应用(native app), 网页应用(web app,或HTML5 app),以及它们的混血儿--混合模式移动应用(hybrid app). 什么是Hybrid ...
- 判断javaScript变量是Ojbect类型还是Array类型
JavaScript是弱类型的语言,所以对变量的类型并没有强制控制类型.所以声明的变量可能会成为其他类型的变量, 所以在使用中经常会去判断变量的实际类型. 对于一般的变量我们会使用typeof来判 ...
- ARC122C-Calculator【乱搞,构造】
正题 题目链接:https://atcoder.jp/contests/arc122/tasks/arc122_c 题目大意 一个数对开始是\((0,0)\),每次可以选择一个数加一或者让一个数加上另 ...
- 三、mybatis多表关联查询和分布查询
前言 mybatis多表关联查询和懒查询,这篇文章通过一对一和一对多的实例来展示多表查询.不过需要掌握数据输出的这方面的知识.之前整理过了mybatis入门案例和mybatis数据输出,多表查询是在前 ...
- yolov5实战之二维码检测
目录 1.前沿 2.二维码数据 3.训练配置 3.1数据集设置 3.2训练参数的配置 3.3网络结构设置 3.4训练 3.5结果示例 附录:数据集下载 1.前沿 之前总结过yolov5来做皮卡丘的检测 ...
- 第一个Java HelloWorld
步骤 1.在记事本或者notePad++中编写java代码 public class Hello { public static void main(String[] args){ System.ou ...