Advertisement
ogv

Untitled

ogv
Oct 29th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. // Runtime: 5 ms, faster than 82.93% of Java online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
  2. // Memory Usage: 51.7 MB, less than 100.00% of Java online submissions for Best Time to Buy and Sell Stock with Transaction Fee.
  3. class Solution {
  4.     public int maxProfit(int[] prices, int fee) {
  5.         int profit = 0;
  6.         int sell = 0;
  7.        
  8.         for (int i = prices.length - 1; i >= 0; i--) {            
  9.             int profitNew = Math.max(profit, sell - prices[i] - fee);            
  10.             sell = Math.max(sell, prices[i] + profit);
  11.             profit = profitNew;
  12.         }
  13.        
  14.         return profit;
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement