0% found this document useful (0 votes)
2 views

Advanced coding assignment 2

The document contains implementations of four algorithms: Maximum Sum Circular Subarray, Stamping The Sequence, Design Browser History, and LRU Cache. Each algorithm is encapsulated within a class with methods to perform specific tasks related to the problem it addresses. The document provides code examples demonstrating the functionality of each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced coding assignment 2

The document contains implementations of four algorithms: Maximum Sum Circular Subarray, Stamping The Sequence, Design Browser History, and LRU Cache. Each algorithm is encapsulated within a class with methods to perform specific tasks related to the problem it addresses. The document provides code examples demonstrating the functionality of each algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Maximum Sum Circular Subarray:

class Solution(object):
def maxSubarraySumCircular(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def kadane(array):
max_sum = curr_max = array[0]
for num in array[1:]:
curr_max = max(num, curr_max + num)
max_sum = max(max_sum, curr_max)
return max_sum
total_sum = sum(nums)
max_kadane = kadane(nums)
min_kadane = kadane([-num for num in nums])
if max_kadane > 0:
return max(max_kadane, total_sum + min_kadane)
return max_kadane
Stamping The Sequence:
class Solution(object):
def movesToStamp(self, stamp, target):
stamp_len = len(stamp)
target_len = len(target)
target = list(target)
result = []
visited = [False] * target_len
stars = 0
def can_stamp(pos):
for i in range(stamp_len):
if target[pos + i] != '?' and target[pos + i] != stamp[i]:
return False
return True
def do_stamp(pos):
count = 0
for i in range(stamp_len):
if target[pos + i] != '?':
target[pos + i] = '?'
count += 1
return count
while stars < target_len:
stamped = False
for i in range(target_len - stamp_len + 1):
if not visited[i] and can_stamp(i):
stars += do_stamp(i)
visited[i] = True
result.append(i)
stamped = True
if stars == target_len:
break
if not stamped:
return []
return result[::-1]
Design Browser History:
class BrowserHistory(object):
def __init__(self, homepage):
"""
:type homepage: str
"""
self.history = [homepage]
self.current_index = 0
def visit(self, url):
"""
:type url: str
:rtype: None
"""
self.history = self.history[:self.current_index + 1]
self.history.append(url)
self.current_index += 1
def back(self, steps):
"""
:type steps: int
:rtype: str
"""
self.current_index = max(0, self.current_index - steps)
return self.history[self.current_index]

def forward(self, steps):


"""
:type steps: int
:rtype: str
"""
self.current_index = min(len(self.history) - 1, self.current_index + steps)
return self.history[self.current_index]
browserHistory = BrowserHistory("leetcode.com")
browserHistory.visit("google.com")
browserHistory.visit("facebook.com")
browserHistory.visit("youtube.com")
print(browserHistory.back(1))
print(browserHistory.back(1))
print(browserHistory.forward(1))
browserHistory.visit("linkedin.com")
print(browserHistory.forward(2))
print(browserHistory.back(2))
print(browserHistory.back(7))
LRU Cache:
class Node:
"""Doubly linked list node."""
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity = capacity
self.cache = {}
self.head = Node(0, 0)
self.tail = Node(0, 0)
self.head.next = self.tail
self.tail.prev = self.head
def _remove(self, node):
"""Remove a node from the linked list."""
prev_node = node.prev
next_node = node.next
prev_node.next = next_node
next_node.prev = prev_node

def _add_to_head(self, node):


"""Add a node right after the head."""
node.prev = self.head
node.next = self.head.next
self.head.next.prev = node
self.head.next = node
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key in self.cache:
node = self.cache[key]
self._remove(node)
self._add_to_head(node)
return node.value
return -1
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
if key in self.cache:
node = self.cache[key]
self._remove(node)
elif len(self.cache) >= self.capacity:
lru_node = self.tail.prev
self._remove(lru_node)
del self.cache[lru_node.key]
new_node = Node(key, value)
self._add_to_head(new_node)
self.cache[key] = new_node
lRUCache = LRUCache(2)
lRUCache.put(1, 1)
lRUCache.put(2, 2)
print(lRUCache.get(1))
lRUCache.put(3, 3)
print(lRUCache.get(2))
lRUCache.put(4, 4)
print(lRUCache.get(1))
print(lRUCache.get(3))
print(lRUCache.get(4))

You might also like