import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。
* 如果你最多隻允許完成一筆交易(即買入和賣出一支股票一次),設計一個算法來計算你所能獲取的最大利潤。
* 注意:你不能在買入股票前賣出股票。
* 示例 1:
* <p>
* 輸入: [7,1,5,3,6,4]
* 輸出: 5
* 解釋: 在第 2 天(股票價格 = 1)的時候買入,在第 5 天(股票價格 = 6)的時候賣出,最大利潤 = 6-1 = 5 。
* 注意利潤不能是 7-1 = 6, 因爲賣出價格需要大於買入價格。
* <p>
* 示例 2:
* <p>
* 輸入: [7,6,4,3,1]
* 輸出: 0
* 解釋: 在這種情況下, 沒有交易完成, 所以最大利潤爲 0。
* <p>
* 來源:力扣(LeetCode)
* 鏈接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock
* 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
*/
public class Solution {
/**
* @param prices
* @return
*/
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice)
minprice = prices[i];
else if (prices[i] - minprice > maxprofit)
maxprofit = prices[i] - minprice;
}
return maxprofit;
}
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for (int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] prices = stringToIntegerArray(line);
int ret = new Solution().maxProfit(prices);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}
解題思路:
最低點買入,最高點賣出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 給定一個數組,它的第 i 個元素是一支給定的股票在第 i 天的價格。
* 設計一個算法來計算你所能獲取的最大利潤。你最多可以完成 兩筆 交易。
* 注意: 你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。
* 示例 1:
* 輸入: [3,3,5,0,0,3,1,4]
* 輸出: 6
* 解釋: 在第 4 天(股票價格 = 0)的時候買入,在第 6 天(股票價格 = 3)的時候賣出,這筆交易所能獲得利潤 = 3-0 = 3 。
* 隨後,在第 7 天(股票價格 = 1)的時候買入,在第 8 天 (股票價格 = 4)的時候賣出,這筆交易所能獲得利潤 = 4-1 = 3 。
* 示例 2:
* 輸入: [1,2,3,4,5]
* 輸出: 4
* 解釋: 在第 1 天(股票價格 = 1)的時候買入,在第 5 天 (股票價格 = 5)的時候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
* 注意你不能在第 1 天和第 2 天接連購買股票,之後再將它們賣出。
* 因爲這樣屬於同時參與了多筆交易,你必須在再次購買前出售掉之前的股票。
* 示例 3:
* 輸入: [7,6,4,3,1]
* 輸出: 0
* 解釋: 在這個情況下, 沒有交易完成, 所以最大利潤爲 0
* 來源:力扣(LeetCode)
* 鏈接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii
* 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
*/
public class Solution {
/**
* @param prices 輸入
* @return 最大的值
*/
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int maxprofit = Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++) {
maxprofit = Math.max(maxprofit, maxProfit(prices, 0, i) + maxProfit(prices, i + 1, prices.length));
}
return maxprofit;
}
/**
* 區間內最大獲益
*
* @param prices 輸入
* @param start 起始值
* @param end 結束值
* @return 結果
*/
private int maxProfit(int[] prices, int start, int end) {
int min = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = start; i <= end && i < prices.length; i++) {
int currentProfit = prices[i];
if (currentProfit < min) {
min = currentProfit;
} else if (currentProfit - min > maxProfit) {
maxProfit = currentProfit - min;
}
}
return maxProfit;
}
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for (int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] prices = stringToIntegerArray(line);
int ret = new Solution().maxProfit(prices);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}
解題思路
窮舉