Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions buyAndSellStock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TC: O(n)
# SC: O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit=0
n=len(prices)
for i in range(n-1):
if prices[i]<prices[i+1]:
profit+=prices[i+1]-prices[i]

return profit
64 changes: 64 additions & 0 deletions peekingIterator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Below is the interface for Iterator, which is already defined for you.
#
# class Iterator:
# def __init__(self, nums):
# """
# Initializes an iterator object to the beginning of a list.
# :type nums: List[int]
# """
#
# def hasNext(self):
# """
# Returns true if the iteration has more elements.
# :rtype: bool
# """
#
# def next(self):
# """
# Returns the next element in the iteration.
# :rtype: int
# """

class PeekingIterator:
def __init__(self, iterator):
"""
Initialize your data structure here.
:type iterator: Iterator
"""
self.iterator=iterator
self.nextEle=self.iterator.next() if self.iterator.hasNext() else None

def advance(self):
self.nextEle=self.iterator.next() if self.iterator.hasNext() else None

def peek(self):
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
return self.nextEle


def next(self):
"""
:rtype: int
"""
temp=self.nextEle
self.advance()
return temp


def hasNext(self):
"""
:rtype: bool
"""
if self.nextEle:
return True
return False


# Your PeekingIterator object will be instantiated and called as such:
# iter = PeekingIterator(Iterator(nums))
# while iter.hasNext():
# val = iter.peek() # Get the next element but not advance the iterator.
# iter.next() # Should return the same value as [val].