【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 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) {
if (prices.size() == ) return ;
int buy_in = prices[], sum = ;
for (int i = ; i < prices.size(); ++i) {
if (prices[i] > buy_in) {
sum += prices[i] - buy_in;
buy_in = prices[i];
} else {
buy_in = prices[i];
}
}
return sum;
}
};
后来在论坛上发现了更加简洁的解法,核心思想其实是比较类似的:
int maxProfit(vector<int> &prices) {
int ret = ;
for (size_t p = ; p < prices.size(); ++p)
ret += max(prices[p] - prices[p - ], );
return ret;
}
其实就是只要有涨就先卖再买,但是这种解法更加简洁清楚。
【LeetCode】122. Best Time to Buy and Sell Stock II的更多相关文章
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】122.Best Time to Buy and Sell Stock II(股票问题)
You are given an integer array prices where prices[i] is the price of a given stock on the ith day. ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 【刷题-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:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...
随机推荐
- 看完48秒动画,让你不敢再登录HTTP网站(附完整示例代码)
在我的 单点登录SSO示例代码 一文中,强烈不建议部署HTTP的SSO服务站点. 在此写个基于网络包嗅探的HTTP会话劫持程序,给大家一个直观的危害性展示. 示例中,我在一台Mac上登录58同城,被另 ...
- 预览github项目的html文件新方法
原文地址:→看过来 写在前面 关于如何在线预览github中的html文件,其实这是一个很多人知道的东西,但是查资料的时候呢总是找不到正确的答案,并且一开始我也是踩了坑的. 踩坑经历 搜出来的结果大概 ...
- 关于cgi、FastCGI、php-fpm、php-cgi
搞了好长时间的php了,突然有种想法,想把这些整理在一起,于是查看各种资料,找到一片解释的很不错的文章,分享一下-- 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式 ...
- Hibernate createQuery调用joincolumn
1. Beans a. Version Bean package locationService.beans; import java.sql.Timestamp; import java.util. ...
- Java基础知识二次学习--第四章 异常
第四章 异常处理 时间:2017年4月26日11:16:39~2017年4月26日11:28:58 章节:04章_01节 04章_02节 视频长度:20:46+01:16 内容:异常的概念 心得: ...
- Linux服务器的远程IP限制
系统环境: Linux-centOS+ubuntu 操作: 编辑允许通过IP 路径:vim /etc/hosts.allow sshd:192.168.1.1 编辑禁止通过IP 路径:vim /etc ...
- 使用Criteria 实现两表的左外连接,返回根对象
(转) 引用 两个实体 Parent(P) 和 Child(C)之间是1:N的关系,现要求符合指定条件的P及所包 含的C 采用hibernate中的Criteria来实现此功能的代码如下: Java代 ...
- winform中的 datagriview 字段自动填充长度
在winfrom 的 datagridview 中 绑定字段 经常回在最后面空出一部分来,显得不美观, 现在教大家如何让它自适应宽度 public static void Autogrid(DataG ...
- Ceph Object Gateway Admin api 获取用户列表问题
按照官方文档使用Admin Ops API 获取用户列表 GET /admin/user时 返回{code: 403, message: Forbidden}这里有两个问题:首先用户列表的请求为 如下 ...
- 懵懂oracle之存储过程2
上篇<懵懂oracle之存储过程>已经给大家介绍了很多关于开发存储过程相关的基础知识,笔者尽最大的努力总结了所有接触到的关于存储过程的知识,分享给大家和大家一起学习进步.本篇文章既是完成上 ...