BOJ/๐Ÿฅˆ36

โ˜…Two-Pointers Upper-Intermediate I - 5 Solvedโ˜… โ˜… 3273 ๋‘ ์ˆ˜์˜ ํ•ฉ โ˜… import sys input=sys.stdin.readline n=int(input()) l=list(map(int,input().split())) X=int(input()) #array l.sort() #two-pointers x,y=0,n-1 ans=0 while x X y-=1 print(ans) ๐Ÿก ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ๋’ค, x๋ฅผ ๋ฐฐ์—ด ์™ผ์ชฝ ๋ / y๋ฅผ ๋ฐฐ์—ด ์˜ค๋ฅธ์ชฝ ๋์— ๋ฐฐ์น˜. l[x]+l[y]๊ฐ€ X๋ณด๋‹ค ํด ๊ฒฝ์šฐ, x๋ฅผ ์•„๋ฌด๋ฆฌ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•ด๋„ X๋ณด๋‹ค ํด ์ˆ˜ ๋ฐ–์— ์—†์Œ. ๋”ฐ๋ผ์„œ y๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ / l[x]+l[y]๊ฐ€ X๋ผ๋ฉด ์ •๋‹ต ์ฐพ์Œ! ans+=1 ํ•œ ๋’ค, l[x]์™€ l[y]๋Š” ์ด์ œ X ๋‚˜์˜ค๋Š” ์—ฐ์‚ฐ์— ํ•„์š” ์—†์œผ๋ฏ€๋กœ x ์˜ค๋ฅธ์ชฝ ์ด๋™, y ์™ผ์ชฝ ์ด๋™ / l[x]+l[y]๊ฐ€.. BOJ/๐Ÿฅˆ 2024. 2. 11.
โ˜…Backtracking Upper-Intermediate I - 9 Solvedโ˜… โ˜… 15663 N๊ณผ M (9) โ˜… import sys input=sys.stdin.readline N,M=map(int,input().split()) nums=list(map(int,input().split())) nums.sort() num,visited=[],[False]*N def track(): if len(num)==M: print(*num) else: last=0 for i in range(len(nums)): if not visited[i] and last!=nums[i]: visited[i]=True num.append(nums[i]) last=nums[i] track() num.pop() visited[i]=False track() โ˜… 15664 N๊ณผ M (10) โ˜… import sys .. BOJ/๐Ÿฅˆ 2024. 1. 20.
โ˜…Backtracking Intermediate I - 10 Solvedโ˜… โ˜… 15649 N๊ณผ M (1) โ˜… N,M=map(int,input().split()) ans=[] def track(): if len(ans) == M: #completed print(*ans) else: for x in range(1,N+1): if len(ans) == 0 or x not in ans: ans.append(x) track() ans.pop() #backtrack track() ๐Ÿ™ƒ backtracking ํฌ์ŠคํŒ… ์ฐธ์กฐ โ˜… 15650 N๊ณผ M (2) โ˜… N,M=map(int,input().split()) ans=[] def track(): if len(ans) == M: #completed print(*ans) else: for x in range(1,N+1): if len(ans) == .. BOJ/๐Ÿฅˆ 2024. 1. 18.
โ˜…Implementation&Simulation Upper-Intermediate I - 3 Solvedโ˜… โ˜… 2564 ๊ฒฝ๋น„์› โ˜… import sys input=sys.stdin.readline w,h=map(int,input().split()) diameter = (w+h)*2 loc=[] ans=0 for _ in range(int(input())): d,l=map(int,input().split()) if d == 3: loc.append(l) elif d == 2: loc.append(l+h) elif d == 4: loc.append(h*2+w-l) else: #d==1 loc.append(h*2+w*2-l) d,l=map(int,input().split()) if d == 3: me = l elif d == 2: me = l+h elif d == 4: me = h*2+w-l else: #d==1 m.. BOJ/๐Ÿฅˆ 2024. 1. 8.
โ˜…Tree Upper-Intermediate I - 3 Solvedโ˜… โ˜… 1991 ํŠธ๋ฆฌ ์ˆœํšŒ โ˜… import sys input=sys.stdin.readline def pre_order(node): print(node, end='') if tree[node][0] != '.': pre_order(tree[node][0]) if tree[node][1] != '.': pre_order(tree[node][1]) def in_order(node): if tree[node][0] != '.': in_order(tree[node][0]) print(node, end='') if tree[node][1] != '.': in_order(tree[node][1]) def post_order(node): if tree[node][0] != '.': post_order(tree[node].. BOJ/๐Ÿฅˆ 2023. 12. 19.
โ˜…BF Upper-Intermediate I - 2 Solvedโ˜… โ˜… 2503 ์ˆซ์ž ์•ผ๊ตฌ โ˜… import sys from itertools import permutations input=sys.stdin.readline N=int(input()) pos=list(permutations(['1','2','3','4','5','6','7','8','9'],3)) idxs=[n for n in range(len(pos))] def get_strike_ball(a,b): strike=0 for x,y in zip(a,b): if x==y: strike+=1 return (str(strike),str(len(a+b)-len(set(a+b))-strike)) for _ in range(N): num,S,B=input().split() didxs=[] for i in idxs: p.. BOJ/๐Ÿฅˆ 2023. 10. 26.
โ˜…Greedy Intermediate II - 4 Solvedโ˜… โ˜… 2012 ๋“ฑ์ˆ˜ ๋งค๊ธฐ๊ธฐ โ˜… import sys input=sys.stdin.readline N=int(input()) ans,i=0,1 for x in sorted([int(input()) for _ in range(N)]): ans+=abs(x-i) i+=1 print(ans) ๐Ÿ’ฒ greedy ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ง๊ด€์ ์œผ๋กœ optimal solution์˜ ๋ชจ์Œ → ์ „์ฒด ์ตœ์  solution์ด๋ผ ์ƒ๊ฐํ•˜๊ณ  ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๐Ÿ’ฒ greedy ๊ด€์  ๋ถ„์„ : ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๊ฐ์ž ์˜ˆ์ƒํ•œ ๋“ฑ์ˆ˜์— ์‹ค์ œ ๋“ฑ์ˆ˜ ๋ถ€์—ฌ, ๊ทธ๋ฆฌ๊ณ  ๋ถˆ๋งŒ๋„ ๊ตฌํ•˜๊ธฐ : ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ถˆ๋งŒ๋„์˜ ํ•ฉ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ถˆ๋งŒ๋„์˜ ํ•ฉ์ด ์ตœ์†Œ โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋จผ์ € ์˜ˆ.. BOJ/๐Ÿฅˆ 2023. 8. 20.
โ˜…Combinatorics Upper-Intermediate I - 4 Solvedโ˜… โ˜… 2697 ๋‹ค์Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ โ˜… import sys input=sys.stdin.readline def next_permutation(n,l): #ex) n = [3,2,1] i = l-2 while i>=0 and n[i]>=n[i+1]: i-=1 if i>=0: j=l-1 while n[j] =0 and n[i] >= n[i+1]: i-=1 if i>=0: j = l-1 while n[j] =0 and l[i]>=l[i+1]: i-=1 if i>=0: j=N-1 while l[j] nK+1=33 if nums>32: print(0) else: ans=[] for three_in_pair in combinations(MBTIs,3): x,y,z=three_in_pair[0],three_in_pair[1].. BOJ/๐Ÿฅˆ 2023. 8. 16.
โ˜…Regular Expression ์ค‘์ƒ๊ธ‰ - 2๋ฌธ์ œ()โ˜… โ˜… ์ •๊ทœํ‘œํ˜„์‹(Regular Expression)์€ ์–ด๋–ป๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ์ง€ ํ•จ์ˆ˜๋ฅผ ์ ์ ˆํžˆ ์‚ฌ์šฉ / ์ •๊ทœํ‘œํ˜„์‹ ๋ฌธ๋ฒ•์„ ์•Œ๋ฉด ๋œ๋‹ค โ˜… 9342 ์—ผ์ƒ‰์ฒด โ˜… import re,sys input=sys.stdin.readline for _ in range(int(input().strip())): chromosome = input().strip() if re.match('^[ABCDEF]?A+F+C+[A,B,C,D,E,F]?$',chromosome): print('Infected!') else: print('Good') ๐Ÿฆ– ์ •๊ทœํ‘œํ˜„์‹ ๋ฌธ๋ฒ• ์ฐจ๋ก€๋Œ€๋กœ ์ž‘์„ฑํ•˜์ž๋ฉด! โ‘  ๋ฌธ์ž์—ด์€ {A, B, C, D, E, F} ์ค‘ 0๊ฐœ ๋˜๋Š” 1๊ฐœ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค → ^[ABCDEF]? โ‘ก ๊ทธ ๋‹ค์Œ์—๋Š” A๊ฐ€ ํ•˜๋‚˜ ๋˜๋Š” ๊ทธ ์ด์ƒ ์žˆ์–ด์•ผ ํ•œ.. BOJ/๐Ÿฅˆ 2023. 8. 14.
โ˜…Set/Map Upper-Intermediate I - 2 Solvedโ˜… โ˜… 2910 ๋นˆ๋„ ์ •๋ ฌ โ˜… import sys input=sys.stdin.readline N,C=map(int,input().split()) freq=dict() message=list(map(int,input().split())) order=1 for n in message: if n not in freq: freq[n] = [1, order] order += 1 else: freq[n][0] += 1 freq_sorted = sorted(freq.items(), key=lambda x:(-x[1][0],x[1][1])) for x in freq_sorted: for _ in range(x[1][0]): print(str(x[0]),end=' ') ๐Ÿง‘๐Ÿป‍๐ŸŽจ hash table์˜ key๊ฐ’์€ ๊ณ ์œ ํ•œ ์ˆซ์ž.. BOJ/๐Ÿฅˆ 2023. 8. 11.
โ˜…DAC ์ค‘์ƒ๊ธ‰1 - 3๋ฌธ์ œ()โ˜… ๐Ÿด DAC ๋ถ„ํ• ์ •๋ณต ๋ฌธ์ œ๋Š” ์ฃผ๋กœ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ sub-problem์„ ๋Œ๋ฆฌ๊ณ  ์ฐจ๋ก€์ฐจ๋ก€ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ์ค‘์ƒ๊ธ‰ ์ด์ƒ์˜ ๋‚œ์ด๋„๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰ํ•ด๋ณด์ž! ๐Ÿด ์•„๋ž˜ DAC ์ด๋ก  ํฌ์ŠคํŒ… ์ฐธ์กฐ Divide&Conquer(DAC) intro> ๐Ÿป ๋ถ„ํ• (Divide)ํ•˜๊ณ  ์ •๋ณต(Conquer)ํ•˜๋Š” ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ์˜ ์ ‘๊ทผ ๋ฐฉ์‹์œผ๋กœ ๋ถ„ํ•  → ์ •๋ณต → ๊ฒฐํ•ฉ ํฌ๊ฒŒ ์„ธ ๋‹จ๊ณ„๋กœ ์ง„ํ–‰๋œ๋‹ค. ๐Ÿป โ‘  ๋ถ„ํ• (Divide): ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ๋” ์ž‘์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ sub-pr sh-avid-learner.tistory.com โ˜… 2630 ์ƒ‰์ข…์ด ๋งŒ๋“ค๊ธฐ โ˜… import sys input=sys.stdin.readline N=int(input()) W,B=0,0 confetti=[] def is_check(arr).. BOJ/๐Ÿฅˆ 2023. 3. 20.
โ˜…Number Theory Upper-Intermediate I - 8 Solvedโ˜… โ˜… 1850 ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ โ˜…import mathA,B = map(int, input().split())ans = ''.join(['1']*math.gcd(A,B))print(ans) ๐Ÿ‘‘ 1์˜ ๊ฐœ์ˆ˜ A์™€ B์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋งŒํผ 1์ด ๋‚˜์—ด๋˜์–ด ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜. ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ์„ฑ์งˆ / ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์„ ์‚ฌ์šฉํ•ด ์ด๋ฅผ ์ฆ๋ช…ํ•ด๋ณด์ž โ€ป 1์ด a๊ฐœ ์žˆ๋Š” ์ˆ˜์™€ 1์ด b๊ฐœ ์žˆ๋Š” ์ˆ˜์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋Š” 1์ด gcd(a,b)๊ฐœ ์žˆ๋Š” ์ˆ˜ (a≥b)โ€ป - ์ฆ๋ช… - → 1์ด a๊ฐœ ์žˆ๋Š” ์ˆ˜๋ฅผ $S_a$, 1์ด b๊ฐœ ์žˆ๋Š” ์ˆ˜๋ฅผ $S_b$๋ผ๊ณ  ํ•˜๋ฉด$$S_a = \underbrace{111 ... 111}$$(1์ด a๊ฐœ)$$S_b = \underbrace{111 ... 111}$$(1์ด b๊ฐœ) → ์•„๋ž˜์™€ ๊ฐ™.. BOJ/๐Ÿฅˆ 2023. 2. 28.