File tree Expand file tree Collapse file tree 5 files changed +77
-0
lines changed
_WeeklyChallenges/W08-[TwoPointer] Expand file tree Collapse file tree 5 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 🚀 1월 4주차 (01/20) 스터디 발제 주제: Two-Pointer
2+ > 발제자: 정수미
3+
4+ ### 🗂️ 스터디 자료
5+ - PDF: [ 바로가기] ( ./Study_BOJ_1253.pdf )
6+ ![ image] ( https://github.com/user-attachments/assets/aeebc15f-c536-4838-893e-3e2becb9b3aa )
7+
8+
9+ ### 📖 문제
10+ - [ 백준 #1253 . 좋다] ( https://www.acmicpc.net/problem/1253 ) : 투포인터 / 골드4
11+ - 정답 코드: [ Study_BOJ_1253_좋다.py] ( ./Study_BOJ_1253_좋다.py )
12+
13+ ### 💻 과제
14+ - [ 백준 #8114 . 블랙 프라이데이] ( https://www.acmicpc.net/problem/18114 ) : 브루트포스, 투포인터 / 골드5
15+ - 정답 코드: [ Assignment_BOJ_18114_블랙프라이데이.py] ( ./Assignment_BOJ_18114_블랙프라이데이.py )
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ N = int (input ()) # 수의 개수
5+ num = list (map (int , input ().split ())) # 입력된 숫자 리스트
6+ num .sort () # 정렬 (투포인터 사용을 위해)
7+
8+ cnt = 0 # 좋은 수의 개수
9+ for i in range (N ):
10+ goal = num [i ] # 현재 확인할 값
11+ start = 0
12+ end = N - 1
13+
14+ while start < end : # 두 포인터가 교차할 때까지 반복
15+ # 현재 두 수의 합이 goal인지 확인
16+ if num [start ] + num [end ] == goal :
17+ if start == i : # start 포인터가 현재 값(goal)을 가리키면 이동
18+ start += 1
19+ elif end == i : # end 포인터가 현재 값(goal)을 가리키면 이동
20+ end -= 1
21+ else : # 두 수의 합이 goal이면서 현재 값(goal)을 포함하지 않을 때
22+ cnt += 1
23+ break
24+
25+ elif num [start ] + num [end ] > goal : # 합이 goal보다 크면 큰 값을 줄여야 하므로 end 감소
26+ end -= 1
27+
28+ else : # 합이 goal보다 작으면 작은 값을 키워야 하므로 start 증가
29+ start += 1
30+
31+ print (cnt )
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ N = int (input ()) # 수의 개수
5+ num = list (map (int , input ().split ())) # 입력된 숫자 리스트
6+ num .sort () # 정렬 (투포인터 사용을 위해)
7+
8+ cnt = 0 # 좋은 수의 개수
9+ for i in range (N ):
10+ goal = num [i ] # 현재 확인할 값
11+ start = 0
12+ end = N - 1
13+
14+ while start < end : # 두 포인터가 교차할 때까지 반복
15+ # 현재 두 수의 합이 goal인지 확인
16+ if num [start ] + num [end ] == goal :
17+ if start == i : # start 포인터가 현재 값(goal)을 가리키면 이동
18+ start += 1
19+ elif end == i : # end 포인터가 현재 값(goal)을 가리키면 이동
20+ end -= 1
21+ else : # 두 수의 합이 goal이면서 현재 값(goal)을 포함하지 않을 때
22+ cnt += 1
23+ break
24+
25+ elif num [start ] + num [end ] > goal : # 합이 goal보다 크면 큰 값을 줄여야 하므로 end 감소
26+ end -= 1
27+
28+ else : # 합이 goal보다 작으면 작은 값을 키워야 하므로 start 증가
29+ start += 1
30+
31+ print (cnt )
You can’t perform that action at this time.
0 commit comments