Skip to content

Commit 49c97fa

Browse files
authored
Merge pull request #167 from Umesh7Dixit/hack1
Adding cpp program
2 parents 79a53c3 + 5458afb commit 49c97fa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
class Solution {
6+
public:
7+
int maxProfit(std::vector<int>& prices) {
8+
int maxProfit = 0;
9+
int mini = prices[0];
10+
11+
for(int i = 1; i < prices.size(); i++) {
12+
int curProfit = prices[i] - mini;
13+
maxProfit = std::max(maxProfit, curProfit);
14+
mini = std::min(mini, prices[i]);
15+
}
16+
17+
return maxProfit;
18+
}
19+
};
20+
21+
int main() {
22+
// Example usage of the Solution class
23+
std::vector<int> prices = {7, 1, 5, 3, 6, 4};
24+
Solution solution;
25+
int maxProfit = solution.maxProfit(prices);
26+
27+
std::cout << "Maximum profit: " << maxProfit << std::endl;
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)