Skip to content

Commit eff07d7

Browse files
committed
Solved: 1920 수 찾기
1 parent 8824e36 commit eff07d7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

jaykxo/week03/1920_수_찾기.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1920 수 찾기
2+
# 메모리: 50412 KB, 시간: 504 ms
3+
4+
import sys
5+
input = sys.stdin.readline
6+
from typing import Any, Sequence
7+
8+
N = int(input())
9+
A = list(map(int, input().split()))
10+
A.sort() # 이분 탐색을 위해 반드시 정렬 필요
11+
M = int(input())
12+
K = list(map(int, input().split()))
13+
14+
def bin_search(A: Sequence, key: Any):
15+
pl = 0
16+
pr = len(A) - 1 # 탐색 범위 오른쪽 끝 (리스트 마지막 인덱스)
17+
18+
while pl <= pr:
19+
pc = (pl + pr) // 2 # 현재 탐색 구간의 중간 인덱스
20+
21+
if A[pc] == key:
22+
print("1")
23+
return pc # 찾으면 인덱스 반환 (실제 문제에서는 출력만 필요)
24+
25+
elif A[pc] < key:
26+
pl = pc + 1 # 찾는 값이 더 크면 오른쪽 반으로 탐색 범위 좁힘
27+
else:
28+
pr = pc - 1 # 찾는 값이 더 작으면 왼쪽 반으로 탐색 범위 좁힘
29+
30+
print("0") # 끝까지 못 찾으면 0 출력
31+
32+
# 찾을 숫자들을 하나씩 이분 탐색 실행
33+
for key in K:
34+
bin_search(A, key)
35+
36+
37+
############ 번외 ############
38+
# node.js 제출 버전
39+
# 메모리: 39068 KB, 시간: 324 ms
40+
41+
# const fs = require("fs");
42+
# const input = fs.readFileSync(0).toString().trim().split("\n");
43+
44+
# const N = Number(input[0]);
45+
# const A = input[1].split(" ").map(Number).sort((a, b) => a - b);
46+
# const M = Number(input[2]);
47+
# const targets = input[3].split(" ").map(Number);
48+
49+
# function binarySearch(arr, target) {
50+
# let start = 0, end = arr.length - 1;
51+
# while (start <= end) {
52+
# let mid = Math.floor((start + end) / 2);
53+
# if (arr[mid] === target) return 1;
54+
# if (arr[mid] < target) start = mid + 1;
55+
# else end = mid - 1;
56+
# }
57+
# return 0;
58+
# }
59+
60+
# console.log(targets.map(t => binarySearch(A, t)).join("\n"));

0 commit comments

Comments
 (0)