12
返回列表 发新帖
楼主: cat110
跳转到指定楼层
上一主题 下一主题
收起左侧

认真刷题的打卡

🔗
 楼主| cat110 2021-2-19 13:14:58 | 只看该作者
全局:
743. Network Delay Time

https://leetcode.com/problems/ne ... -easy-to-understand

class Solution:
    def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
        # https://brilliant.org/wiki/dijkstras-short-path-finder/
        """
        Dijkstra
        time O(E+VlogV)
        space O(E+V)
        """
        
        # https://leetcode.com/problems/ne ... -easy-to-understand
        
        # build graph
        weight = collections.defaultdict(dict)
        for u, v, w in times:
            weight[v] = w
        
        heap = [(0,k)]
        distance = {}
        while(heap):
            cost, i = heapq.heappop(heap)
            if i not in distance:
                distance = cost
                for j in weight:
                    heapq.heappush(heap,(cost+weight[j], j))
        
        if len(distance) == n:
            return max(distance.values())
        return -1
        
回复

使用道具 举报

🔗
 楼主| cat110 2021-2-21 12:51:13 | 只看该作者
全局:
class Solution(object):
    def validTree(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool
        """
        # union find to solve this
        
        root = [i for i in range(n)]
        
        def find(i):
            if root[i]==i:
                return i
            root[i] = find(root[i])
            return root[i]
        
        def union(a, b):
            ra = find(a)
            rb = find(b)
            if ra == rb:
                return True
            
            root[ra] = rb
            root[a] = rb
            return False
        
        for edge in edges:
            if union(edge[0], edge[1]):
                print(root)
                return False
        ### ALWAYS
        for i in range(n):
            find(i)
            
        return len(set(root)) ==1
        
        
回复

使用道具 举报

🔗
 楼主| cat110 2021-2-21 17:42:45 | 只看该作者
全局:
#269. Alien Dictionary
class Solution(object):
    def alienOrder(self, words):
        """
        :type words: List[str]
        :rtype: str
        """
        graph = collections.defaultdict(list)
        status = {}
        
        def buildGraph(w1,w2):
            if w1 == w2:
                return True
            minlen = min(len(w1),len(w2))
            # abcxyz, abc
            if w1[:minlen] == w2[:minlen] and len(w1)>len(w2):
                return False
            i = 0
            while i<minlen and w1[i]==w2[i]:
                i+=1
            
            if w1[min(i,len(w1)-1)] in graph[w2[min(i,len(w2)-1)]]:
                #conflict
                return False
            graph[w1[min(i,len(w1)-1)]].append(w2[min(i,len(w2)-1)])
            return True
               
        # build Graph
        pool = []
        pool.extend(words[0])
        for word1, word2 in zip(words[:-1],words[1:]):
            pool.extend(word1+word2)
            if not buildGraph(word1, word2):
                return ""
        for c in pool:  
            status[c] = 0
        #print(status)
        
        """
        0 = unvisited
        1 = visited
        -1 = visiting
        """
        ret_stack = []
        
        def dfs(c):
            #print(c, status)
            if status[c] != 0:
                # T if finished , F if cycle found
                return status[c] == 1
            status[c] = -1
            for nextc in graph[c]:
                if not dfs(nextc):
                    return False
            status[c] = 1
            ret_stack.append(c)
            return True
            
        #print(status)
        for i in status.keys():
            r = dfs(i)
            
            if not r:
                return ""
        ret_stack.reverse()   
        return "".join(ret_stack)
            
        
回复

使用道具 举报

🔗
 楼主| cat110 2021-2-26 15:39:03 | 只看该作者
全局:
近期
2/14        301H, 236M, 105M 547M 98M       
2/15        721M 207M 210M        
2/16        332M,133M101E       
2/17        1153H,863M,684M       
2/18        399M,785M,323M       
2/19        886M,797M,743M       
2/20        1334M,128H,841M       
2/21        261M,1042M,57M,269H       
2/22        526M,131M,329H       
2/23        339M,364M,545M       
2/24        430M,417M,694M       
2/25        1087M,938E,114M       
回复

使用道具 举报

🔗
 楼主| cat110 2021-3-17 14:14:36 | 只看该作者
全局:
一直在刷只是跟新频率不高。近期:
2/26        46M
2/27        112E,47M,113M,208M,211M
2/28        
3/1        39M,366M,77M
3/2        733E,494M,40M
3/3        257E,216M,41H
3/4        1192H,254M
3/5        
3/6        95M,116M,117M,78M
3/7        108E,90M,489H
3/8        320M
3/9        784M,109M
3/10        106M,542M,130M
3/11        934M,1254M,314M
3/12        
3/13        679H.38M,987H
3/14        99H
3/15        1110M,104E,490M
回复

使用道具 举报

🔗
 楼主| cat110 2021-3-22 06:20:26 | 只看该作者
全局:
3/16        198M
3/17        213M,337M,256M,257E
3/18        529M,1026M
3/19        690E,979M,827H,968H
3/20        1161M,103M,515M,513M,429M
回复

使用道具 举报

🔗
 楼主| cat110 2021-3-30 16:11:10 | 只看该作者
全局:
3/21        865M,834H
3/22        55M,45M,1306M
3/23        62M,63M,64M,980H
3/24        1239M,36M,37H
3/25        11M
3/26        92M,22M
回复

使用道具 举报

🔗
 楼主| cat110 2021-3-30 16:12:14 | 只看该作者
全局:
3/27        221M,206E,25H
3/28        843M,121E,122E
3/29        123H,309M
回复

使用道具 举报

🔗
 楼主| cat110 2021-4-6 12:16:40 | 只看该作者
全局:
3/30        152M,53E
3/31        10H,853M
4/1        72H
4/2        813M,1335H,312H,410H
4/3        139M,140H,846M,1235H
4/4        704E
回复

使用道具 举报

🔗
 楼主| cat110 2021-4-14 15:33:22 | 只看该作者
全局:
4/5        4H
4/6        1326H,1024M,45M,50M
4/7        69E,981M
4/8       
4/9        153M,154H
4/10        81M,33M,162M,315H
4/11        74M,240M,528M,142M
4/12        287M,169E
4/13        801M
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表