# 01 getMaxProfit

ε…ˆ

// You are given an array prices where prices[i] is the price of a given stock on the ith day,
// and an integer fee representing a transaction fee.
// Find the maximum profit you can achieve. You may complete as many transactions as you like,
// but you need to pay the transaction fee for each transaction.

// Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

vv

function getMaxProfit(prices, fee) {
  if(prices.length < 1) return 0
  
  let maxProfit = 0;
  let profitList = []
  for(let i=1; i<prices.length; i++){
    profitList.push(prices[i] - prices[i-1])
  }
  
  profitList.forEach((item, i) => {
    if(item>-fee){
      maxProfit += item 
     
    } else {
       maxProfit -= fee
    }
    
  })
   if(profitList[profitList.length-1]>-fee){
      maxProfit -= fee
    }
  
  return maxProfit
}

Last updated