【Best Time to Buy and Sell Stock II】cpp
题目:
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 sum_profits = , pre_price = prices[];
for ( size_t i = ; i < prices.size(); ++i )
{
sum_profits += (prices[i]>pre_price) ? prices[i]-pre_price : ;
pre_price = prices[i];
}
return sum_profits;
}
};
tips:
Greedy算法。
核心算法:如果当天的价格比前一天高,sum_profits就累加上当天的利润与前一天利润的差值(即昨天买,今天买);如果当天的价格比昨天低,则不更新sum_profits(即今天买,今天卖)。
========================================
第二次过这道题,低买高卖。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ret = ;
for ( int i=; i<prices.size(); ++i )
{
if ( prices[i]>prices[i-] )
{
ret += prices[i]-prices[i-];
}
}
return ret;
}
};
【Best Time to Buy and Sell Stock II】cpp的更多相关文章
- leetcode 【 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 a ...
- 【Best Time to Buy and Sell Stock III 】cpp
题目: 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 III 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- 【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 ...
- 【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
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 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 ...
随机推荐
- 使用FusionCharts创建可更新数据的JavaScript图表
先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-app启动画面
1.app启动画面设计 用photoshop设计启动画面,图片分辨率为720*1280 保存的文件名为splash.png 将splash.png复制到res\drawable,如图 PS:要先添加闪 ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:7.项目介绍之架构(1)
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 <迷你微信>服务器端是使用Java语言,Mina框架编写的,一个良好的架构关系到后期迭代的方便程度 ...
- 面试官:自己搭建过vue开发环境吗?
开篇 前段时间,看到群里一些小伙伴面试的时候被面试官问到这类题目.平时大家开发vue项目的时候,相信大部分人都是使用 vue-cli脚手架生成的项目架构,然后 npm run install 安装依赖 ...
- SAP成都研究院CEC团队三巨头之一:M君的文章预告
国人总倾向于把特点或者作用类似的人或物放在一起比较并做出排名,于是就有了许多"某某某三巨头"的称谓. 最举世闻名的莫过于二战三巨头:丘吉尔,罗斯福和斯大林. 还有陪伴咱八零后童年时 ...
- codeforce 599B Spongebob and Joke
一道水题WA那么多发,也是醉了.f看成函数的话,其实就是判断一下反函数存不存在. 坑点,只能在定义域内判断,也就是只判断b[i].没扫一遍前不能确定Impossible. #include<bi ...
- 【BZOJ4810】[YNOI2017] 由乃的玉米田(莫队+bitset)
点此看题面 大致题意: 给你一段序列,每次询问一段区间内是否存在两个数的差或和或积为\(x\). 莫队算法 看到区间询问+可以离线,首先想到了莫队啊. 但是,在较短的时间内更新信息依然比较难以实现. ...
- 2018.5.14 XML文档类型定义----DTD
1.DTD概述 一个完全意义上的XML文件不仅仅是Well-fromed(格式良好的),而且还应该是使用了一些自定义的标记ValidatingXMl(有效的)文档也就是说他必须遵守文档类型的定义中已声 ...
- 说说qwerty、dvorak、colemak三种键盘布局
[qwerty布局] qwerty布局大家应该都很熟悉了,全世界最普及的键盘布局. 截止到去年接触并使用dvorak布局之前,我使用了十几年qwerty布局,在http://speedtest.10f ...
- Charles拦截请求
一.通过Charles抓包,可拦截请求并篡改交互信息 1.可篡改客户端向服务器发起的请求信息(服务器收到的是假消息) 2.可篡改服务器返回给客户端的响应结果(客户端看到的是假消息) 二.篡改用户请求 ...