From 3f8b4c95a6132b656ed6956835bd888d799164ed Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 28 Oct 2025 18:42:57 -0500 Subject: [PATCH] Competitive-Coding-10 completed --- buy_sell_stock_II.py | 11 +++++++++++ peeking_iterator.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 buy_sell_stock_II.py create mode 100644 peeking_iterator.py diff --git a/buy_sell_stock_II.py b/buy_sell_stock_II.py new file mode 100644 index 0000000..5ce6f61 --- /dev/null +++ b/buy_sell_stock_II.py @@ -0,0 +1,11 @@ + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + max_profit=0 + for i in range(1,len(prices)): + if prices[i]>prices[i-1]: + max_profit+=prices[i]-prices[i-1] + return max_profit + + + diff --git a/peeking_iterator.py b/peeking_iterator.py new file mode 100644 index 0000000..415c1d9 --- /dev/null +++ b/peeking_iterator.py @@ -0,0 +1,32 @@ +class PeekingIterator: + def __init__(self, iterator): + """ + Initialize your data structure here. + :type iterator: Iterator + """ + self.iterator = iterator + self.next_val = iterator.next() if iterator.hasNext() else None + + """ + Returns the next element in the iteration without advancing the iterator. + :rtype: int + """ + + def peek(self): + return self.next_val + + def next(self): + """ + :rtype: int + """ + cur = self.next_val + self.next_val = self.iterator.next() if self.iterator.hasNext() else None + return cur + + def hasNext(self): + """ + :rtype: bool + """ + return self.next_val is not None + + \ No newline at end of file