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
20 changes: 20 additions & 0 deletions buysell2.py
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions peekingiterator.py
Original file line number Diff line number Diff line change
@@ -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].