Simple Java solution, O(n) time O(1) space


  • 0
    O
        public int maxProfit(int[] prices, int fee) {
            int nohold = 0, hold = -50000, _nohold=0; 
            for (int i=0; i<prices.length; i++){
                _nohold = Math.max(nohold, hold+prices[i]-fee); 
                hold = Math.max(hold, nohold-prices[i]);
                nohold = _nohold; 
            }
            return Math.max(nohold, hold); 
        }
    

Log in to reply
 

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