Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 121. Best Time to Buy and Sell Stock

Posted on 2021-02-12 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int maxProfit(vector<int>& prices) {
int min_price = INT_MAX, max_profit = 0;
for (int price : prices) {
max_profit = max(max_profit, price - min_price);
min_price = min(min_price, price);
}
return max_profit;
}
};
# Dynamic Programming# Array
LeetCode 120. Triangle
LeetCode 122. Best Time to Buy and Sell Stock II
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%