121.Best Time to Buy and Sell Stock
Greedy Approach: At each step, choose the best immediate option without worrying about future steps.(O(n))
Wiki topics:
💻 · Programming
121.Best Time to Buy and Sell Stock
Greedy Approach: At each step, choose the best immediate option without worrying about future steps.(O(n))
class Solution {
public:
int maxProfit(vector<int>& prices) {
int min=INT_MAX;
int profit=0;
for(int i=0;i<prices.size();i++){
if (prices[i]<min){
min=prices[i];
}
profit=max(profit,prices[i]-min);
}
return profit;
}
}; 메타데이터
- post_id
- 258e670652d6
- slug
- 121-best-time-to-buy-and-sell-stock-258e670652d6
- url
- https://medium.com/@kunaldebnathofficial843/121-best-time-to-buy-and-sell-stock-258e670652d6
- canonical_url
- https://medium.com/@kunaldebnathofficial843/121-best-time-to-buy-and-sell-stock-258e670652d6
- author_url
- https://medium.com/@kunaldebnathofficial843
- status
- ok
- fetched_at
- 2026-07-13 06:23:13