码迷,mamicode.com
首页 > 其他好文 > 详细

Best Time to Buy and Sell Stock

时间:2014-11-30 23:19:36      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:leetcode   动态规划   股票收益最大   java   

问题一 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

标签:leetcode   动态规划   股票收益最大   java   

原文地址:http://blog.csdn.net/lgcssx/article/details/41629439

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!