Best Time to Buy and Sell Stock II
来源:互联网 发布:淘宝卖家必备工具 编辑:程序博客网 时间:2024/06/02 02:05
Say you have an array for which the ith element is the price of a given stock on dayi.
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).
Solution:
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); if(len == 0) return 0; prices.push_back(-1); int index = 0, flag = 0, maxprofit = 0; for(int i = 1; i <= len; ++i) { if(prices[i] < prices[i-1]) { if(flag) { maxprofit += prices[i-1] - prices[index]; flag = 0; } index = i; } else flag = 1; } return maxprofit; }};
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
- 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
- Best Time to Buy and Sell Stock II
- 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
- Best Time to Buy and Sell Stock II
- 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
- 2015 Multi-University Training Contest 4(hdu5334 - Virtual Participation)数学
- CSS基础-14CSS盒子模型-内边距
- 优化listview的方法
- 鸟哥的私房菜学习笔记九 关于文件。。。。。
- 【独立开发者er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
- Best Time to Buy and Sell Stock II
- [二分]poj2976 Dropping tests
- 【剑指Offer面试题】 九度OJ1367:二叉搜索树的后序遍历序列
- hdu网格中有多少个矩形【模拟】
- 编程之美:第四章 数字之趣 4.11扫雷游戏的概率
- NYOJ 311 完全背包
- java 左右移位运算
- Aircrack-ng 简单的WEP加密破解(上)
- centos中文乱码