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
13 changes: 13 additions & 0 deletions problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# problem 1

#https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
# n size of prices -> 0(n)
# 1
for i in range(len(prices)-1):
if prices[i] < prices[i+1]:
profit = profit + (prices[i+1]-prices[i])
return profit
72 changes: 72 additions & 0 deletions problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# problem2.py

#https://leetcode.com/problems/peeking-iterator/description/


# 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.tempPeak = self.iterator.next() if self.iterator.hasNext() else None



def peek(self): #1
"""
Returns the next element in the iteration without advancing the iterator.
:rtype: int
"""
if self.tempPeak:
return self.tempPeak
return -1

def next(self):
"""
:rtype: int
"""
temp = self.peek() if self.hasNext() else -1
if temp == -1:
return temp
self.tempPeak = self.iterator.next() if self.iterator.hasNext() else None
return temp



def hasNext(self):
"""
:rtype: bool
"""
if self.peek() != -1:
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].