From 6956b537c21699a4b0c425bc0dc368fab82fdef5 Mon Sep 17 00:00:00 2001 From: SUJAY GIJRE Date: Sun, 1 Mar 2026 12:04:27 -0500 Subject: [PATCH 1/2] Create BestTimeToBuySellStock.cpp --- BestTimeToBuySellStock.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 BestTimeToBuySellStock.cpp diff --git a/BestTimeToBuySellStock.cpp b/BestTimeToBuySellStock.cpp new file mode 100644 index 0000000..3b4df23 --- /dev/null +++ b/BestTimeToBuySellStock.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int valley = INT_MAX; + int peak = INT_MIN; + int i=0; + int profit = 0; +] + while (i< prices.size()-1) { + while (i= prices[i+1]) + i++; + valley = prices[i]; + while (i Date: Sun, 1 Mar 2026 12:05:01 -0500 Subject: [PATCH 2/2] Create peekingIterator.cpp --- peekingIterator.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 peekingIterator.cpp diff --git a/peekingIterator.cpp b/peekingIterator.cpp new file mode 100644 index 0000000..ed4aace --- /dev/null +++ b/peekingIterator.cpp @@ -0,0 +1,54 @@ +/* + * Below is the interface for Iterator, which is already defined for you. + * **DO NOT** modify the interface for Iterator. + * + * class Iterator { + * struct Data; + * Data* data; + * public: + * Iterator(const vector& nums); + * Iterator(const Iterator& iter); + * + * // Returns the next element in the iteration. + * int next(); + * + * // Returns true if the iteration has more elements. + * bool hasNext() const; + * }; + */ + +class PeekingIterator : public Iterator { +public: + int nextEle; + int nextVal; + PeekingIterator(const vector& nums) : Iterator(nums) { + // Initialize any member here. + // **DO NOT** save a copy of nums and manipulate it directly. + // You should only use the Iterator interface methods. + nextEle = Iterator::hasNext(); + if (nextEle) { + nextVal = Iterator::next(); + } + } + + // Returns the next element in the iteration without advancing the iterator. + int peek() { + return nextVal; + } + + // hasNext() and next() should behave the same as in the Iterator interface. + // Override them if needed. + int next() { + int oldVal = nextVal; + + nextEle = Iterator::hasNext(); + if (nextEle) { + nextVal = Iterator::next(); + } + return oldVal; + } + + bool hasNext() const { + return nextEle; + } +};