leetcode - Best Time to Buy and Sell Stock III
2013-03-03 21:50
316 查看
题目描述:点击此处
class Solution { public: int maxProfit(vector<int> &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function int profit = 0; vector<int>::iterator ii; int size = prices.size(); if (size ==0 ) return 0; int * leftProfit = new int[size+1]; int *rightProfit = new int[size+1]; int val = 1000000000;; int j=0; for (ii=prices.begin(); ii!=prices.end(); ii++){ if (*ii<val) val = *ii; if (j==0 || *ii-val > leftProfit[j-1]) leftProfit[j] = *ii - val; else leftProfit[j] = leftProfit[j-1]; j++; } j = size-1; val =-1; for (ii=prices.end()-1; ii+1!=prices.begin(); ii--){ if (*ii > val) val = *ii; if (j==size-1 || val -*ii>rightProfit[j+1]) rightProfit[j] = val -*ii; else rightProfit[j] = rightProfit[j+1]; j--; } if (leftProfit[size-1]!=rightProfit[0]) return -1; profit = leftProfit[size-1]; for (j=0; j+1<size; j++){ val = leftProfit[j]+rightProfit[j+1]; if (val>profit) profit = val; } delete [] leftProfit; delete [] rightProfit; return profit; } };
相关文章推荐
- leetcode 123 —— Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock III -- LeetCode
- LeetCode动态规划思想:Best Time to Buy and Sell Stock III
- 【LeetCode】Best Time to Buy and Sell Stock III
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告
- LeetCode | Best Time to Buy and Sell Stock III(股票购买抛售问题III)
- [LeetCODE] Best Time to Buy and Sell Stock I II III IV V
- LeetCode | Best Time to Buy and Sell Stock III
- leetcode_c++:Best Time to Buy and Sell Stock III(123)
- Best Time to Buy and Sell Stock III :from LeetCode
- Best Time to Buy and Sell Stock III ---- LeetCode
- 【leetcode】Best Time to Buy and Sell Stock III
- LeetCode 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
- [LeetCode] 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
- LeetCode OJ Best Time to Buy and Sell Stock I II III IV
- leetcode Best Time to Buy and Sell Stock(I~III)(*)