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 ...
随机推荐
- Element-ui上传文件(删除、添加、预览)
先看下效果是不是你所需要的.... 上传文件进度条后续会加上的.... 功能需求:默认为上传状态 1.未上传:点击可上传文件 2.已上传:点击可上传文件 (1).鼠标移入[删除] (2).鼠标点击[预 ...
- 推荐一个好用的E2E前端测试框架cypress
Cypress 是一个E2E的前端自动化测试框架,同样是基于BDD的思想设计的,话不多说,上demo https://github.com/Spillage/cypress-demo PS, 还有一个 ...
- markdown首行缩进
首行缩进两个字符:(每个表示一个空格,连续使用两个即可) 半角的空格 全角的空格
- 学号20175212 《Java程序设计》第7周学习总结
学号20175212 <Java程序设计>第7周学习总结 教材学习内容总结 8.1.String类 可以使用String类声明对象并创建对象,例如: String s = new Stri ...
- web plugins
<build> <resources> <resource> <directory>src/main/java</directory> &l ...
- Jedis路由key的算法剥离
在Redis集群中,会有很多个分片,如果此时利用Jedis来操作此Redis集群,那么他会把数据路由到不到的分片上.而且如果动态的往集群中增加分片,也不会影响Jedis的功能.究竟是怎么做到的呢? 由 ...
- js的回调函数
介绍首先从英文介绍开始 A callback is a function that is passed as an argument to another function and is execut ...
- javascript事件流机制
(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...
- 使用Python+turtle绘制动画重现龟兔赛跑现场
问题描述: 在经典的龟兔赛跑故事中,兔子本来是遥遥领先的,结果因为骄傲,居然在比赛现场睡了一觉,醒来后发现乌龟已经快到终点了,于是赶紧追赶,无奈为时已晚,最终输掉了比赛.本文使用turtle绘制乌龟和 ...
- JDBCUtils——原生
需要导入的包: mysql-connector-java-5.1.37-bin.jar import java.sql.Connection; import java.sql.DriverManage ...