반응형

Development/Algorithm 33

[백준] 1927번: 최소 힙 (python)

www.acmicpc.net/problem/1927 1927번: 최소 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0 www.acmicpc.net import sys import heapq N = int(sys.stdin.readline()) X = [int(sys.stdin.readline()) for _ in range(N)] heap = [] for x in X: if x == 0: if not heap: print(0) else: print(heapq.heappop(heap)) else: heapq.heappush(heap, x)

[백준] 1759번: 암호 만들기 (python)

www.acmicpc.net/problem/1759 1759번: 암호 만들기 첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다. www.acmicpc.net import sys from itertools import combinations def divide_alphabet(alphabet): vowel = [] consonant = [] for a in alphabet: if a in ['a', 'e', 'i', 'o', 'u']: vowel.append(a) else: consonant.append(a) return vowel, consonant def make_su..

[백준] 11279번: 최대 힙 (python)

www.acmicpc.net/problem/11279 11279번: 최대 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 www.acmicpc.net import sys def swap(a,b): tmp = a a = b b = tmp return a, b def insert_max_heap(heap, x): child = len(heap) heap.append(x) while(True): parent = child//2 if heap[parent] < heap[child]: heap[parent], heap[child] = swap(he..

[백준] 2143번: 두 배열의 합 (python)

www.acmicpc.net/problem/2143 2143번: 두 배열의 합 첫째 줄에 T(-1,000,000,000 ≤ T ≤ 1,000,000,000)가 주어진다. 다음 줄에는 n(1 ≤ n ≤ 1,000)이 주어지고, 그 다음 줄에 n개의 정수로 A[1], …, A[n]이 주어진다. 다음 줄에는 m(1≤m≤1,000)이 주어지고, 그 다 www.acmicpc.net import sys def find_sum_dic(num, array): sum_dic = {} for i in range(num): for j in range(i+1,num+1): s = sum(array[i:j]) if s in sum_dic: sum_dic[s] += 1 else: sum_dic[s] = 1 return sum_d..

[백준] 2003번: 수들의 합 2 (python)

www.acmicpc.net/problem/2003 2003번: 수들의 합 2 첫째 줄에 N(1 ≤ N ≤ 10,000), M(1 ≤ M ≤ 300,000,000)이 주어진다. 다음 줄에는 A[1], A[2], …, A[N]이 공백으로 분리되어 주어진다. 각각의 A[x]는 30,000을 넘지 않는 자연수이다. www.acmicpc.net import sys N, M = map(int,sys.stdin.readline().split()) A = list(map(int,sys.stdin.readline().split())) num = 0 idx = 0 q = [] while(idx = N: break q.append(A[idx]) idx += 1 elif sum(q) >= M: if sum(q) == M: ..

[백준] 2096번: 내려가기 (python)

www.acmicpc.net/problem/2096 2096번: 내려가기 첫째 줄에 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 숫자가 세 개씩 주어진다. 숫자는 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 중의 하나가 된다. www.acmicpc.net import sys def use_dp(fun, dp): x1 = board_r[0] + fun(dp[:2]) x2 = board_r[1] + fun(dp) x3 = board_r[2] + fun(dp[1:]) return [x1, x2, x3] if __name__ == '__main__': N = int(sys.stdin.readline()) max_dp = [0]*3 min_dp = [0]*3 for r in range..

[백준] 2748번: 피보나치 수 2 (python)

www.acmicpc.net/problem/2748 2748번: 피보나치 수 2 피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다. 이를 식으로 써보면 Fn = Fn-1 + Fn-2 (n ≥ 2)가 www.acmicpc.net def iter(n, cache): if n < 2: cache[n] = n return cache[n] if cache[n] != -1: return cache[n] cache[n] = iter(n-1, cache) + iter(n-2, cache) return cache[n] def fibonacci(n): cache = [-1 for _ in range(n+1)] ret..

[백준] 1339번: 단어수학 (python)

www.acmicpc.net/problem/1339 1339번: 단어 수학 첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대 www.acmicpc.net def make_score(N, word): score = {} for i in range(N): for j, e in enumerate(word[i]): s = 10**(len(word[i])-j-1) if not e in score: score[e] = s else: score[e] += s return score def cal_max(score): sorted_score = sorted(list(sc..

[백준] 3055번: 탈출 (python)

www.acmicpc.net/problem/3055 3055번: 탈출 사악한 암흑의 군주 이민혁은 드디어 마법 구슬을 손에 넣었고, 그 능력을 실험해보기 위해 근처의 티떱숲에 홍수를 일으키려고 한다. 이 숲에는 고슴도치가 한 마리 살고 있다. 고슴도치는 제 www.acmicpc.net import sys def find_loc(R,C,chart,e='S'): loc = [] for r in range(R): for c in range(C): if chart[r][c] == e: loc.append([r,c]) return loc def move_position(R,C,chart,s_loc): expand = [] goal = False for l in s_loc: lr,lc = l near = [[lr,..

[백준] 1103번: 게임 (python)

www.acmicpc.net/problem/1103 1103번: 게임 줄에 보드의 세로 크기 N과 가로 크기 M이 주어진다. 이 값은 모두 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 보드의 상태가 주어진다. 쓰여 있는 숫자는 1부터 9까지의 자연수 또는 www.acmicpc.net import sys sys.setrecursionlimit(10**6) dr = [0, 1, 0,-1] dc = [1, 0,-1, 0] def dfs(r ,c): global state, visited if not(0

반응형