-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmaxProfit.cpp
More file actions
66 lines (51 loc) · 1.92 KB
/
maxProfit.cpp
File metadata and controls
66 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
/*
121. Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
*/
class Solution {
public:
int maxProfit(std::vector<int>& prices) {
int maxPro{0}, mn{INT_MAX};
for(size_t i{0};i<prices.size();i++)
{
mn=std::min(mn,prices[i]);
maxPro=std::max(maxPro,prices[i]-mn);
}
return maxPro;
}
int maxProfit_v2(std::vector<int>& prices)
{
int currentMax {0}, max{0};
for(int i=0; i<prices.size()-1; ++i)
{
int diff = prices[i+1] - prices[i];
if(diff == 0) continue;
// if previous profit is less than the current one (negative profit = loss)
// then it should be discarded
// that is why maximum of (diff+currentMax) and diff
// is checked here
// For instance 4 6 1 2 3 5 in this array after 6 the
// algorithm should be restarted because max diff is 5 -1 = 4.
// so max(-5+3 , 2-1) = 1, so calculation will start from 1.
currentMax = std::max(diff+currentMax, diff);
max = std::max(currentMax, max);
}
return max;
}
};
int main()
{
std::vector<int> prices {7,1,5,3,6,4};
std::cout<<Solution().maxProfit(prices);
std::cout<<std::endl; // 5
std::vector<int> prices2 {1,2};
std::cout<<Solution().maxProfit(prices2); // 1
std::cout<<std::endl;
std::cout<<Solution().maxProfit_v2(prices); // 5
}