class Solution {
public:
int maxProfit(vector<int> &prices) {
if( prices.size() == 0)
return 0;
int minv = prices[0];
int maxv = 0;
for( int i = 1 ; i < prices.size() ; i++ ) {
maxv = max(maxv,prices[i] - minv);
minv = min(minv,prices[i]);
}
return maxv;
}
};
Arpit's Newsletter read by 15000+ engineers
🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.