leetcode 买卖股票问题
leetcode121 Best Time to Buy and Sell Stock
说白了找到最大的两组数之差即可
class Solution {
public:
int maxProfit(vector<int>& prices) {
int m = ;
for(int i = ; i < prices.size(); i++){
for(int j = i + ; j < prices.size(); j++){
m = max(m, prices[j] - prices[i]);
}
}
return m;
}
};
leetcode122 Best Time to Buy and Sell Stock II
关键在于明白股票可以当天卖出再买进
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == ) return ;
int m = ;
for(int i = ; i < prices.size() - ; i++){
if(prices[i] < prices[i + ]){
m += prices[i + ] - prices[i];
}
}
return m;
}
};
leetcode 买卖股票问题的更多相关文章
- LeetCode 买卖股票的最佳时机 II
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次 ...
- LeetCode 买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【Leetcode】【简单】【122. 买卖股票的最佳时机 II】【JavaScript】
题目描述 122. 买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票) ...
- [LeetCode] 121. 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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 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] 309. 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 ...
随机推荐
- 在命令行启动vscode
1.windows使用 code . 命令打开vscode 1.打开vscode安装位置,进入bin文件夹,复制路径 eg:E:\Microsoft VS Code\bin:2.回到桌面,右键我的电脑 ...
- Vue.js错误: Maximum call stack size exceeded
这几天正自学Vue, 用eggjs + vue 采用前后分离,写一个网站练练手. 增加了一个商品详情页 Detail.vue的时候,点击进入Detail.vue的时候,页面显示空白,打开浏览器调试工具 ...
- Kafka 1.0.0集群安装
环境 主机名 IP 地址 安装路径 系统 sht-sgmhadoopdn-01 172.16.101.58 /opt/kafka_2.12-1.0.0 /opt/kafka(软连接) CentOS L ...
- win10php环境变量配置(xampp环境)
我的电脑--属性(右键)--高级系统设置--环境变量--系统变量--Path--编辑 新建两条变量: 一个是xampp文件下的php文件,例如 C:\xampp\php 一个是xampp文件下的php ...
- idea crack
ThisCrackLicenseId-{ “licenseId”:”11011”, “licenseeName”:”Wechat”, “assigneeName”:”tree-deep-see-dee ...
- 跟随我在oracle学习php(13)
常用的css样式 [class~="col-6"]:选择我所有类名中包含有col-6独立单词的元素 [class*="col-"]:选择所有类名中含有" ...
- Python的魔术方法总结
魔术方法:再不需要程序员定义,本身就存在类中的方法就是魔术方法. 魔术方法通常都长这样:__名字__. 1.__str__和__repr__ 为了方便记忆看如下列子 class Course: def ...
- validation-api各注解的用法
入参用@Valid,要不下面实体类中的注解不生效 @AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为True@DecimalMax(value) 被注解的 ...
- 第一次实验报告x
C程序设计实验报告 实验项目:2.3.3字符与ASCII码,2.3.4运算符的表达式与应用,2.3.5顺序结构应用程序,3.3.1数学函数的算法描述,3.3.2鸡兔同笼的算法描述,3.3.3确定坐标的 ...
- .net正则匹配
char[] weixin = txtweixinhao.Text.Trim().ToCharArray(); for (int i = 0; i < weixin.Length; i++) i ...