Baekjoon 150

[백준 24416번] 알고리즘 수업 - 피보나치 수 1

n = int(input()) dp = [0]*41 count_r = 0 count_d = 0 def fib_recursive(n): # 일반적인 재귀함수 피보나치 수열 계산 global count_r if n==1 or n==2: count_r += 1 return 1 else: return fib_recursive(n-1) + fib_recursive(n-2) def fib_dynamic(n): # 동적계획법을 사용한 피보나치 수열 계산 global count_d if n==1 or n==2: if dp[n] == 0: dp[n] = 1 return dp[n] else: if dp[n] != 0: return dp[n] else: count_d += 1 dp[n] = fib_dynamic(n-1) +..

Algorithm/DP 2023.02.28

[백준 13305번] 주유소

import sys input = sys.stdin.readline # n 입력받기 n = int(input()) # 총 비용 total = 0 # n-1개의 거리를 저장하는 리스트 distance = list(map(int,input().split())) # 주유소의 리터당 가격을 저장하는 리스트 price = list(map(int,input().split())) # 남은 거리 수 remain = sum(distance) # 현재 최소 거리 min_price = price[0] # 현재 누적된 거리 sum_distance = distance[0] for i in range(n-1): if i == n-2: total += (sum_distance * min_price) break if min_price

Algorithm/Greedy 2023.02.24

[백준 2108번] 통계학

import sys input = sys.stdin.readline # n 입력받기 n = int(input()) # n개의 값을 저장하는 리스트 array = [0]*n # 빈도수를 저장하는 리스트 count_list = [] # 최빈값 mode = 0 # 입력받기 for i in range(n): array[i] = int(input()) # 중간 인덱스 mid_index = n//2 # 오름차순 정렬 array.sort() # 집합 자료형 사용 set_n = set(array) # 사전 자료형 사용 dictionary = dict() # 사전 자료형에 숫자 '값 : 빈도수(카운팅)' 저장 => 시간복잡도 최소화 for i in array: if i not in dictionary: dictionar..

Algorithm/Sorting 2023.02.24

[백준 1764번] 듣보잡 ( + input과 sys.stdin.readline의 차이점)

import sys input = sys.stdin.readline n, m = map(int,input().split()) list_n = [] list_m = [] count = 0 count_list = [] for i in range(n): list_n.append(input().rstrip()) for i in range(m): list_m.append(input().rstrip()) set_n = set(list_n) set_m = set(list_m) for i in set_m: if i in set_n: count += 1 count_list.append(i) count_list.sort() print(count) for i in count_list: print(i) 이번 문제는 두 개의 ..

Algorithm/Sorting 2023.02.23

[백준 10815번] 숫자 카드

1. 이진 탐색을 활용하여 푼 방법 (1856ms) n = int(input()) array_n = sorted(list(map(int,input().split()))) m = int(input()) array_m = list(map(int,input().split())) def binary_search(array,target,start,end): while start target: end = mid - 1 else: start = mid + 1 return 0 for i in array_m: print(binary_search(array_n,i,0,n-1),end=" ") 2. 집합(Set) 자료형을 사용하여 푼 방법 (664ms) n = int(input()) array_n = list(map(int..

[백준 1920번] 수 찾기(이진 탐색)

import sys input = sys.stdin.readline n = int(input()) array_n = sorted(list(map(int,input().split()))) m = int(input()) array_m = list(map(int,input().split())) def binary_search(array,target,start,end): while start target: end = mid - 1 else: start = mid + 1 return 0 for i in array_m: print(binary_search(array_n,i,0,n-1)) 이번 문제는 이전에 정렬 알고리즘에서 다루었지만, 출제자가 원하는 것은 이분 탐색 알고리즘을 사용하여 푸는 것이고 무엇보다 이진탐..

[백준 1920번] 수 찾기

import sys input = sys.stdin.readline n = int(input()) # List의 in은 평균 시간복잡도가 O(N)이고, Set의 in은 평균 시간복잡도가 O(1)이다 # (Set은 해쉬 테이블을 사용하기 때문) set_n = set(map(int,input().split())) m = int(input()) array_m = list(map(int,input().split())) for i in array_m: if i in set_n: print(1) else: print(0) 이번 문제는 이진 탐색을 사용하지 않고 풀이하려고 했었는데, n개의 데이터가 들어있는 리스트(List)로 저장하고 in 연산자를 사용하니 시간초과가 발생했다. 따라서 이를 해결하기 위해 in 연산..

Algorithm/Sorting 2023.02.21