首页 > 解决方案 > 分析用户网站访问模式时间和空间复杂度

问题描述

大家好,我想知道是否有人可以帮助我了解 leetcode 1152 https://leetcode.com/problems/analyze-user-website-visit-pattern/分析用户网站访问模式的解决方案的时间和空间复杂性。我觉得空间复杂度是 O(N),其中 N 是参数中每个列表的长度,时间复杂度是 O(N^4)。空间复杂度是否还取决于有多少长度序列?

class Solution:
    def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
        dic = {}
        seq ={}
        
        for i in range(len(username)):
            user = username[i]
            if user in dic:
                dic[user].append([timestamp[i], website[i]])
            else:
                dic[user] = [[timestamp[i], website[i]]]
        d = {}
        for key in dic:
            d[key] = {}
            l = dic[key]
            if len(l) >= 3:
                l.sort(key = lambda x:  x[0])
            for i in range(len(l)):
                for j in range(i+1, len(l)):
                    for k in range(j+1, len(l)):
                        s = l[i][1] + " " + l[j][1] + " " + l[k][1]
                        if s not in d[key]:
                            d[key][s] = 1
        c = 0
        max = ""
        for key in d:
            for k in d[key]:
                if k not in seq:
                    seq[k] =1
                else:
                    seq[k] += 1
        for key in seq:
            if seq[key] > c:
                max = key
                c = seq[key]
            if seq[key] == c:
                if key < max:
                    max = key
                    c = seq[key] 
        return max.split(" ")

标签: algorithm

解决方案


推荐阅读