leetcode--Best Time to Buy and Sell Stock II
来源:互联网 发布:w3cschool php教程 编辑:程序博客网 时间:2024/05/29 04:41
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) { //注意题意,对最大利益的时间限制,只有卖了才能再买,所以只能是前面的元素减去后面的元素。 //注意:同一天可以先卖再买,这一行为导致数列的正差值最大值是后面的大元素减去前面的小元素,然后累加的结果 int i=0; int m_profit=0; int l=prices.size(); for (i;i<l-1;i++) { if (prices[i]<prices[i+1]) m_profit+=prices[i+1]-prices[i];} return m_profit; }};
0 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 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
- LeetCode-Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock II
- Struts2
- windows系统下Python环境的搭建
- hdu 1421 搬寝室
- 对象及其内存管理
- 黑马程序员--集合框架---泛型限定
- leetcode--Best Time to Buy and Sell Stock II
- 10730-Antiarithmetic?【暴力枚举】
- 牛人博客收集
- ios中发邮件功能实现的几种方式
- [离散对数] uva 11916 Emoogle Grid
- 好记性不如烂笔头101-spring3(21)-jdbcTemplate和NamedParameterJdbcTemplate的小差异
- CSS3 Animation
- 黑马程序员 OC基础: 面向对象_继承&多态(三)
- 用JSP写的一个用户登录界面