File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ # 백준 3078번: 좋은 친구
2+
3+ from collections import defaultdict
4+ import sys
5+
6+ input = sys .stdin .readline
7+
8+ # N, K <= 3 * 10^5
9+ N , K = map (int , input ().rstrip ().split ())
10+ names = [len (input ().rstrip ()) for _ in range (N )]
11+ # print(names)
12+
13+ # '좋은 친구'의 2가지 조건
14+ # 1. 인덱스(등수) 차이가 K 이하일 것
15+ # 2. 글자수가 같을 것
16+
17+ # 브루트포싱의 시간복잡도
18+ # 각 사람마다 나머지 사람에 대해서 조사 -> O(N^2)
19+ # 정렬되어 있다는 특성이 있으므로 슬라이딩 윈도우로 접근해야 할 듯.. 어떤 방법이든 O(N^2)보다는 작아야 함
20+
21+ left , right = 0 , 1
22+ freq = defaultdict (int )
23+ freq [names [0 ]] = 1
24+ answer = 0
25+
26+ while left < right and right < N :
27+ # print(left, right)
28+ if right - left <= K :
29+ answer += freq [names [right ]]
30+ freq [names [right ]] += 1
31+ # print(f'answer: {answer}')
32+ right += 1
33+ else :
34+ freq [names [left ]] -= 1
35+ left += 1
36+
37+ print (answer )
You can’t perform that action at this time.
0 commit comments