1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class SharesLeetcode714 {
static int maxProfit(int[] prices, int fee) { int b1 = -prices[0]; int s1 = 0; for (int i = 1; i < prices.length; i++) { int s0 = Math.max(s1, b1 + prices[i] - fee); int b0 = Math.max(b1, s1 - prices[i]); s1 = s0; b1 = b0; } return s1; }
public static void main(String[] args) { System.out.println(maxProfit(new int[]{1, 3, 2, 8, 4, 9}, 2)); System.out.println(maxProfit(new int[]{1, 3, 7, 2, 18, 3}, 3));
System.out.println(maxProfit(new int[]{1, 3, 7, 5, 10, 3}, 3)); System.out.println(maxProfit(new int[]{1, 3, 7, 5, 10, 11, 3}, 3)); System.out.println(maxProfit(new int[]{2,1,4,4,2,3,2,5,1,2}, 1)); } }
|