LeetCode Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
题意:做一次买入卖出的最大收益。思路:一次线性扫描解决。
public class Solution { public int maxProfit(int[] prices) { if (prices.length == 0) return 0; int low = prices[0]; int ans = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] < low) low = prices[i]; else if (ans < prices[i] - low) ans = prices[i] - low; } return ans; } }
Best Time to Buy and Sell Stock II
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 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).
题意:能够不断买入卖出的最大收益。思路:按理说找出每段上升序列的头尾差。可是能够发现事实上头尾差就是每相邻的差(保证后一位较大)。
public class Solution { public int maxProfit(int[] prices) { if (prices.length == 0) return 0; int ans = 0; for (int i = 0; i < prices.length - 1; i++) { if (prices[i+1] > prices[i]) ans += prices[i+1] - prices[i]; } return ans; } }
Best Time to Buy and Sell Stock III
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).
思路:注意卖出和买入能够同一时刻,两次就意味着把序列切成了两部分,那么一次从前遍历运行第一题的操作,再从后来一次,求和的最大值。
public class Solution { public int maxProfit(int[] prices) { if (prices.length == 0) return 0; final int len = prices.length; int low = prices[0], Max = 0; int[] profit = new int[len]; profit[0] = 0; for (int i = 1; i < len; i++) { low = Math.min(low, prices[i]); if (Max < prices[i] - low) Max = prices[i] - low; profit[i] = Max; } int tmpMax = prices[len-1]; int ans = profit[len-1]; Max = 0; for (int i = len-2; i >= 0; i--) { tmpMax = Math.max(tmpMax, prices[i]); if (Max < tmpMax - prices[i]) Max = tmpMax - prices[i]; if (ans < profit[i] + Max) ans = profit[i] + Max; } return ans; } }
相关文章
- Leetcode 之Flatten Binary Tree to Linked List(50)
- Java实现 LeetCode 837 新21点(DP)
- Java实现 LeetCode 824 山羊拉丁文(暴力)
- Java实现 LeetCode 622 设计循环队列(暴力大法)
- Java实现 LeetCode 599 两个列表的最小索引总和(使用hash提高效率)
- Java实现 LeetCode 488 祖玛游戏
- 【LeetCode算法-13】Roman to Integer
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
- [LeetCode] Best Time to Buy and Sell Stock III
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-1694. 重新格式化电话号码【字符串,分块】
- 【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
- [LeetCode] 33. 搜索旋转排序数组 ☆☆☆(二分查找)
- [LeetCode] Flatten Binary Tree to Linked List
- LeetCode: Sum Root to Leaf Numbers [129]
- LeetCode之Sort List
- Leetcode:best_time_to_buy_and_sell_stock_II题解
- PAT 1033 To Fill or Not to Fill[dp]
- leetcode 849. Maximize Distance to Closest Person
- leetcode 405. Convert a Number to Hexadecimal
- leetcode 108. Convert Sorted Array to Binary Search Tree
- leetcode 453. Minimum Moves to Equal Array Elements