Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Refer: 1)
- * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108871/2-solutions-2-states-DP-solutions-clear-explanation!
- * 2)
- * https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems
- *
- * At day i, we may buy stock (from previous sell status) or do nothing (from
- * previous buy status): buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]);
- *
- * OR
- *
- * At day i, we may sell stock (from previous buy status) or keep holding (from
- * previous sell status): sell[i] = Math.max(sell[i - 1], buy[i - 1] +
- * prices[i]);
- *
- * Time Complexity: O(N)
- *
- * Space Complexity: O(1)
- *
- * N = length of input prices array.
- */
- class Solution {
- public int maxProfit(int[] prices, int fee) {
- if (prices == null || prices.length <= 1) {
- return 0;
- }
- int buy = -prices[0];
- int sell = 0;
- for (int price : prices) {
- int prevSell = sell;
- sell = Math.max(sell, buy + price - fee);
- buy = Math.max(buy, prevSell - price);
- }
- return sell;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement