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& 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; + } +};