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 | 8886eddb2b4567eab0677ca08a0ab3e65e059f2a (patch) | |
tree | 184b03d30031234da06e3942958f6781446989ae | |
parent | 6c32bedf2796f3eb4cef9cc9d4b3c753a4391ea9 (diff) | |
download | solutions-8886eddb2b4567eab0677ca08a0ab3e65e059f2a.tar.gz solutions-8886eddb2b4567eab0677ca08a0ab3e65e059f2a.tar.bz2 solutions-8886eddb2b4567eab0677ca08a0ab3e65e059f2a.zip |
Add peoblem 121 .
-rw-r--r-- | cpp/121.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/cpp/121.cpp b/cpp/121.cpp new file mode 100644 index 0000000..cba465d --- /dev/null +++ b/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; + } +}; |