> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/array/01-getmaxprofit.md).

# # 01 getMaxProfit

先

```javascript
// 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

```javascript
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
}
```
