배열 합치기

import sys
input = sys.stdin.readline

A, B = map(int, input().split())
arrA = list(map(int, input().split()))
arrB = list(map(int, input().split()))
arrR = []

ptrA = 0
ptrB = 0

while ptrA + ptrB != A + B:
    # 한 배열에서 포인터가 끝까지 이동한 경우(반대쪽 배열에서 더 큰것만 남음) : 반대쪽 배열의 아이템 추가
    if ptrA == A:
        arrR.append(arrB[ptrB])
        ptrB += 1
    elif ptrB == B:
        arrR.append(arrA[ptrA])
        ptrA += 1

    # 포인터가 가리키는 아이템끼리 비교하여 작은것을 넣음.
    elif arrA[ptrA] <= arrB[ptrB]:
        arrR.append(arrA[ptrA])
        ptrA += 1
    elif arrA[ptrA] > arrB[ptrB]:
        arrR.append(arrB[ptrB])
        ptrB += 1

for item in arrR:
    print(item, end = ' ')

블로그

import sys
input = sys.stdin.readline

N, X = map(int, input().split())
arr = list(map(int, input().split()))

sta = 0
end = X-1

max = 0
res = 0
tot = 0

while end != N:
    if sta == 0:
        for i in range(0, X):
            tot += arr[i]
    else:
        tot -= arr[sta-1]
        tot += arr[end]

    if tot > max:
        max = tot
        res = 1
    elif tot == max:
        res += 1

    sta += 1
    end += 1

if max == 0:
    print("SAD")
else:
    print(max)
    print(res)

겹치는 건 싫어

import sys
input = sys.stdin.readline

N, K = map(int, input().split())
arr = list(map(int, input().split()))   # 입력받은 숫자 배열

sta = 0     # arr에서 최장 연속 부분 수열의 원소로 count중인 수열의 첫 index
end = 0     # arr에서 최장 연속 부분 수열의 원소로 count중인 수열의 마지막 index
res = 0     # 결과 값 (최장 연속 부분 수열의 길이)
dic = {}    # 딕셔너리 (값 : 값의 개수)

for X in arr:   # arr에 있는 원소 순서대로 봄.
    # 같은 게 있는지 체크
    if X in dic:    # X가 dic에 있는 경우
        dic[X] += 1
        end += 1
        if dic[X] > K:   # 개수가 K개 초과인 경우
            while True:   # 초과하는 아이템까지 개수 -1 처리 (3 2 5 5 6 4 4 5 로 5가 3번 나오면 3 2 5를 지워버림)
                item = arr[sta]
                sta += 1
                dic[item] -= 1
                if dic[item] < 0:       # 개수가 0보다 작으면 아예 삭제
                    del dic[item]
                if item == X:
                    break
    else:   # X가 dic에 없는 경우
        dic[X] = 1
        end += 1

    res = max(res, end - sta)
    # print(cnt, res)

print(res)