查看: 2617| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] leetcode的算法时间是如何计算的?

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
最近在刷leetcode,屡次发现了自己同样的算法,在最后的排行榜中比别人低,不知道有没有大佬可以解惑?
一个例子: 这个例子比较明显,今天的每日一题任务中:Find the town Judge
题目:
In a town, there are N people labelled from 1 to N.  There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

The town judge trusts nobody.
Everybody (except for the town judge) trusts the town judge.
There is exactly one person that satisfies properties 1 and 2.
You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.

If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return -1.

不一定是最优解,但是也是beat 90%以上的解法: 时间744ms
  1. def findJudge(self, N: int, trust: List[List[int]]) -> int:
  2.         society_level = [0 for i in range(N)]
  3.         suspicion_level = [0 for i in range(N)]

  4.         for el in trust:
  5.             society_level[el[1] - 1] += 1
  6.             suspicion_level[el[0] - 1] += 1

  7.         for i in range(N):
  8.             if society_level[i] == N - 1:
  9.                 if suspicion_level[i] == 0:
  10.                     return i + 1

  11.         return -1
复制代码


然后我的解法几乎和他一样,然后后续做了微弱改动和他一样了,但是需要1000ms:
  1. first = [0 for k in range(N)]
  2.         second = [0 for k in range(N)]
  3.         for i in trust:
  4.             first[i[0]-1] += 1
  5.             second[i[1]-1] += 1
  6.         for j in range(N):
  7.             if first[j] == 0 and second[j] == N-1:#he trust nobody and he got N-1 trust
  8.                 return j+1
  9.         return -1
复制代码


不太明白~~理论上,同样的代码都是给leetcode的服务器来跑,所以应该是一样的时间吧? 还是说这个取决于生成了不同的test case?
  

上一篇:组队一起刷Datacamp课程
下一篇:day 4,如何干翻所有coding面试
推荐
494383005 2020-5-11 12:22:09 | 只看该作者
全局:
我刚刚也碰到了这个问题 同样算法别人超过95% 我只超过18%
我的猜测是 leetcode会一直给每道题增加testcase 超过95%那个老哥是18年提交的 现在20年了可能社区里其他人给这道题加了几千个testcase
回复

使用道具 举报

🔗
教皇 2020-5-11 05:10:34 | 只看该作者
全局:
我OA老是死在runtime 上,明明都差不多
回复

使用道具 举报

🔗
草草草群 2020-5-11 05:18:25 | 只看该作者
全局:
这题是不是跟find celebrity那一题有点像(蠡口迩奇奇)
回复

使用道具 举报

🔗
fqee 2020-5-11 06:06:53 | 只看该作者
全局:
你的第7行(对比上面的10-11 行)会多evaluate 一次== 比较, 大概只有这个区别了吧
回复

使用道具 举报

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

本版积分规则

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