我的Google Kickstart回合C Python代码有什么问题?

时间:2020-05-17 15:29:29

标签: python arrays list for-loop

可以在Python3中解决此问题,看看我的代码有什么问题吗?

问题:
Avery具有N个正整数的数组。数组的第i个整数是Ai。

如果连续子数组的长度为m,并且按该顺序包含整数m,m-1,m-2,...,2、1,则该数组为m-countdown。例如,[3,2,1]是3倒数。

您能帮助Avery计算她数组中K倒数的次数吗?

输入:
输入的第一行给出测试用例的数量,T。每个测试用例均以包含整数N和K的行开头。第二行包含N个整数。第i个整数是Ai。

输出:
对于每个测试用例,输出一行包含Case #x:y的行,其中x是测试用例编号(从1开始),y是她数组中K倒数的数量。

限制
时间限制:每个测试集20秒。
内存限制:1GB。
1≤T≤100。
2≤K≤N。
对于所有i,1≤Ai≤2×105。

测试集1:
2≤N≤1000。

测试集2:
最多10个测试用例为2≤N≤2×105。
对于其余情况,2≤N≤1000。

示例:

Input

3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100

Output

Case #1: 2
Case #2: 0
Case #3: 1

在示例案例1中,有两个3倒计时,如下所示。
1 2 3 7 9 3 2 1 8 3 2 1
1 2 3 7 9 3 2 1 8 3 2 1

在示例案例2中,没有2个倒数计时。

在示例案例3中,有一个6倒计时,如下所示。
100 7 6 5 4 3 2 1 100

这是我的代码

t = int(input())
# list 'numbers' store the number of countdowns in each testcase
numbers = []
for i in range(t):
    n, k = map(int, input().split()) 
    li = list(map(int, input().split()))
    # count the number of elements in countdown
    c1 = 1  
    # count the numbers of countdowns that match K
    c2 = 0
    for i in range(n-1):
        if li[i]-li[i+1] == 1:
            c1 += 1
        else:
            if c1 == k:
                c2 += 1
            c1 = 1
        print(c1, c2)
    numbers.append(c2)
for x in range(t):
    print('Case #{}: {}'.format(x, numbers[x]))

0 个答案:

没有答案