Problem statement

https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/

Solution

Actually what is asked is to perform simple graph traversal, we can use dfs or bfs.

Complexity

Time complexity is O(N + E), where N is number of nodes and E is number of edges. Space complexity is O(N).

Code

class Solution:
    def crawl(self, startUrl, htmlParser):
        def host(s):
            idx = s.find("/", 7)
            return s if idx == -1 else s[:idx]
        
        visited = set([startUrl])
        
        def dfs(url):
            for link in htmlParser.getUrls(url):
                if host(url) != host(link): continue
                if link in visited: continue
                visited.add(link)
                dfs(link)
                
        dfs(startUrl)
        return visited