[
design
]
BinarySearch 0733 Peekable Iterator
Problem statement
https://binarysearch.com/problems/Peekable-Iterator/
Solution
Similar to Leetcode 0284. Peeking Iterator, but here what is given is list, not iterator.
Complexity
Time is O(n)
for init and O(1)
for other operations, space is O(n)
.
Code
class PeekableIterator:
def __init__(self, nums):
self.nums = nums
self.p = 0
def peek(self):
return self.nums[self.p]
def next(self):
self.p += 1
return self.nums[self.p - 1]
def hasnext(self):
return self.p < len(self.nums)