diff options
author | crupest <crupest@outlook.com> | 2020-09-27 21:10:44 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-09-27 21:10:44 +0800 |
commit | ca302ffa2b2f0bb7b5d2c2694d4361793d896a18 (patch) | |
tree | 2c5cfb3580a68019021df663ce8c2ac37e34a8dc | |
parent | 92d2a30599a6c7c3b0917026542650fc0d3866eb (diff) | |
download | crupest-ca302ffa2b2f0bb7b5d2c2694d4361793d896a18.tar.gz crupest-ca302ffa2b2f0bb7b5d2c2694d4361793d896a18.tar.bz2 crupest-ca302ffa2b2f0bb7b5d2c2694d4361793d896a18.zip |
import(solutions): Add peoblem 121 .
-rw-r--r-- | works/solutions/cpp/121.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/works/solutions/cpp/121.cpp b/works/solutions/cpp/121.cpp new file mode 100644 index 0000000..cba465d --- /dev/null +++ b/works/solutions/cpp/121.cpp @@ -0,0 +1,24 @@ +#include <vector> + +using std::vector; + +class Solution +{ +public: + int maxProfit(vector<int> &prices) + { + if (prices.size() <= 1) return 0; + + int result = 0; + int min = prices.front(); + for (int i = 1; i < prices.size(); i++) + { + if (prices[i] - min > result) + result = prices[i] - min; + if (prices[i] < min) + min = prices[i]; + } + + return result; + } +}; |