
🔎 문제
문제
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?
입력
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.
출력
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.
문제
에이버리에는 N개의 양의 정수로 이루어진 배열이 있습니다. 배열의 i번째 정수는 Ai입니다.
연속된 하위 배열은 길이가 m이고 정수 m, m-1, m-2, ..., 2, 1을 순서대로 포함하는 경우 m-카운트다운입니다. 예를 들어 [3, 2, 1]은 3 카운트다운입니다.
에이버리가 배열에서 K- 카운트다운의 수를 세는 것을 도와줄 수 있나요?
입력
입력의 첫 번째 줄은 테스트 케이스의 수인 T. T 테스트 케이스가 이어집니다.
각 테스트 케이스는 정수 N과 K가 포함된 줄로 시작하며, 두 번째 줄에는 N개의 정수가 들어 있습니다. i번째 정수는 Ai입니다.
출력
각 테스트 케이스에 대해 케이스 #x: y를 포함하는 한 줄을 출력합니다. 여기서 x는 테스트 케이스 번호(1부터 시작)이고 y는 배열의 K 카운트다운 횟수입니다.
💡풀이 과정
def count_k_countdowns(N, K, A):
countdowns = 0
i = 0
while i < N:
if A[i] == K:
found = True
for j in range(K):
if i + j >= N or A[i + j] != K - j:
found = False
break
if found:
countdowns += 1
i += K
else:
i += 1
else:
i += 1
return countdowns
T = int(input().strip())
for t in range(1, T + 1):
N, K = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
result = count_k_countdowns(N, K, A)
print(f"Case #{t}: {result}")
👍🏻 숏코딩
import sys; input = sys.stdin.readline
for t in range(1, int(input()) + 1):
N, K = map(int, input().split())
A = list(map(int, input().split()))
countdown = list(range(K, 0, -1))
answer = 0
for i in range(N - K + 1):
for j in range(K):
if A[i + j] != countdown[j]:
break
else:
answer += 1
print('Case #%d: %d' % (t, answer))'Python > 코딩 테스트' 카테고리의 다른 글
| [백준] 14171번: Cities and States (4) | 2024.11.13 |
|---|---|
| [백준] 13220번 Secret (6) | 2024.11.12 |
| [동적계획법] N으로 표현 (0) | 2024.11.08 |
| [백준] 15829번: Hashing (1) | 2024.11.07 |
| [프로그래머스 - 완전 탐색] 소수 찾기 (0) | 2024.09.17 |
댓글