Best Time to Buy and Sell Stock
来源:互联网 发布:淘宝店代购口红靠谱 编辑:程序博客网 时间:2024/06/03 19:06
问题一 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]的最大profitint[] right = new int[prices.length];// 记录了prices[i..n]的最大profitint 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;}
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- 那些平时可能用不上,跳槽很有用的框架
- 深度优先搜索、广度优先搜索及其生成树
- 心花路放
- GridControl详解 分组排序汇总
- Flex和Java通信报错
- Best Time to Buy and Sell Stock
- C++矩阵优化算法
- 【.NET】EF框架之三种模式
- ImageView 动态设置大小
- Silverlight和浏览器交互介绍
- eclipse maven web项目与web项目的区别
- 设计模式(十六)之Command
- java IO相关API探索之 AutoCloseable接口
- jsp自定义分页标签