[Leetcode 122, Medium] Best Time to Buy and Sell Stock II
来源:互联网 发布:端口的作用是什么 编辑:程序博客网 时间:2024/05/15 01:09
Problem:
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).
Analysis:
Solutions:
C++:
int maxProfit(vector<int>& prices) { int maxValue = 0; vector<int> v_profits; v_profits.push_back(0); int max_profit = 0; for(int i = 1; i < prices.size(); ++i) v_profits.push_back(prices[i] - prices[i - 1]); for(int i = 0; i < v_profits.size(); ++i) max_profit += (v_profits[i] > 0 ? v_profits[i] : 0); return max_profit; }Java:
Python:
0 0
- [Leetcode 122, Medium] Best Time to Buy and Sell Stock II
- [Medium]Best Time to Buy and Sell Stock II
- LeetCode 122: Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- LeetCode: Best Time to Buy and Sell Stock II [122]
- [leetcode 122] Best Time to Buy and Sell Stock II
- leetcode-122 Best Time to Buy and Sell Stock II
- leetcode || 122、Best Time to Buy and Sell Stock II
- LeetCode 122: Best Time to Buy and Sell Stock II
- Leetcode# 122 Best Time to Buy and Sell Stock II
- leetcode 122: Best Time to Buy and Sell Stock II
- leetCode #122 Best Time to Buy and Sell Stock II
- leetcode: (122) Best Time to Buy and Sell Stock II
- 122 Best Time to Buy and Sell Stock II [Leetcode]
- LeetCode 122: Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- 【LEETCODE】122-Best Time to Buy and Sell Stock II
- Swift开发必备技巧:static和class的使用
- 简单、优美的Dialog
- 简析Swift和C的交互
- Objective-C学习笔记(十八)——对象方法和类方法的声明,定义及调用
- 快速排序算法
- [Leetcode 122, Medium] Best Time to Buy and Sell Stock II
- 智能语言方向持续更新开更
- Objective-C对象与Core Foundation对象
- LeetCode #20 Valid Parentheses
- 如何修改mysql配置中的字符集
- Best time to buy and sell stocks IV
- 乒乓球教程
- 斯坦福《机器学习》Lesson5感想———1、成学习算法
- [Leetcode 123, Hard] Best Time to Buy and Sell Stock III