Skip to content

Commit

Permalink
[Silver IV] Title: 수 찾기, Time: 416 ms, Memory: 43640 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ddubbu-dev committed Oct 27, 2024
1 parent 85c81f7 commit 6185249
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
4 changes: 2 additions & 2 deletions 백준/Silver/1920. 수 찾기/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

### 성능 요약

메모리: 46720 KB, 시간: 428 ms
메모리: 43640 KB, 시간: 416 ms

### 분류

이분 탐색, 자료 구조, 해시를 사용한 집합과 맵, 정렬

### 제출 일자

2024년 8월 12일 20:48:40
2024년 10월 27일 17:53:00

### 문제 설명

Expand Down
56 changes: 21 additions & 35 deletions 백준/Silver/1920. 수 찾기/수 찾기.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@

"""
[눈여겨볼 것]
Q. 왜 이진탐색을 써야하는가?
A. N=10^5, O(N^2)의 경우 100억 연산, 즉 100초가 걸린다.
이에 O(N*logN)으로, 탐색 연산수를 줄여줘야한다.
[고도화 필요]
- 이진트리구조를 이용하고 싶었으나, 실패
- 우선 분할 정복으로 풀이
"""

import sys
def input():
return sys.stdin.readline().rstrip()

N = int(input())
A = list(map(int, input().split()))
A.sort()
# 팁) 대소 규칙을 넣어, 좌/우 편하게 버리기 위함
# - 순간 A 기존 순서가 바뀌는 단점이 있다고 생각했는데, B를 순회하니깐 노 상관
nums = list(map(int, input().split()))
nums.sort()

M = int(input())
B = list(map(int, input().split()))
targets = map(int, input().split())


def binary_search(target, start_idx, end_idx):
if start_idx == end_idx:
if A[start_idx] == target:
return True
else:
return False
def check_is_in(target: int):
l = 0
r = len(nums) - 1

while l <= r:
mid = (l + r) // 2

mid_idx = (start_idx + end_idx) // 2
mid_item = A[mid_idx]
if nums[mid] == target:
return 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1

if target <= mid_item: # 좌 탐색
return binary_search(target, start_idx, mid_idx)
else: # 우 탐색
return binary_search(target, mid_idx + 1, end_idx)
return 0


size = len(A)
for item in B:
if binary_search(item, 0, size - 1):
print(1)
else:
print(0)
for target in targets: # O(10^5)
print(check_is_in(target)) # O(log(10^5)) 으로 줄여야함

0 comments on commit 6185249

Please sign in to comment.