我曾尝试过Google的kickstart 2020挑战赛。 C轮问题1让我有些难过。我尝试了许多不同的方式来完成挑战。这个问题看起来很简单,但我无法完成。问题是我不明白自己在做什么错。请为我指出正确的方向,或为我的代码指出问题。
Google Kickstart 2020-C轮|问题1 https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/00000000003380d2
Avery has an array of N positive integers. The i-th integer of the array is Ai. A contiguous subarray is an m-countdown if it is of length m and contains the integers m, m-1, m-2, ..., 2, 1 in that order. For example, [3, 2, 1] is a 3-countdown. Can you help Avery count the number of K-countdowns in her array? Input The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai. Output For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.
get the number of cases Loop in range(number of cases): get the N (number of elements), K(initial countdown value) get the array of values generate an array of the countdown sequence [K ... 1] - signature counter = 0 Loop elem in range(Number of elements): if elem == K: if there is space to slice the array (length of signature) - possible signature if possible signature == signature: counter += 1 print(counter)
#!/usr/bin/python
# -*- coding: utf-8 -*-
noc = int(input('')) # getting the number of cases # NOC- number of cases
# Loop over the # of cases
for c in range(noc):
(N, K) = [int(i) for i in input('').split(' ')] # getting N, K
# N - number of elements given
# K - initial countdown value
# getting the elements
caseList = [int(i) for i in input('').split(' ')]
# generating a 'signature' or list of factorial for the countdown
steps = [i for i in range(1, K + 1)][::-1]
# counter for number of matches
countdown = 0 # init value
# loop over each element i n list
for i in range(N):
# if element == K(init countdown number)
if caseList[i] == K:
# make sure there is space to get the sliced array
if i <= len(caseList) - len(steps):
# get the next m numbers if
if caseList[i:i + len(steps)] == steps:
countdown += 1 # increment
print countdown # print the number of matches
答案 0 :(得分:1)
您的解决方案看起来不错,除了输出不是指定的,不是针对Python 3,而是针对2,只需将其更改为:
print(f'Case {c}: {countdown}')
除此之外,您要做的工作比需要的多。您实际上只需要遍历整个列表一次即可计算K倒计时。
例如:
import sys
from io import StringIO
sys.stdin = StringIO('3\n2 2\n2 1\n8 2\n0 2 1 2 2 1 2 1 0\n0 2\n\n')
t = int(input())
for c in range(t):
(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
# initialise goal, position in array and count
goal, i, count = k, 0, 0
while i < n:
# if item in current position matches current goal
if a[i] == goal:
# then next goal item is one less
goal -= 1
# until all in K-countdown were found
if goal == 0:
# then start over and increase count
goal = k
count += 1
# look at the next position
i += 1
# else (current item doesn't match goal), if already looking for start of K-countdown
elif goal == k:
# look at the next position
i += 1
# else (current item doesn't match goal, goal wasn't start of K-countdown)
else:
# look for start of K-countdown
goal = k
print(f'Case #{c}: {count}')
答案 1 :(得分:0)
我发现您的解决方案没有任何问题。可能是您的输出格式。
您应该以Case #x的形式输出:y,其中x是测试用例编号(从1开始),y是她数组中K倒数的数量。
示例:
案例1:2
案例2:0
案例3:1
注意:如果您使用的是print x
而不是print(x)
,请确保使用的是Python 2.x
答案 2 :(得分:0)
我也在想同样的事情。
让我们看看给定的约束:
1 ≤ T ≤ 100. # Testcases
2 ≤ K ≤ N. # Value of K
1 ≤ Ai ≤ 2 × 105, for all i. # Index- i
# Test Set 1
2 ≤ N ≤ 1000.
# Test Set 2
2 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 2 ≤ N ≤ 1000.
现在假设我们有一个测试用例
nums = [1]
k = 1
有人可能会认为K = 1倒数= 1对吗?其实不是。
仔细阅读2 <= N,表示
数组长度必须为最小长度= 2。
Expected result,
nums = [1]
K = 1
coutdown = 0
答案 3 :(得分:0)
当约束已经说 2<=N 是不是就没有数组长度=0或1的测试用例了
答案 4 :(得分:0)
@MFK34 没有问题,除了 print() 在 python 3 中需要括号,他在循环结束时立即打印答案并且解决方案不符合预期。以下是我修改后的解决方案。
#!/usr/bin/python
# -*- coding: utf-8 -*-
noc = int(input('')) # getting the number of cases # NOC- number of cases
op = []
# Loop over the # of cases
for c in range(noc):
(N, K) = [int(i) for i in input('').split(' ')] # getting N, K
caseList = [int(i) for i in input('').split(' ')]
steps = [i for i in range(1, K + 1)][::-1]
# counter for number of matches
countdown = 0 # init value
# loop over each element i n list
for i in range(N):
# if element == K(init countdown number)
if caseList[i] == K:
# make sure there is space to get the sliced array
if i <= len(caseList) - len(steps):
# get the next m numbers if
if caseList[i:i + len(steps)] == steps:
countdown += 1 # increment
op.append(countdown)
for i,d in enumerate(op):
print("Case #"+str(i+1)+":",d)
我刚刚将结果存储在一个数组中,然后按预期顺序打印在输入的末尾。