【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 ...
随机推荐
- Struts2学习小结
1 基础 使用:导入 jar 包,配置 web.xml,并引入 struts.xml 文件 DMI:动态方法调用,调用时使用!分隔 action 名与方法名,如 index ! add.action, ...
- 通过特定获取获取电脑外网IP地址
void get_WanIp() { }; ]; ]; ; }; GetTempPathA(MAX_PATH,szFilePath); strcat(szFilePath,"IPinTheW ...
- 应用沙盒(Application Sandbox)
一.应用沙盒目录 应用沙盒包含多个目录: 1.应用程序包:(application bundle):包含所有的资源文件和可执行文件,并且是只读目录. 2.Library/Preferences/:存放 ...
- Windows进程间通信--共享内存映射文件(FileMapping)--VS2012下发送和接收
之前以为两个互不相关的程序a.exe b.exe通信就只能通过网络,人家说可以通过发消息,我还深以为不然,对此,我表示万分惭愧. 之前课本上说的进程间通信,有共享内存.管道等之类的,但没有自己操刀写过 ...
- iOS Masonry 查看更多 收起
Masonry 查看更多 收起效果实现,带动画 demo下载地址: https://github.com/qqcc1388/MasonryDemo
- secureCrt Linux 文件传输
1.在secureCRT终端下输入rz命令,查看Linux是否安装rz文件传输服务 如果提示未安装则先安装rz服务:安装命令: apt-get install lrzsz 2.进入要上传的目的文件 ...
- 搭建redis集群遇到的坑
搭建redis集群遇到的坑 #!/bin/bash # 作者: tuhooo # 日期: 2017.4.23 20.15 # 用途: 通过ruby脚本启动redis伪集群 if [ $2 == &qu ...
- mysql 归档方案(一次性)
一. 归档流程: 1. 导出需要的数据 2. 创建临时表table_tmp 3. 导入数据到临时表 4. 修改原始表名为table_bak 5. 修改临时表为原始表名 二.归档方式对比 1. sele ...
- 01 Memcached 安装与介绍
一:Memcached 介绍 ()官网网址:www.mamcached.org () 主要功能是:高性能,分布式的内存对象缓存系统. ()Nosql不仅仅是关系型数据库,显著特点key value ...
- 编程算法 - 二叉树的最低公共祖先 代码(C)
二叉树的最低公共祖先 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉树的最低公共祖先(lowest common ancestor), 首先先序遍 ...