Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
7oSkaaa authored Jun 22, 2023
2 parents 2b3da94 + 418b6aa commit 3854107
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/data/problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@
{
"day": "21",
"title": "Minimum Cost to Make Array Equal"
},
{
"day": "22",
"title": "Best Time to Buy and Sell Stock with Transaction Fee"
}
]
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Author: Ahmed Hossam

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
// Get the number of prices in the vector.
int n = prices.size();

// Initialize variables for cash (available funds) and hold (stock held).
// Set the initial value of hold to the negative of the first stock price.
int cash = 0, hold = -prices[0];

// Iterate through the prices starting from the second price.
for(int i = 1; i < n; i++){
// Calculate the maximum of either keeping the cash as it is or selling the stock and deducting the fee.
cash = max(cash, hold + prices[i] - fee);

// Calculate the maximum of either keeping the hold as it is or buying the stock and deducting the cash.
hold = max(hold, cash - prices[i]);
}

// Return the maximum profit (cash) after all transactions.
return cash;
}
};
43 changes: 43 additions & 0 deletions 06- June/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
1. **[Find the Highest Altitude](#19--find-the-highest-altitude)**
1. **[K Radius Subarray Averages](#20--k-radius-subarray-averages)**
1. **[Minimum Cost to Make Array Equal](#21--minimum-cost-to-make-array-equal)**
1. **[Best Time to Buy and Sell Stock with Transaction Fee](#22--best-time-to-buy-and-sell-stock-with-transaction-fee)**

<hr>
<br><br>
Expand Down Expand Up @@ -1255,4 +1256,46 @@ public:
}
};
```

<hr>
<br><br>

## 22) [Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)

### Difficulty

![](https://img.shields.io/badge/Medium-orange?style=for-the-badge)

### Related Topic

`Array` `Dynamic Programming` `Greedy`

### Code


```cpp
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
// Get the number of prices in the vector.
int n = prices.size();

// Initialize variables for cash (available funds) and hold (stock held).
// Set the initial value of hold to the negative of the first stock price.
int cash = 0, hold = -prices[0];

// Iterate through the prices starting from the second price.
for(int i = 1; i < n; i++){
// Calculate the maximum of either keeping the cash as it is or selling the stock and deducting the fee.
cash = max(cash, hold + prices[i] - fee);

// Calculate the maximum of either keeping the hold as it is or buying the stock and deducting the cash.
hold = max(hold, cash - prices[i]);
}

// Return the maximum profit (cash) after all transactions.
return cash;
}
};
```

0 comments on commit 3854107

Please sign in to comment.