StockBuyAndSell { public int maxProfitIII(int[] prices) { int maxProfit = 0; if (prices.length > 1) { int[] left = new int[prices.length], right = new int[prices.length]; int min = prices[0]; for (int i = 1; i < prices.length; i++) { if (prices[i] < min) { min = prices[i]; } left[i] = Math.max(left[i - 1], prices[i] - min); } int max = prices[prices.length - 1]; for (int i = prices.length - 2; i >= 0; i--) { if (prices[i] > max) { max = prices[i]; } right[i] = Math.max(right[i + 1], max - prices[i]); } for (int i = 0; i < prices.length; i++) { maxProfit = Math.max(left[i] + right[i], maxProfit); } } return maxProfit; }  int maxProfitI(int[] prices); int maxProfitII(int[] prices); int maxProfitIII(int[] prices); int maxProfitIV(int k, int[] prices); int maxProfitWithCooldown(int[] prices); int maxProfitWithTransactionFee(int[] prices, int fee);  }
@Test(dataProvider = "stocksIII") public void testmaxProfitIII(int[] stocks, int maxProfit) { assertEquals(maxProfit, new StockBuyAndSell().maxProfitIII(stocks)); }