algorithm - 分析用户网站访问模式时间和空间复杂度
问题描述
大家好,我想知道是否有人可以帮助我了解 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(" ")
解决方案
推荐阅读
- angular - “+”语法的角度路由官方示例用法
- r - r 中数据中的保加利亚西里尔文
- python - AttributeError:“元组”对象没有属性“readlines”
- node.js - 在我的 ReactNative 应用程序上安装 Realm 失败;与:npm 错误!在领域@3.6.0 安装脚本“node-pre-gyp install --fallback-to-build”失败
- angular - Angular 9:升级到 Angular 9“9.0.0-rc.5”时出错
- loopback4 - 如何在node js的loopback 4框架中应用搜索api
- create-react-app - 创建 React 应用程序不起作用:错误 @typescript-eslint/eslint-plugin@2.10.0:引擎“节点”与此模块不兼容?
- amazon-web-services - 放大推送通知问题(React Native)
- python - 在包含大量信息的文本中为日期添加字符
- javascript - 多个 HTML 文件仅链接到一个 JS 文件