Java very short(6 line) and fast(15 ms) 0(n) time 0(1) space solution


  • 0
    K
    class Solution {
        public int maxProfit(int[] prices, int fee) {
           int buy= Integer.MIN_VALUE,sale=0;
            
            for(int price:prices){
                buy=Math.max(buy,sale-price);
                sale=Math.max(sale,buy+price-fee);
            }
            return sale;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.