leetcode122 Best Time to Buy and Sell Stock
题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益。
思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二天卖出,再买进,第三天卖出,第四天买进,第五天卖出。
真正计算的就是前一天的价格和当天的价格的差值,[1,3,-2,3],大于0买进,否则不买。
代码:
- int maxProfit(vector<int>& prices) {
- int n = prices.size();
- int a[n-];
- for(int i = ;i<n-;i++)
- a[i] = prices[i+]-prices[i];
- int ans = ;
- for(int j = ;j<n-;j++)
- {
- if(a[j]>)
- ans += a[j];
- }
- return ans;
- }
leetcode122 Best Time to Buy and Sell Stock的更多相关文章
- LeetCode122: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 a ...
- Leetcode-122 Best Time to Buy and Sell Stock II
#122 Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...
- LeetCode122——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 a ...
- [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 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 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 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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LintCode] 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 ...
随机推荐
- 周记【距gdoi:96天】
倒计时从三位数变成了两位数. 然后这周还是很不知道怎么说,经常写一道题写两天.但是总算把后缀数组写完了,也整理完了. 然后周末都不知道干了什么周末就过去了.无聊看了两道省选题发现都是不会做系列,看了以 ...
- BZOJ1093 [ZJOI2007]最大半连通子图 【tarjan缩点 + DAG最长路计数】
题目 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意 两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G ...
- 【NOIP 模拟赛】Evensgn 剪树枝 树形dp
由于树规做的少所以即使我考试想出来正确的状态也不会转移. 一般dp的转移不那么繁杂(除了插头.....),即使多那也是清晰明了的,而且按照树规的一般思路,我们是从下到上的,所以我们要尽量简洁地从儿子那 ...
- 一个简易的Python全站抓取系统
很长时间没有更新博客了,前一阵时间在做项目,里面有一个爬虫系统,然后就从里面整理了一点代码做成了一个简易的爬虫系统,还挺实用的. 简单说来,这个爬虫系统的功能就是:给定初始的链接池,然后设定一些参数, ...
- CentOS 6.4安装配置ldap
CentOS 6.5安装配置ldap 时间:2015-07-14 00:54来源:blog.51cto.com 作者:"ly36843运维" 博客 举报 点击:274次 一.安装l ...
- (转)用python实现抓取网页、模拟登陆
涉及一系列内容,部分已在前面转载,仍转自crifan: http://www.crifan.com/how_to_use_some_language_python_csharp_to_implemen ...
- 【SPOJ-QTREE3】树链剖分
http://www.spoj.com/problems/QTREE3/ 时间限制:2s 代码长度限制:50000B 内存限制:1536MB [题目描述] 给出N个点的一棵树(N-1条边 ...
- HDU1847 Good Luck in CET-4 Everybody!
大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在 考场浸润了十几载的当代大学生,Kiki和Cici更懂得 ...
- MongoDB 聚合(管道与表达式)
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). aggregate() 方法 MongoDB中 ...
- loggin
# 参考:https://www.cnblogs.com/DI-DIAO/p/8793136.html BASE_LOG_DIR = os.path.join(BASE_DIR, "log& ...