From 9642d4cba67bedb5e1264fec6be1f247e0f88c9c Mon Sep 17 00:00:00 2001 From: Emar Ali <44610462+emaaarrr@users.noreply.github.com> Date: Fri, 1 Oct 2021 19:50:44 +0530 Subject: [PATCH] Create best time to buy new stocks --- Improved best time to buy stocks | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Improved best time to buy stocks diff --git a/Improved best time to buy stocks b/Improved best time to buy stocks new file mode 100644 index 0000000..321841a --- /dev/null +++ b/Improved best time to buy stocks @@ -0,0 +1,15 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int minPrice = INT_MAX; + int maxProf = 0; + for(int i = 0; i < prices.size(); i++) + { + minPrice = min(minPrice, prices[i]); + maxProf = max(maxProf, prices[i] - minPrice); + } + return maxProf; + } +}; + +// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/