首页 > 代码库 > Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock
问题一 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 int maxProfit(int[] prices) { assert prices.length >= 0; int min = Integer.MAX_VALUE; int max = 0;// 寻找最小的,一次保存最大的 for (int p : prices) { min = Math.min(min, p); max = Math.max(max, p - 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 int maxProfit(int[] prices) { assert prices.length >= 0; int local = 0; int max = 0; for (int i = 1; i < prices.length; i++) { local = prices[i] - prices[i - 1]; int x = local > 0 ? local : 0; max += x; } return max; }
问题三 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).
最多交易两次的最大收益。
int[] left = new int[prices.length];// 记录了prices[0..i]的最大profit
int[] right = new int[prices.length];// 记录了prices[i..n]的最大profit
最后依次加起来求出最大的收益
public int maxProfit(int[] prices) { if (prices.length < 2) return 0; int[] left = new int[prices.length];// 记录了prices[0..i]的最大profit int[] right = new int[prices.length];// 记录了prices[i..n]的最大profit int left_min = prices[0]; for (int i = 1; i < prices.length; i++) { left_min = Math.min(left_min, prices[i]); left[i] = Math.max(prices[i] - left_min, left[i - 1]); } int right_max = prices[prices.length - 1]; for (int i = prices.length - 2; i >= 0; i--) { right_max = Math.max(right_max, prices[i]); right[i] = Math.max(right_max - prices[i], right[i + 1]); } int max = 0; for (int i = 1; i < prices.length; i++) { max = Math.max(max, left[i] + right[i]); } return max; }
问题四 求出一次股票买卖收益最大时买入和卖出的日期即数组的index(实际是问题一的变形)
这时可以定义一个2位数组,用来保存买入和卖出的index,当全局的收益有变化时,更新即可。
public int[] maxProfits(int[] prices) { assert prices.length >= 0; int min = Integer.MAX_VALUE; int[] res = new int[2];// 最终保存的最大 int[] temp = new int[2];// index的临时的记录 int max = 0; for (int i = 0; i < prices.length; i++) { if (min > prices[i]) { min = prices[i]; temp[0] = i; } if (max < prices[i] - min) { temp[1] = i; max = prices[i] - min; res = Arrays.copyOf(temp, 2); } } System.out.println(res[0] + "----" + res[1]); return res; }
Best Time to Buy and Sell Stock
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。