From 185ef9fcb0e59f13e9ee0ccb261693cdaddebab0 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 23 Feb 2021 21:07:19 +0800 Subject: import(solutions): Move leetcode solutions to subdir. --- works/solutions/leetcode/cpp/121.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 works/solutions/leetcode/cpp/121.cpp (limited to 'works/solutions/leetcode/cpp/121.cpp') diff --git a/works/solutions/leetcode/cpp/121.cpp b/works/solutions/leetcode/cpp/121.cpp new file mode 100644 index 0000000..8561fa3 --- /dev/null +++ b/works/solutions/leetcode/cpp/121.cpp @@ -0,0 +1,24 @@ +#include + +using std::vector; + +class Solution +{ +public: + int maxProfit(vector &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; + } +}; -- cgit v1.2.3