문제풀이/투포인터

https://www.acmicpc.net/problem/1940코드n = int(input())m = int(input())ingredients = list(map(int, input().split()))ingredients.sort()l, r = 0, n-1answer = 0while l 각 재료들은 고유한 번호를 갖고 있고, 우리는 이 재료들 중 합이 m이 되는 2개의 재료 조합을 구하면 된다. 이를 구하기 위해 정렬과 투포인터를 사용했다. 1. 재료 배열을 오름차순으로 정렬한다.2. l은 배열의 시작점, r은 배열의 끝점으로 초기화하고, `l 2-1) l과 r 위치에 있는 두 재료의 합이 m과 같다면 answer를 증가시키고, l과 r을 각각 안쪽으로 한 칸씩 옮긴다. 2-2) 합이 m보다 작..
https://www.acmicpc.net/problem/3078코드import sysinput = sys.stdin.readlinen, k = map(int, input().split())students = [input() for _ in range(n)]window = dict()answer = 0for i in range(k+1): length = len(students[i]) if length in window: answer += window[length] else: window[length] = 0 window[len(students[i])] += 1for i in range(k+1, n): length = len(students[i]) ..
https://www.acmicpc.net/problem/15565코드import sysinput = sys.stdin.readlinen, k = map(int, input().split())dolls = list(map(int, input().split()))l = 0answer = n+1lion = 0for r in range(n): if dolls[r] == 1: lion += 1 while lion >= k: if lion == k and dolls[l] == 1: break if dolls[l] == 1: lion -= 1 l += 1 if lion == k: answer = m..
https://www.acmicpc.net/problem/15831코드n, b, w = map(int, input().split())pebbles = input()answer = 0left, right = 0, 0count = dict([("B", 0), ("W", 0)])count[pebbles[left]] += 1while right = w: answer = max(answer, right-left+1) right += 1 if right > n-1: break count[pebbles[right]] += 1 while count['B'] > b: count[pebbles[left]] -= 1 left += 1print(answe..
https://www.acmicpc.net/problem/1253코드import sysinput = sys.stdin.readlinen = int(input())arr = list(map(int, input().split()))arr.sort()ans = 0for i in range(n): l, r = 0, n-1 while l = r: break if arr[l]+arr[r] == arr[i]: ans += 1 break elif arr[l]+arr[r]  설명1. 처음 입력으로 주어진 배열 arr를 오름차순 정렬한다.2. 정렬된 arr의 첫번째 원소부터 좋은 수인지 판별한다.    a. 투포인터로 검색을 할..
딜레이레이
'문제풀이/투포인터' 카테고리의 글 목록