Best Time to Buy and Sell Stock I
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 == null || prices.length < 2) { return 0; } int max = 0; int min = prices[0]; for (int i = 1; i < prices.length; i++) { min = Math.min(min, prices[i]); max = Math.max(max, prices[i] - min); } return max; } }
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 == null || prices.length == 0) { return 0; } int sum = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i - 1]) { sum += prices[i] - prices[i - 1]; } } return sum; } }
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).
解决思路
技巧:前后分别扫一遍,从前往后扫记录下以当前节点卖出的最大收益(方法如问题I),从后往前记录以当前节点卖出之后,后面再交易一次能够得到的最大收益。
最后同时扫描这两个数组,然后记录下两个数组的之和的最大值。
时间复杂度为O(n).
程序
public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length < 2) { return 0; } int len = prices.length; int[] forward = new int[len]; int[] backward = new int[len]; // cal backward int min = prices[0]; for (int i = 1; i < len; i++) { min = Math.min(min, prices[i]); backward[i] = prices[i] - min; } // cal forward int max = prices[len - 1]; for (int i = len - 2; i >= 0; i--) { max = Math.max(max, prices[i + 1]); forward[i] = Math.max(max - prices[i + 1], forward[i + 1]); } int maxProfit = 0; for (int i = 0; i < len; i++) { maxProfit = Math.max(maxProfit, backward[i] + forward[i]); } return maxProfit; } }
Best Time to Buy and Sell Stock IV
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 k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
解决思路
1. dfs方法 + dp优化
二维数组dp[i, j]代表在区间[i, j]中能够获得的最大收益。
程序
public class Solution { private int max = 0; public int maxProfit(int k, int[] prices) { if (prices == null || prices.length < 2 || k <= 0) { return 0; } if (k > prices.length / 2) { k = prices.length / 2; } int len = prices.length; int[][] maxOneSellDp = getDpOptimize(prices); helper(prices, 0, len, k, 0, maxOneSellDp); return max; } private int[][] getDpOptimize(int[] prices) { int n = prices.length; int[][] dp = new int[n][n]; for (int i = 0; i < n - 1; i++) { int min = prices[i]; for (int j = i + 1; j < n; j++) { min = Math.min(min, prices[j]); dp[i][j] = prices[j] - min; } } return dp; } private void helper(int[] prices, int start, int end, int k, int profit, int[][] maxOneSellDp) { if (start > end || k < 0) { return; } if (profit > max) { max = profit; } for (int i = start + 1; i < end; i++) { // int maxOneSell = getMaxOneSell(prices, start, i); // dp optimize int maxOneSell = maxOneSellDp[start][i]; helper(prices, i + 1, end, k - 1, profit + maxOneSell, maxOneSellDp); } } }
第一种方法TLE。