[面试]给你一串股票价格,找出买点和卖点,使得利润最大。(Amazon 面试题)

Give array of integers representing historic stock prices. Find the buying and selling points to maximize the profit.


		int[] stocks = { 2, 3, 15, 23, 45, 1, 32, 42, 21, 45, 23 };
		int min = stocks[0];
		int max = stocks[1] - min;
		for (int i = 2; i < stocks.length; i++) {
			if (stocks[i] - min > max) {
				max = stocks[i] - min;
			}
			if (stocks[i] < min) {
				min = stocks[i];
			}
		}
		System.out.println(max);


你可能感兴趣的:(面试)