LeetCode - Best Time to Buy and Sell Stock II
来源:互联网 发布:sql server数据库教程 编辑:程序博客网 时间:2024/06/05 04:20
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) { if(prices.empty()||prices.size()<=1){ return 0;}int max=0,lowest=prices[0],highest=prices[0],flag=0;//0:待买入,1:待卖出for(int i=1;i<prices.size();i++){if(flag==0){//待买入状态if(prices[i]>lowest){flag=1;highest=prices[i];}else{lowest=prices[i];}}else if(flag==1){//待卖出状态if(prices[i]<highest){flag=0;max+=highest-lowest;lowest=prices[i];highest=prices[i];}else{highest=prices[i];}}}if(flag){max+=highest-lowest;}return max; }};
- [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
- CF#197
- SQLite使用总结3_好多约束Constraints
- vim - 系统剪切板
- JAVA数据库连接池
- SQLite使用总结4_增删改查
- LeetCode - Best Time to Buy and Sell Stock II
- maven 打包
- Remove Norman Security Suite with WindowsUninstaller.Org Removal Tips
- MYSQL中EXPLAIN的说明
- js一些方法的兼容性问题
- poj 2406 poj 1961 个人对吉大KMP模板的理解 KMP 基础题--找周期串
- Python网络页面抓取和页面分析
- 链表面试题
- 黑马程序员-String类的总结和用法