Best Time to Buy and Sell Stock II
来源:互联网 发布:郑州php培训学校 编辑:程序博客网 时间:2024/09/21 08:17
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 sum = 0; int j = 0; int n = prices.size(); while(j < n) { while(j+1 < n && prices[j] >= prices[j+1]) j++; if(j < n-1) { sum -= prices[j]; j++; while(j+1 < n && prices[j] <= prices[j+1]) j++; sum += prices[j]; } j++; } return sum; }};
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
- Oracle package 定义和java调用
- Java线程中interrupt那点事
- Libjingle和各协议的关系
- RedHat Ubuntu查看系统信息命令汇总
- 马航事情由于马来西亚方面还有很多事情有所隐瞒?
- Best Time to Buy and Sell Stock II
- hibernate annotation Resouce
- console.log的坑
- 张孝祥整理的JAVA面试题目(五)(每天一更)
- Android 中动态替换string.xml的值
- 入门篇:yum安装Linux+Apache+PHP+MySQL服务器环境(CentOS篇)
- Libjingle综述
- Ext调用规则返回json数据到页面
- 单片机的几种寻址方式