Best Time to Buy and Sell Stock

本文介绍了几种关于股票买卖时机选择的算法实现,包括一次交易、多次交易、两次交易的最佳时机及收益计算,并提供了具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值