LeetCode 123: Best Time to Buy and Sell Stock III
来源:互联网 发布:mac os 10.9 dmg镜像 编辑:程序博客网 时间:2024/05/19 19:31
Difficulty: 4
Frequency: 2
Problem:
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 at most two transactions.
Note:
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) { // Start typing your C/C++ solution below // DO NOT write int main() function if (prices.size() < 2) return 0; int * from_begin = new int[prices.size()]; int * from_end = new int[prices.size()]; int i_max_profit = 0, i_min = 0, i_max = 0; from_begin[0] = 0; from_end[prices.size() - 1] = 0; for (int i = 1; i<prices.size(); ++i) { if (prices[i]>prices[i_max]) { i_max = i; i_max_profit = max(i_max_profit, (prices[i_max] - prices[i_min])); } else if(prices[i]<prices[i_min]) { i_max = i_min = i; } from_begin[i] = i_max_profit; } i_max_profit = 0, i_min = i_max = prices.size() - 1; for (int i = prices.size() - 2; i>=0; --i) { if (prices[i]<prices[i_min]) { i_min = i; i_max_profit = max(i_max_profit, (prices[i_max] - prices[i_min])); } else if(prices[i]>prices[i_max]) { i_max = i_min = i; } from_end[i] = i_max_profit; } i_max_profit = 0; for (int i = prices.size() - 1; i>=0; --i) { i_max_profit = max(i_max_profit, (from_end[i] + from_begin[i]));//See Notes. This is important. } delete [] from_begin; delete [] from_end; return i_max_profit; }};
Notes:
Originally, I wrote max(i_max_profit, from_end[i] + from_begin[i-1]). This has two errors. First is i-1 should be i. Because we can sell it and then buy it in one day. The second is if without a() outside from_end[i] + from_begin[i-1] , it will cause runtime error. I guess it is because max is define as a macro using ?: operator, it has a low priority.
- LeetCode 123: Best Time to Buy and Sell Stock III
- LeetCode: Best Time to Buy and Sell Stock III [123]
- LeetCode 123 Best Time to Buy and Sell Stock III
- [leetcode 123] Best Time to Buy and Sell Stock III
- leetcode || 123、Best Time to Buy and Sell Stock III
- leetcode-123 Best Time to Buy and Sell Stock III
- leetcode 123: Best Time to Buy and Sell Stock III
- 123 Best Time to Buy and Sell Stock III [Leetcode]
- Best Time to Buy and Sell Stock III - LeetCode 123
- LeetCode 123: Best Time to Buy and Sell Stock III
- LeetCode 123 Best Time to Buy and Sell Stock III
- LeetCode 123 -- Best Time to Buy and Sell Stock III
- Leetcode 123 Best Time to Buy and Sell Stock III
- LeetCode 123 Best Time to Buy and Sell Stock III
- Leetcode Best Time to Buy and Sell Stock III 123
- [Leetcode 123] Best Time to Buy and Sell Stock III
- **Leetcode 123 Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III
- Linux下去掉^M的方法
- 一些资源
- HDU 4353 Finding Mine(做法经典 三角形内部点/面积)
- 在linux环境下安装hadoop环境和配置
- MySQL新建用户,授权,删除用户,修改密码
- LeetCode 123: Best Time to Buy and Sell Stock III
- 反思大数据:超越技术与思维,数据本身才是真正入口(虎嗅网)
- standby 与监听器
- html中DIV与SPAN标签的区别
- 表压缩前后所占用的BLOCK情况的对比总结
- activity的跳转
- HDOJ 4712 - Hamming Distance 猥琐随机化...
- hdu3722 Card Game (KM)
- string类的实现