leetcode笔记:Best Time to Buy and Sell Stock II
来源:互联网 发布:备份软件 编辑:程序博客网 时间:2024/05/22 10:45
一. 题目描述
Say you have an array for which the i-th 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).
二. 题目分析
该题是买卖股票的第二题。题目给出的基本信息没什么变化,与第一题不同的是的,允许进行任意次数的买卖,但必须保证买卖一次后才能进行第二次操作。继续分析会发现,这实际上是求股价数组所有上升段的和。
三. 示例代码
#include <iostream>#include <vector>using namespace std;class Solution {public: int maxProfit(vector<int> &prices) { int result = 0; int i = 0; int len = prices.size(); if(len <= 1) return 0; for(i = 1; i < len; ++i) { if (prices[i] > prices[i - 1]) result += prices[i]-prices[i - 1]; } return result; }};
四. 小结
与该题相关的题目还有好几道。后续更新…
2 0
- 【LeetCode笔记】Best Time to Buy and Sell Stock II
- leetcode笔记:Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- 深入浅出: Java回调机制(异步)
- Android官方屏幕适配
- The resource could not be loaded because the App Transport Security policy requires the use of a sec
- 短信套餐
- 图形打印
- leetcode笔记:Best Time to Buy and Sell Stock II
- view添加手势与tableView冲突问题
- windos 2008 IIS7 关于mp3、mp4 无法播放的问题 -已解决
- jQuery幻灯片插件Owl Carousel之itemsDesktop的使用
- Android更新UI的两种方法
- 关于mysql数据库以及sql语句的优化技术
- mysql 语句优化
- 基础Shader
- JavaScript中的变量及数据类型