diff --git a/buysell2.py b/buysell2.py new file mode 100644 index 0000000..4295c79 --- /dev/null +++ b/buysell2.py @@ -0,0 +1,20 @@ +# Time Complexity : O(N) where N is the length of the prices list +# Space Complexity : O(1) as we are using only a constant amount of extra space +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am iterating through the prices list and checking if the next day's price is higher than the current day's price +# If it is, I calculate the profit by subtracting the current day's price from the next day's price and add it to the total profit +# This way, I am effectively capturing all the upward trends in the stock prices to maximize profit. As we are allowed to make multiple transactions, this approach works well. + +from typing import List +class Solution: + def maxProfit(self, prices: List[int]) -> int: + n = len(prices) + profit = 0 + for i in range(n-1): + if prices[i] < prices[i+1]: + selling = prices[i+1] - prices[i] + profit += selling + return profit diff --git a/peekingiterator.py b/peekingiterator.py new file mode 100644 index 0000000..d22415f --- /dev/null +++ b/peekingiterator.py @@ -0,0 +1,64 @@ +# Time Complexity : O(1) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am maintaining a variable nextElement to store the next element of the iterator +# In the constructor, I initialize nextElement with the first element of the iterator if it exists +# The peek method simply returns the nextElement without advancing the iterator +# The next method returns the current nextElement and then updates it to the next element of the iterator +# The hasNext method checks if nextElement is not None to determine if there are more elements in the iterator + +# 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): + self.iterator = iterator + if iterator.hasNext(): + self.nextElement = iterator.next() + else: + self.nextElement = None + + + + def peek(self): + return self.nextElement + + def next(self): + result = self.nextElement + if self.iterator.hasNext(): + self.nextElement = self.iterator.next() + else: + self.nextElement = None + return result + + def hasNext(self): + print(self.nextElement) + return self.nextElement != None + + +# 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]. \ No newline at end of file