Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)
1. 题目
1.1 英文题目
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
1.2 中文题目
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
1.3输入输出
| 输入 | 输出 |
|---|---|
| prices = [7,1,5,3,6,4] | 7 |
| prices = [1,2,3,4,5] | 4 |
| prices = [7,6,4,3,1] | 0 |
1.4 约束条件
- 1 <= prices.length <= 3 * 104
- 0 <= prices[i] <= 104
2. 实验平台
IDE:VS2019
IDE版本:16.10.1
语言:c++11
3. 分析
这一题可以借鉴121题的方法,也就是Kadane's Algorithm,具体来说就是求出相邻两个元素间的差值,组成数组,再将数组中的正值加到一起,即可。具体代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxpro = 0;
for (int i = 1; i < prices.size(); i++)
maxpro += max(0, prices[i] - prices[i - 1]);
return maxpro;
}
};
Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)的更多相关文章
- 【一天一道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(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-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 & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ 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】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 a ...
- 【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. ...
- 31. 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 price ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- flink的checkpoint页面监控
flink web页面中提供了针对Job Checkpoint相关的监控信息.Checkpoint监控页面共有overview.history.summary和configuration四个页签,分别 ...
- mybatis运行出现org.apache.ibatis.binding.BindingException
今天学习mybatis的第一天,发现用junit测试报出了次异常:org.apache.ibatis.binding.BindingException: Type interface cn.dzp.d ...
- Resource和Autowired区别
1.使用场景 @Resource和@Autowired都是做bean注入时使用 @Resource是jdk的注解,不是spring的注解:由包javax.annotation.Resource提供,需 ...
- THINKPHP_(8)_修改TP源码,支持基于多层关联的任一字段进行排序
之前博文 前述博文THINKPHP_(1)_修改TP源码,支持对中文字符串按拼音进行排序,其解决的主要问题是,对于查询出的think\collection数据,按指定字段对数据进行排序,从而在页面上进 ...
- TVM性能评估分析(一)
TVM性能评估分析(一) System Overview AutoTVM vs Auto-scheduler Table 1. Workflow Comparision Figure 1. Searc ...
- 部署TVM Runtime
部署TVM Runtime本文主要介绍如何在开发板上部署TVM Runtime, 在本地机器安装完整的TVM(包含了TVM Runtime以及编译功能), 并且使用一个简单的远程调用例子测试是否部署成 ...
- Covid经济型自主汽车
Covid经济型自主汽车 Autonomous Vehicles in Covid Economy Covid经济已经对汽车行业产生了负面影响,更多的变化正在进行中,同时也带来了大量的不确定性.我们可 ...
- CVPR2020:端到端学习三维点云的局部多视图描述符
CVPR2020:端到端学习三维点云的局部多视图描述符 End-to-End Learning Local Multi-View Descriptors for 3D Point Clouds 论文地 ...
- TcaplusDB祝大家端午安康!
"五月五,端午到,赛龙舟,真热闹.吃粽子,带香包,蚊虫不来身边闹."这首脍炙人口.描绘着端午节风俗的儿歌,想必大家都听过. 每年的农历五月初五,是我国四大传统节日(春节.清明节.端 ...
- 瞎搞!你真的懂什么是ERP、中台和低代码吗?
欢迎关注微信公众号:sap_gui (ERP咨询顾问之家) 互联网信息圈就是这样,写个不知所云的文章,再配上博眼球的标题,立马引来众多围观,不少公众号也蜂拥转载,完全不去思考文章写得对不对,有没有道理 ...