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 ...
随机推荐
- ubuntu nginx ssl 证书配置
前几天自己用 egg.js 写了个 api 接口,然后把它部署到服务器上.服务器是ubuntu 16.04 + nginx:因为要用到https,然后今天实践了一下如何配置https. 关于htt ...
- 一个axios的简单教程
转载于:https://www.jianshu.com/p/13cf01cdb81f 转载仅供个人学习 首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js ...
- W10激活
1.首先,我们先查看一下Win10正式专业版系统的激活状态: 点击桌面左下角的“Windows”按钮,从打开的扩展面板中依次点击“设置”-“更新和安全”,并切换到“激活”选项卡,在此就可以查看到当前系 ...
- restore not found的错误(问题2)
最近在写gan,那么就牵扯到在一个session中加载两个图,restore的时候会有问题.如这篇文章写的(http://blog.csdn.net/u014659656/article/detail ...
- sql数据表中的值重新命名
select u.id,u.name,u.sex, 2 (case u.sex 3 when 1 then '男' 4 when 2 then '女' 5 else '空的' 6 end 7 )性别 ...
- 设置position(absolute,fixed)导致flex布局不生效
个人解决办法:将要设置display:flex的dom外面在套一个div,并且设置宽度,就可以了.
- 下拉菜单css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sql:按年、月、日钻取时间
#按月排SELECT count(EN_NAME), DATE_FORMAT( CREATE_DATE, "%Y-%m" )FROM financeWHERE DATE_FORMA ...
- spring cloud微服务下手动回滚事务
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 这里使用的场景是,跨服务调用接口,比如:用户信息和用户积分 ...
- python之生成器与迭代器
生成器 列表生成式:li = [i for i in range(10)] 将列表生成式中[]换成()即为生成器generator 生成器保存的对象不是具体的数,而是一种算法,一种推导式,调用__ne ...