【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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
解题:简单贪心,如果今天买,明天涨就不卖,如果明天跌就卖掉,如果后天涨明天就又买进来。
代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int answer = ;
if(prices.size() == )
return ;
for(int i = ;i < prices.size()-;i++)
{
if(prices[i+]>prices[i])
answer += prices[i+]-prices[i];
}
return answer;
}
};
我还问了这个问题:http://oj.leetcode.com/discuss/4082/why-do-i-have-to-add-if-prices-size-0-return-0
Java版本:
public class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
for(int i = 0;i < prices.length-1;i++){
if(prices[i+1] > prices[i])
sum += prices[i+1]-prices[i];
}
return sum;
}
}
【leetcode刷题笔记】Best Time to Buy and Sell Stock II的更多相关文章
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- LeetCode(122) 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 ...
- LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
Description Say you have an array for which the ith element is the price of a given stock on day i. ...
- [LeetCode&Python] Problem 122. 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 II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- 122. Best Time to Buy and Sell Stock II@python
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
随机推荐
- Servlet的API(二)
web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象.request和response对象既然代表请求和响应,那我们获取 ...
- java之JDK动态代理
© 版权声明:本文为博主原创文章,转载请注明出处 JDK动态代理: JDK动态代理就是在程序运行期间,根据java的反射机制自动的帮我们生成相应的代理类 优势: - 1. 业务类只需要关注业务逻辑本身 ...
- Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java
Atitit.程序包装exe启动器 打包 发布 设计 -生成exe java 1. 要实现的功能1 2. ahk是个好东东..能启动了...1 3. exe4j vs nativej1 4. 2 ...
- Atitit.软件硕士 博士课程 一览表 attilax 总结
Atitit.软件硕士 博士课程 一览表 attilax 总结 1. Attilax聚焦的领域1 2. 研究生硕士博士课程汇总表1 3. 博士课程3 4. Attilax额外的4 5. 参考4 1. ...
- android 蓝牙低耗能(LBE)技术介绍
蓝牙低能耗(BLE)技术是低成本.短距离.可互操作的鲁棒性无线技术.工作在免许可的2.4GHz ISM射频频段.它从一開始就设计为超低功耗(ULP)无线技术. 它利用很多智能手段最大限度地减少功耗. ...
- CP936 转换成 UTF-8
最近写了一个抓取脚本,抓取的大部分内容正常,但少部分乱码 检测字符编码,得出的结果是CP936 mb_detect_encoding($str, 'GBK, gb2312, GB18030, ISO- ...
- python 文字识别 之 pytesseract
pytesseract资源 链接:https://pan.baidu.com/s/1eTsqhsY 密码:j0yo 安装时前面一直next就可以了,直到这一步,勾选Math和Chinese,支持计算和 ...
- 使用CSDN CODE来存放OPENSTACK位于GITHUB上的源代码
use CSDN CODE to pull openstack codes 2014-11-20 Author:Hyphen 问题 直接从GITHUB上获代替码,常常是没保障,特别是用DEVSTACK ...
- 安装mingw后,在命令窗体编译c文件
1.编译test.cpp文件 #include<iostream> int main(int argc,char **argv) { std::cout<<"he ...
- 【BZOJ2115】[Wc2011] Xor 高斯消元求线性基+DFS
[BZOJ2115][Wc2011] Xor Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ...