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).

思路:找出数组中相邻元素差累计和最大的多个序列即可。

比如[1,2,3,4,5,4,2,5],在这个序列中[1,2,3,4,5]和[2,5]为累计和为正数的序列,另外一个知识点就是5-1=(2-1)+(3-2)+(4-3)+(5-4)

代码:

int maxProfit(vector<int>& prices) {
int res = ;
for(size_t i = ; i < prices.size(); i++){
res += max(prices[i] - prices[i - ], );
}
return res;
}

另外一种实现:

int maxprofit(vector<int>& nums){
int res = ;
size_t n = nums.size();
for(size_t i = ; i < n; i++){
if(nums[i] > nums[i - ]){
res += nums[i] - nums[i - ];
}
}
rerurn res;
} //或者用while int maxprofit(vector<int>& nums){
int res = ;
size_t n = nums.size();
while(size_t i < n-){
if(nums[i] > nums[i - ]){
res += nums[i +] - nums[i];
}
i++;
}
rerurn res;
}
 

[Array]122. Best Time to Buy and Sell Stock II(obscure)的更多相关文章

  1. 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 ...

  2. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. 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 al ...

  4. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

  5. 【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. ...

  6. 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 ...

  7. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  8. 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 ...

  9. 【刷题-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 ...

随机推荐

  1. 把类完善了一下,播放器也完善了一下,纯MFC与WinMM的产物

  2. UMP系统架构 Controller服务器

  3. apache反向代理和监听多个端口设置

    修改apache配置文件httpd.conf 一.监听多个端口 在Listen 80后添加监听端口,如 Listen 80 Listen 8080 Listen 8008 二.反向代理设置 1.取消一 ...

  4. 那些使用VSCode写Python踩过的坑(Anaconda配置)

    1. 如何在vscode上配置的配置方法请务必一定要直接参考官方文档Getting Started with Python in VS Code,不要去看什么杂七杂八的blog,要么过时要么不准确要么 ...

  5. 【NOI2010】能量采集

    题面 题目分析 对于第\((i,j)\)个位置,对答案的贡献为\(2*gcd(i,j)-1\). 所以有\(ans=2*\sum\limits_{i=1}^n\sum\limits_{j=1}^mgc ...

  6. CF961F k-substring

    题意:给你一个字符串(sl<=1e6),问每一个起点到1和终点到sl距离相等的子串的最长不等于串长的border. 标程: #include<cstdio> #include< ...

  7. leetcode-95-不同的二叉搜索树②*

    题目描述: 方法一:递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self. ...

  8. day1-初识Python以及环境搭建

    ---恢复内容开始--- 为什么学习Python? 软件质量:python的可读性很强,易于理解,非常接近于人类的自然语言. 提高开发者效率:相当于C,C++和JAVA等编译/静态型语言,Python ...

  9. 跳表上线性dp——1150D 好题!

    题目设计的很好,感觉做了这题对dp的状态有了更深的理解 /* 先预处理序列自动机 dp[i][j][k]表示匹配到i,j,k时的最靠前的位置 那么现在A串加入了一个字母,我们要求的就是dp[i+1][ ...

  10. php中Cookies

    PHP Cookies cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制,PHP 透明地支持 HTTP cookie. cookie 常用于识别用户. Cookie 是什么? c ...