[LeetCode]122 Best Time to Buy and Sell Stock II
2015-01-06 17:50
447 查看
https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
http://blog.csdn.net/linhuanmars/article/details/23164149
http://blog.csdn.net/linhuanmars/article/details/23164149
public class Solution { public int maxProfit(int[] prices) { // If as many transactions // Gready int pro = 0; int buy = 0; boolean hold = false; int len = prices.length; for (int i = 0 ; i < len ; i ++) { if (!hold) { // Find the next buy day if (i + 1 < len && prices[i + 1] > prices[i]) { buy = prices[i]; hold = true; } } else { // Find the next sell day if ((i == len - 1) || // Last day, sell whatever we have (i + 1 < len && prices[i + 1] < prices[i])) // A good sell day. { pro += prices[i] - buy; hold = false; } } } return pro; } }
相关文章推荐
- LeetCode(122) Best Time to Buy and Sell Stock II (Java)
- 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, Medium] 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]
- 122 Best Time to Buy and Sell Stock II [Leetcode]
- leetcode 122: Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- Leetcode#122 Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- [Leetcode 51] 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_question_122 Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- [LeetCode 122] - 买入与卖出股票的最佳时机II(Best Time to Buy and Sell Stock II)