【问题标题】:Is this colidity challenge (Zinc 2018) test case wrong?这个 colidity 挑战(Zinc 2018)测试用例是错误的吗?
【发布时间】:2020-08-01 09:49:46
【问题描述】:

我只是随机接受了这个挑战......

问题和报告可以在这里找到:https://app.codility.com/demo/results/training3NRM6P-HSG/

对于测试用例N = 100,000, all performances are different.,它说:got 166661666700000 expected 665533373

对于N = 100,000所有不同的表现不应该是:C(100000, 3) = int(len(A) * (len(A) - 1) * (len(A) - 2) / 3 / 2)665533373是如何计算的?

将我的解决方案粘贴到此处以方便阅读:

def solution(A):
    # write your code in Python 3.6
    if not A or len(A) < 3:
        return 0
        
    if len(set(A)) == len(A):
        return int(len(A) * (len(A) - 1) * (len(A) - 2) / 3 / 2)
        
    check = {}
    def bt(path, nxt):
        if len(path) == 3:
            t = tuple(path)
            if t not in check:
                check[t] = None
            return
        
        if len(path) > 3:
            return
        
        for i in range(nxt, len(A)):
            if i > nxt and A[i] == A[i-1]:
                continue
            path.append(A[i])
            bt(path, i + 1)
            path.pop()
            
    bt([], 0)
    
    return len(check)

【问题讨论】:

    标签: python algorithm math combinations permutation


    【解决方案1】:

    仔细看问题!它清楚地表明,“由于答案可能非常大,因此提供模 10^9 + 7 (1,000,000,007)”。

    你的回答,166661666700000 % (10^9 + 7) = 665533373,这是预期的结果。

    所以理论上你需要做的就是像这样编辑代码的最后一行:

    return len(check) % (10**9 + 7)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多