首页 > 代码库 > [LeetCode]122 Best Time to Buy and Sell Stock II
[LeetCode]122 Best Time to Buy and Sell Stock II
https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。