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 ...
随机推荐
- gdkoi前的复习
又浪了一天…… 整理下学的,这两天都温习(预习)一下吧. 27号就是gdkoi了好怕…… 数据结构 ------树 -------------平衡树 -------------线段树/树状数组 --- ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 微信小程序使用原生WebSokcet实现断线重连及数据拼接
以前做小程序为了应急找了个插件去链接WebSokcet,文章传送门. 回过头在新项目中再次使用时出现了些许问题,不一一赘述.遂决定好好用一下原生的WebSokcet. 一.说明 1.小程序原生的Web ...
- React & Redux 的一些基本知识点
一.React.createClass 跟 React.Component 的区别在于后者使用了ES6的语法,用constructor构造器来构造默认的属性和状态. 1. React.createCl ...
- Different Integers 牛客多校第一场只会签到题
Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...
- centos6上使用fpm打python2.7 rpm包并兼容python2.6
centos6上使用fpm打python2.7 rpm包并兼容python2.6 作者 运维小兵_加油 关注 2016.09.22 00:28 字数 501 阅读 45评论 0喜欢 1 工作中我们常常 ...
- grub ubuntu启动
set root=(hd0,gpt10) 现在变为 gpt9 了 安装固态后.变成了 (hd1,gpt11) set prefix=(hd0,gpt10)/boot/grub insmod norma ...
- (转)用python实现抓取网页、模拟登陆
涉及一系列内容,部分已在前面转载,仍转自crifan: http://www.crifan.com/how_to_use_some_language_python_csharp_to_implemen ...
- jquery教程-Jquery 获取标签个数 size()函数用法
jquery教程-Jquery 获取标签个数 size()函数用法,size() 方法返回被 jQuery 选择器匹配的元素的数量. 语法 $(selector).size() jQuery ...
- 关押罪犯洛谷P1525
题目+评测传送门 思路 其实这一题有2种不同的思路,但是由于我实在是太蒟蒻了,只会其中一种,另一种看了半天都不知道它在讲什么/(ㄒoㄒ)/~~ 首先,我们要学习一下二分图及其判断方法博客,然后这个题目 ...