0% found this document useful (0 votes)
2 views2 pages

Week 3

The document contains two coding problems from LeetCode. The first problem involves implementing a BrowserHistory class using a doubly linked list to manage URLs and navigate through history. The second problem focuses on finding the maximum subarray sum in a circular array using Kadane's algorithm and handling edge cases for negative numbers.

Uploaded by

Matrix Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Week 3

The document contains two coding problems from LeetCode. The first problem involves implementing a BrowserHistory class using a doubly linked list to manage URLs and navigate through history. The second problem focuses on finding the maximum subarray sum in a circular array using Kadane's algorithm and handling edge cases for negative numbers.

Uploaded by

Matrix Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

WEEK-3

1.https://leetcode.com/problems/design-browser-history/
# A class to represent a node in a doubly linked list
class Node:
def __init__(self, url: str):
# The previous and next nodes in the list
self.prev = None
self.next = None
# The URL represented by this node
self.url = url

# A class to represent a browser history


class BrowserHistory:
def __init__(self, homepage: str):
# Create a new node to represent the homepage
self.curr = Node(homepage)

# Method to add a new URL to the history


def visit(self, url: str) -> None:
# Create a new node to represent the new URL
self.curr.next = Node(url)
# Set the previous node for the new node to be the current node
self.curr.next.prev = self.curr
# Make the new node the current node
self.curr = self.curr.next

# Method to navigate back in the history by the given number of steps


def back(self, steps: int) -> str:
# While there are previous nodes and we haven't gone back enough steps yet
while self.curr.prev and steps > 0:
# Move back one node by setting the current node to the previous node
self.curr = self.curr.prev
steps -= 1
# Return the URL represented by the current node
return self.curr.url

# Method to navigate forward in the history by the given number of steps


def forward(self, steps: int) -> str:
# While there are next nodes and we haven't gone forward enough steps yet
while self.curr.next and steps > 0:
# Move forward one node by setting the current node to the next node
self.curr = self.curr.next
steps -= 1
# Return the URL represented by the current node
return self.curr.url

2. https://leetcode.com/problems/stamping-the-sequence/
class Solution:
def maxSubarraySumCircular(self, nums: List[int]) -> int:
def kadane(arr :List[int])->int :
max_ends_here = max_so_far = arr[0]
for el in arr[1:]:
max_ends_here = max(el, max_ends_here + el)
max_so_far = max(max_ends_here, max_so_far )
return max_so_far
max_kadane = kadane(nums) #step1
totsum = sum(nums) #step2

inverted_arr = [-x for x in nums]


min_kadane = kadane(inverted_arr) #step3

if totsum == -min_kadane : # handle edge case where all


#elements in nums are negative.
return max_kadane

return max(max_kadane, totsum + min_kadane) #compare and return max value.

You might also like