diff --git a/Buyandsellstock.py b/Buyandsellstock.py new file mode 100644 index 0000000..fd18cfd --- /dev/null +++ b/Buyandsellstock.py @@ -0,0 +1,20 @@ +class Solution(object): + # tc : O(n), # sc : O(1) + def maxProfit(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + # appraoch : dp check buying today and selling the other day and so on and see the exhaustively check both buying r not busyign that day + # better approach : peak valley approach + # lets take a test case [1,5,6] the profit is bought on 1st day 5-1, 6-5 = 4+1 = 5 + #or bought on 1st day and sold on 3rd day 6-1 = 5 again + # so the profit is basically if the next day is greater than than curr day then we get the profit there and if we apply this logic till the end of the arr we get our profit + # its like by the first day and sell the last most profitabel day to get the proit and in betwen buy and sell + busy and sell and so on.. gives us the same output + + 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 \ No newline at end of file diff --git a/PeekingIterator.py b/PeekingIterator.py new file mode 100644 index 0000000..04730f4 --- /dev/null +++ b/PeekingIterator.py @@ -0,0 +1,60 @@ +# Below is the interface for Iterator, which is already defined for you. +# +# class Iterator(object): +# 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(object): + # tc : O(1) for all, sc : O(1) + # the prb with accessign the curr iterator eis as we move to the next we loose the curr element so we need to save te element so that we can peek the element + # having a global variable nextelemnt which is basically the iterator.next, for hasnext we can check from the iterator.hasnext + def __init__(self, iterator): + """ + Initialize your data structure here. + :type iterator: Iterator + """ + self.iterator = iterator + self.next_element = iterator.next() if iterator.hasNext() else None # edge case if there are no other elemenst and we want to access the next element we will simply return none + + def peek(self): + """ + Returns the next element in the iteration without advancing the iterator. + :rtype: int + """ + return self.next_element # at peek we just return the element and ntg else to do + + def next(self): + """ + :rtype: int + """ + # at next since we also need to move our pointer lest first save our element + curr = self.next_element + self.next_element = self.iterator.next() if self.iterator.hasNext() else None # now lets move our pointer and before moving save the elemnt for future cases + return curr # return the element + + def hasNext(self): + """ + :rtype: bool + """ + return self.next_element != None # if we don't have any nextelement that means we are at the end + +# 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