๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90

TIL51

[TIL] CSAPP 9์žฅ ๊ณต๋ถ€2 - ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น 1. ์•„์นจ ๋ฌธ์ œ ํ’€์ด 1. 1764๋ฒˆ - ๋“ฃ๋ณด์žก import sys N, M = map(int, sys.stdin.readline().split()) dic = {} #๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ for _ in range(N) : dic[sys.stdin.readline().strip()] = 1 count = 0 result = [] for _ in range(M) : b = sys.stdin.readline().strip() #๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ if b in dic : # ๋”•์…”๋„ˆ๋ฆฌ์— ํ‚ค๊ฐ’์œผ๋กœ ์กด์žฌํ•˜๋ฉด #๋“ฃ๋„๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์ด๋‹ค. count += 1 result.append(b) print(count) result.sort() for i in range(count) : print(result[i]) ์ „์— ํ’€์—ˆ๋˜.. 2023. 5. 12.
[TIL] CSAPP 9์žฅ ๊ณต๋ถ€ - ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ๊ฐœ๋… ์ •๋ฆฌ 1. ์•„์นจ ๋ฌธ์ œ ํ’€์ด 1. 2563๋ฒˆ - ์ƒ‰์ข…์ด import sys n = int(sys.stdin.readline()) array = [list(map(int, sys.stdin.readline().split())) for _ in range(n)] canvas = [[0]*100 for _ in range(100)] result = 0 for i in range(n) : for j in range(array[i][0], array[i][0]+10) : for k in range(array[i][1], array[i][1]+10) : if canvas[j][k] == 0 : canvas[j][k] = 1 result += 1 print(result) ์ฒ˜์Œ์—๋Š” ์ƒ‰์ข…์ด์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ ๊ฒน์น˜๋Š”.. 2023. 5. 11.
[TIL] RB Tree ๊ตฌํ˜„ (with C) 1. RB Tree ์ƒ์„ฑํ•˜๊ธฐ RB Tree ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒ์„ฑ - ์—ฌ๋Ÿฌ ๊ฐœ์˜ RB Tree๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. sentinel node(๊ฒฝ๊ณ„๋…ธ๋“œ) ์‚ฌ์šฉ - ๋ชจ๋“  nil ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ //RB Tree ๊ตฌ์กฐ์ฒด ์ƒ์„ฑ rbtree *new_rbtree(void) { rbtree *p = (rbtree *)calloc(1, sizeof(rbtree)); //ํ˜„์žฌ ํŠธ๋ฆฌ์—์„œ NIL๋…ธ๋“œ๊ฐ€ ๋  ๋…ธ๋“œ ์ƒ์„ฑ //๊ฐ ํŠธ๋ฆฌ๋งˆ๋‹ค NIL๋…ธ๋“œ๋Š” 1๊ฐœ์”ฉ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. node_t *NIL = (node_t *)calloc(1, sizeof(node_t)); NIL->color = RBTREE_BLACK; p->root = NIL; p->nil = NIL; return p; } 2. RB Tree ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œํ•˜๊ธฐ RB Tre.. 2023. 5. 10.
[TIL] ๊ธฐ๊ณ„ ์ˆ˜์ค€ ํ‘œํ˜„3 + RB Tree ์‚ฝ์ž… ๊ตฌํ˜„(์ฝ”๋“œX) 1. ๊ธฐ๊ณ„ ์ˆ˜์ค€ ํ‘œํ˜„ 1. ์œ ํšจ ์ฃผ์†Œ ์ ์žฌ(Load Effective Address) leaq S, D = D ← &S : ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋ ˆ์ง€์Šคํ„ฐ๋กœ ์ฝ์–ด๋“ค์ด๋Š” ์ธ์ŠคํŠธ๋Ÿญ์…˜์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ–์ง€๋งŒ, ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ „ํ˜€ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฒซ๋ฒˆ์งธ ์˜คํผ๋žœ๋“œ(S)์˜ ์œ ํšจ ์ฃผ์†Œ๋ฅผ ๋ชฉ์ ์ง€(D)์— ๋ณต์‚ฌํ•œ๋‹ค. ํฌ์ธํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ์ผ๋ฐ˜์ ์ธ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ์˜ˆ์‹œ ) x in %rdi, y in %rsi leaq (%rdi, %rsi, 4), %rdi //x = x + 4*y 2. ํ”„๋กœ์‹œ์ €(Procedure) procedure call ์€ ์†Œํ”„์›จ์–ด์—์„œ์˜ ์ฃผ์š” ์ถ”์ƒํ™” : ์ง€์ •๋œ ์ธ์ž๋“ค๊ณผ ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ํŠน์ • ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ์‹ธ์ฃผ๋Š” ๋ฐฉ๋ฒ• ์ œ๊ณต ๋ฌด์Šจ ๊ฐ’์ด ๊ณ„์‚ฐ๋˜๊ณ , ์ด ํ”„๋กœ์‹œ์ €๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์ƒํƒœ์—์„œ ๋ฌด์Šจ ํšจ๊ณผ๋ฅผ.. 2023. 5. 8.
[TIL] Make๋ž€? + stack ๋ฌธ์ œ ํ’€์ด 1. Make ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์„ ์œ„ํ•ด ์œ ๋‹‰์Šค ๊ณ„์—ด ์šด์˜ ์ฒด์ œ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋นŒ๋“œ ๋„๊ตฌ ์—ฌ๋Ÿฌ ํŒŒ์ผ๋“ค๋ผ๋ฆฌ์˜ ์˜์กด์„ฑ๊ณผ ๊ฐ ํŒŒ์ผ์— ํ•„์š”ํ•œ ๋ช…๋ น์„ ์ •์˜ํ•จ์œผ๋กœ์จ ํ”„๋กœ๊ทธ๋žจ์„ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ตœ์ข… ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ณผ์ •์„ ์„œ์ˆ ํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œ์ค€์ ์ธ ๋ฌธ๋ฒ•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. Makefile : make๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ํ”„๋กœ์ ํŠธ์˜ ๋ชฉ๋ก ๋ฐ ์ปดํŒŒ์ผ ๋ฐ ๋งํฌ ๊ทœ์น™ 1. Makefile ์ด ํ•„์š”ํ•œ ์ด์œ  ๋ฐ˜๋ณต๋œ๋Š” ์ปดํŒŒ์ผ ์ž‘์—… ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค. ์ˆ˜์ •๋œ ํŒŒ์ผ๋งŒ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ๋‚˜ ๊ณต๋™ ํ”„๋กœ์ ํŠธ์—์„œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค. 2. ์ปดํŒŒ์ผ ๊ณผ์ • ์†Œ์Šค ํŒŒ์ผ(*.c) : ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ ๋ชฉ์ ํŒŒ์ผ(*.o) : gcc compiler ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑ → ๊ธฐ๊ณ„์–ด ์ƒ์„ฑ ์‹คํ–‰ํŒŒ์ผ(*.out) : gcc compi.. 2023. 5. 7.
[TIL] Red-Black Tree ๊ฐœ๋…(์‚ฝ์ž…/์‚ญ์ œ) + ๊ธฐ๊ณ„ ์ˆ˜์ค€ ํ‘œํ˜„(์Šคํƒ)2 1. ๋ฌธ์ œ ํ’€์ด 1. 1874๋ฒˆ - ์Šคํƒ ์ˆ˜์—ด import sys n = int(sys.stdin.readline()) stack = [] result = '' idx = 1 flag = True for i in range(n) : num = int(sys.stdin.readline()) for j in range(idx, num+1): stack.append(j) result += '+\n' idx += 1 if num == stack[-1] : stack.pop() result += '-\n' else : flag = False break if flag : print(result) else : print('NO') ์Šคํƒ ๋ฌธ์ œ ํ’€์ด ํ’€์ด 1. ํ˜„์žฌ ์ž…๋ ฅ ๋ฐ›์€ ์ˆ˜๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ทธ ์ˆ˜๊นŒ์ง€ +, stack์— .. 2023. 5. 6.
[TIL] LinkedList & AVL Tree (with C) 1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ(Linked List) ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ํ•œ ์ค„๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ์ข…๋ฅ˜ ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ฐ ๋…ธ๋“œ์— data ๊ณต๊ฐ„๊ณผ ํ•œ๊ฐœ์˜ ํฌ์ธํ„ฐ ๊ณต๊ฐ„ ์กด์žฌ ํฌ์ธํ„ฐ๋Š” ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋‘๊ฐœ์˜ ํฌ์ธํ„ฐ ๊ณต๊ฐ„ ์กด์žฌ ํฌ์ธํ„ฐ๋Š” ํ˜„์žฌ ๋…ธ๋“œ์˜ ์•ž์˜ ๋…ธ๋“œ์™€ ๋’ค์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์ผ๋ฐ˜์ ์ธ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์™€ ์ฒ˜์Œ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ ์›ํ˜•์œผ๋กœ ๋งŒ๋“  ๊ตฌ์กฐ โ˜… ๋‹จ์ผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ(C์–ธ์–ด) โ˜… 1. header file - linkedlist_node.h #pragma once // ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ typedef struct node { int data; // ๊ฐ’ ์ €์žฅ struct node.. 2023. 5. 5.
[TIL] C์–ธ์–ด ๊ณต๋ถ€ํ•˜๊ธฐ 1. ์ฝ”๋“œ refactoring 1. 12891๋ฒˆ - DNA ๋น„๋ฐ€๋ฒˆํ˜ธ import sys s, p = map(int, sys.stdin.readline().split()) dna = list(sys.stdin.readline().strip()) # A, C, G, T esssential = list(map(int, sys.stdin.readline().split())) def remove(st) : global a, c, g, t if dna[st] == 'A' : a -= 1 elif dna[st] == 'C' : c -= 1 elif dna[st] == 'G' : g -= 1 elif dna[st] == 'T' : t -= 1 return 0 def add(e) : global a, c, g, t i.. 2023. 5. 4.
[TIL] ๋ฌธ์ œ ํ’€์ด ์—ฐ์Šต - two point (with python) 1. ๋ฌธ์ œ ํ’€์ด ์—ฐ์Šต 1. 2018๋ฒˆ - ์ˆ˜๋“ค์˜ ํ•ฉ import sys N = int(sys.stdin.readline()) start = 1 # ํ˜„์žฌ ๋”ํ•œ ๊ฐ’ ์ค‘ ์ฒซ๋ฒˆ์งธ ๊ฐ’ end = 1 # ํ˜„์žฌ ๋”ํ•œ ๊ฐ’ ์ค‘ ๋งˆ์ง€๋ง‰ ๊ฐ’ count = 1 # ์ž๊ธฐ ์ž์‹  ํฌํ•จ sum = 1 # ์ด ๋”ํ•œ ๊ฐ’ # ์ตœ์†Œ 2๊ฐœ์˜ ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ # start๋Š” N์˜ ์ ˆ๋ฐ˜๋ณด๋‹ค ํด ์ˆ˜ ์—†๋‹ค. while start != N // 2 + 1 : if sum < N : # ์•„์ง ๋ˆ„์ ํ•ฉ์ด N๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ end += 1 sum += end elif sum == N : # N๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ count += 1 sum -= start start += 1 else : # N๋ณด๋‹ค ํด ๊ฒฝ์šฐ sum -= start start += 1.. 2023. 5. 3.
[TIL] ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ ํ’€์ด3 1. ๋ฌธ์ œ ํ’€์ด ์—ฐ์Šต 1. 11660๋ฒˆ - ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5 import sys N, M = map(int, sys.stdin.readline().split()) table = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] # ๋ˆ„์ ํ•ฉ์„ ์ €์žฅํ•  ์ด์ฐจ์› ๋ฐฐ์—ด dp = [[0] * (N+1) for _ in range(N+1)] for i in range(1, N+1) : for j in range(1, N+1) : dp[i][j] = table[i-1][j-1] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] # ์›ํ•˜๋Š” ๊ตฌ๊ฐ„ํ•ฉ ๊ตฌํ•˜๊ธฐ for _ in range(M) : x1,y1,x2,y2 = map(int, s.. 2023. 5. 2.
[TIL] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ ํ’€์ด + ๊ธฐ๊ณ„ ์ˆ˜์ค€ ํ‘œํ˜„1 1. ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ ํ’€์ด 1. 1931๋ฒˆ - ํšŒ์˜์‹ค ๋ฐฐ์ • import sys N = int(sys.stdin.readline()) # meeting[0] : ์‹œ์ž‘ ์‹œ๊ฐ„ / meeting[1] : ๋๋‚˜๋Š” ์‹œ๊ฐ„ meeting = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] meeting.sort(key= lambda x : (x[1], x[0])) count = 1 prev = meeting[0][1] # ์•ž ํšŒ์˜ ๋๋‚˜๋Š” ์‹œ๊ฐ„ for i in range(1, N) : # ๋งŒ์•ฝ ํ˜„์žฌ ํšŒ์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ์•ž ํšŒ์˜ ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด if prev people[i][1] : count += 1 interview = people[i][1] pr.. 2023. 5. 1.
[TIL] Algorithm - Greedy ๊ธฐ๋ณธ ๋ฌธ์ œ ํ’€์ด 1. ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (ํƒ์š• ์•Œ๊ณ ๋ฆฌ์ฆ˜) ํ•ญ์ƒ ๊ฐ ๋‹จ๊ณ„์— ์žˆ์–ด์„œ ๊ฐ€์žฅ ์ข‹์„ ๊ฑฐ๋ผ ์ƒ๊ฐ๋˜๋Š” ์„ ํƒ์„ ํ•œ๋‹ค. ์ด ์„ ํƒ์ด ์ „์ฒด์ ์œผ๋กœ ์ตœ์ ํ•ด๋กœ ์•ˆ๋‚ดํ•˜๊ฒŒ ๋  ๊ฑฐ๋ผ๋Š” ๋ฐ”๋žŒ์„ ๊ฐ€์ง€๊ณ  ๋ถ€๋ถ„์ ์œผ๋กœ ์ตœ์ ์ธ ์„ ํƒ์„ ํ•œ๋‹ค. ์–ธ์ œ๋‚˜ ์ตœ์ ์˜ ํ•ด๋‹ต์„ ๊ตฌํ•˜์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ ๋งŽ์€ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๋ฅผ ๊ตฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 1. 11047๋ฒˆ - ๋™์ „ 0 import sys N, K = map(int, sys.stdin.readline().split()) # ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. coin = [int(sys.stdin.readline()) for _ in range(N)] count = 0 # ๋™์ „์˜ ๊ฐ€์น˜๊ฐ€ ๊ฐ€์žฅ ํฐ ๋™์ „๋ถ€ํ„ฐ ๋ฐ˜๋ณต ์‹œ์ž‘ for i in range(N-1, -1, -1) : if K == 0 : break # ํ˜„์žฌ K๋ณด๋‹ค ์ž‘์€ ์ˆ˜.. 2023. 4. 30.
[TIL] ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ ํ’€์ด2 1. 11049๋ฒˆ - ํ–‰๋ ฌ ๊ณฑ์…ˆ ์ˆœ์„œ 1. ์ตœ์  ๊ด„ํ˜ธ ๋ฌถ๊ธฐ์˜ ๊ตฌ์กฐ : n๊ฐœ์˜ ํ–‰๋ ฌ์„ ์–ด๋–ป๊ฒŒ ๊ณฑํ• ์ง€ ์• ๋งคํ•จ์„ ์—†์• ๊ธฐ ์œ„ํ•ด ๊ด„ํ˜ธ๋กœ ๋ฌถ๋Š”๋‹ค. ex) ((A1, A2), A3) or (A1, (A2, A3)) ์ด ๋ฌธ์ œ๋Š” n๊ฐœ์˜ ํ–‰๋ ฌ๋“ค์˜ ์ฒด์ธ ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ A1, A2, ... , An์˜ ์Šค์นผ๋ผ ๊ณฑ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•˜๋„๋ก ๊ด„ํ˜ธ๋ฅผ ๋ฌถ์–ด๋ผ! ๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. AiAi+1...Aj๋ฅผ ๊ด„ํ˜ธํ˜ธ ์ตœ์ ์œผ๋กœ ๋ฌถ๊ธฐ ์œ„ํ•ด Ak Ak+1์˜ ์‚ฌ์ด์—์„œ ๋‚˜๋ˆˆ๋‹ค๊ณ  ๊ฐ€์ • AiAi+1...Ak Ak+1Ak+2...Aj ์œ„์˜ ๋‘ ๋ถ€๋ถ„๋ฌธ์ œ๋Š” ๊ฐ๊ฐ ์ตœ์  ๊ด„ํ˜ธ ๋ฌถ๋Š” ๋ฐฉ๋ฒ•์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. 2. ์žฌ๊ท€ํ•ด i == j ์ผ ๊ฒฝ์šฐ 0 i < j ์ผ ๊ฒฝ์šฐ → k, k+1๋กœ ๋‚˜๋ˆˆ๋‹ค. Ai = pi-1 * pi ๋ผ๋ฉด Ai..kAk+1..j = pi-1 *pk.. 2023. 4. 29.
[TIL] ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ ํ’€์ด 1. DP ๋ฌธ์ œ ํ’€์ด 1. 9084๋ฒˆ - ๋™์ „ import sys T = int(sys.stdin.readline()) for _ in range(T) : N = int(sys.stdin.readline()) coin = list(map(int, sys.stdin.readline().split())) M = int(sys.stdin.readline()) dp = [0] * (M+1) # ๊ฐ๊ฐ์˜ ๊ธˆ์•ก์— ๋Œ€ํ•œ ๋™์ „์œผ๋กœ ๋งŒ๋“ค ๋ชจ๋“  ๋ฐฉ๋ฒ•์˜ ์ˆ˜ ์ €์žฅ dp[0] = 1 # ๊ธฐ์ € ๊ฐ’์œผ๋กœ ์ง€์ • for c in coin : for i in range(1, M+1) : if i >= c : dp[i] += dp[i-c] print(dp[M]) ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ € ๋ฐฉ์‹์„ ์ƒ๊ฐํ•ด๋‚ด๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ํž˜๋“ค.. 2023. 4. 28.
[TIL] Algorithm - ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ณธ 1. ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ํ’€์ด 1. 1388๋ฒˆ - ๋ฐ”๋‹ฅ ์žฅ์‹ import sys N, M = map(int, sys.stdin.readline().split()) graph = [list(sys.stdin.readline().strip()) for _ in range(N)] visited = [[0] * M for _ in range(N)] # ๊ฐ™์€ ํ–‰์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•˜๋ฏ€๋กœ ์—ด ๋ฒˆํ˜ธ ์ฆ๊ฐ€ def col(x, y) : visited[x][y] = 1 stack = [(x, y)] while stack : curx, cury = stack.pop() for i in range(1, M) : ny = cury + i if ny < M and graph[curx][ny] == '-' and visited[curx.. 2023. 4. 27.
[TIL] ์œ„์ƒ ์ •๋ ฌ ๋ฌธ์ œ ํ’€์ด + Computer System2 1. ์œ„์ƒ ์ •๋ ฌ ๋ฌธ์ œ ํ’€์ด 1. 2637๋ฒˆ - ์žฅ๋‚œ๊ฐ ์กฐ๋ฆฝ import sys from collections import deque N = int(sys.stdin.readline()) M = int(sys.stdin.readline()) basic = [[] for _ in range(N)] inDegree = [0] * N for _ in range(M) : x, y, k = map(int, sys.stdin.readline().split()) basic[y-1].append((x-1, k)) inDegree[x-1] += 1 def topologicalsort() : # component[x][y] -> x๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ y์˜ ๊ฐœ์ˆ˜ component = [[0] * N for _ in range.. 2023. 4. 26.
[TIL] Algorithm - ์œ„์ƒ ์ •๋ ฌ + ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ 1. ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ํ’€์ด 1. 2573๋ฒˆ - ๋น™์‚ฐ โ–ถ BFS๋ฅผ ์ด์šฉํ•œ ํ’€์ด import sys from collections import deque N, M = map(int, sys.stdin.readline().split()) iceberg = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] # ๋น™์‚ฐ ๋…น์ด๋Š” ํ•จ์ˆ˜ def melting() : change = [] for i in range(1, N-1) : for j in range(1, M-1) : if iceberg[i][j] != 0 : sea = 0 # ํ˜„์žฌ ์œ„์น˜์—์„œ 0๊ณผ ๋งž๋‹ฟ์•„ ์žˆ๋Š” ๊ฐœ์ˆ˜ for k in ran.. 2023. 4. 25.
[TIL] Algorithm - ๋‹ค์ต์ŠคํŠธ๋ผ + ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ ํ’€์ด(BFS) 1. ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Dijkstra algorithm) ๋ชจ๋“  ๊ฐ„์„ ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๊ฐ€ ํ•ญ์ƒ 0์ด์ƒ์ž„์„ ๊ฐ€์ •ํ•˜๊ณ  ๋‘ ๊ผญ์ง“์  ๊ฐ„์˜ ๊ฐ€์žฅ ์งง์„ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ„์„  ์™„ํ™”(Edge Relaxation) ์‹œ์ž‘ ์ •์  s์—์„œ ๋‹ค๋ฅธ ์ •์  v๊นŒ์ง€์˜ ์ดˆ๊ธฐ ๊ฑฐ๋ฆฌ ๊ฐ’์„ ๋ถ€์—ฌํ•˜๊ณ  ๋‹จ๊ณ„๋ฅผ ๊ฑฐ๋“ญํ•˜์—ฌ ๊ฐœ์„ ์‹œํ‚ค๋Š” ๊ฒƒ ๊ฐ„์„  (u, v)๋ฅผ ์™„ํ™”ํ•˜๋Š” ๊ณผ์ • u๋ฅผ ํ†ตํ•ด v๊นŒ์ง€ ๊ฐ€๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋ฐœ๊ฒฌ๋œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, v์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ์ถ”์ •๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„ O(|E| + |V|log|V|) https://ko.wikipedia.org/wiki/๋ฐ์ดํฌ์ŠคํŠธ๋ผ_์•Œ๊ณ ๋ฆฌ์ฆ˜ 1. ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ•œ ๋ฌธ์ œ ํ’€์ด 1. 1916๋ฒˆ - ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ import sys import heapq N =.. 2023. 4. 24.
728x90