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

Algorithm

(120)
[BOJ] ๋น„์Šทํ•œ ๋‹จ์–ด ํ•ด์„ค ์ฝ”๋“œ N = int(input()) compare = list(input()) ans = 0 for _ in range(N-1): compare_word = compare[:] check_word = list(input()) for _ in range(len(check_word)): a = check_word.pop(0) if a in compare_word: compare_word.remove(a) else: check_word.append(a) A = len(compare_word) B = len(check_word) if (A==0 and B==0) or (A==1 and B==0) or (A==0 and B==1) or (A==1 and B==1): ans += 1 print(ans) ๋ถ„์„ ..
[Leetcode] Group Anagrams ํ•ด์„ค ์ฝ”๋“œ class Solution(object): def groupAnagrams(self, strs): d = {} for str in strs: key = ''.join(sorted(str)) if key not in d: d[key] = [str] else: d[key].append(str) return d.values() ๋ถ„์„ > ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, value๊ฐ’์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜์–ด์„œ ๊ณ„์† ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์€ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ํ•ด์‹œ๋งต์˜ key๊ฐ’์—๋Š” ์ด๋“ค์„ ์ •๋ ฌํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“  ๊ฒƒ์„ ๋„ฃ๊ณ , value๊ฐ’์—๋Š” ๊ฐ™์€ key๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๋“ค์„ ๋ฐฐ์—ด๋กœ appendํ•˜๋ฉด ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ•ด์‹œ๋งต์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ๋‹ค. Group Anagrams - LeetCod..
์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋”” 4์ฃผ์ฐจ 1. 2016๋…„ def solution(a, b): date = ['FRI','SAT','SUN','MON','TUE','WED','THU'] month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] cnt = 0 for i in range(a-1): cnt += month[i] ans = (cnt + b) % 7 return date[ans-1] 2. ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ def solution(s): if len(s) == 4 and s.isdigit(): return True elif len(s) == 6 and s.isdigit(): return True else: return False 3. ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ def solution(s): s..
์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋”” 3์ฃผ์ฐจ 1. ๋‹คํŠธ๊ฒŒ์ž„ def solution(dart): ans = [] for i in range(len(dart)): if dart[i] =='S': if dart[i-1] == '0' and dart[i-2] == '1': ans.append(10) else: ans.append(int(dart[i-1])) elif dart[i] == 'D': if dart[i-1] == '0' and dart[i-2] == '1': ans.append(10**2) else: ans.append(int(dart[i-1])**2) elif dart[i] == 'T': if dart[i-1] == '0' and dart[i-2] == '1': ans.append(10**3) else: ans.append(int(dart[i-..
[BOJ] ๋žœ์„  ์ž๋ฅด๊ธฐ ๋‚˜์˜ ์ฝ”๋“œ a,b = map(int,input().split()) arr = [int(input()) for _ in range(a)] k = 1 ans = [] while True: imp = [] for num in arr: t = num // k imp.append(t) k += 1 if sum(imp) == b: break while True: ans = [] k += 1 for num in arr: ans.append(num//k) if ans != imp: break print(k-1) ๋ถ„์„ > ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚  ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ํšจ์œจ์„ฑ์„ ์ œ์™ธํ•˜๊ณ  ์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๊ธฐ ์œ„ํ•ด์„œ ํ’€์–ด๋ณด์•˜๋‹ค. ๋‹น์—ฐํžˆ ๊ฒฐ๊ณผ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ. ํ•ด์„ค ์ฝ”๋“œ a,b = map(int,input().split()) arr = [in..
[BOJ] ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด ์ฝ”๋“œ t = int(input()) num = list(map(int,input().split())) dp = [1] * t for i in range(t): for j in range(i): if num[i] > num[j]: dp[i] = max(dp[i],dp[j]+1) # ํ˜„์žฌ์˜ dp๊ฐ’๊ณผ ์ด์ „ ์ˆ˜์˜ dp๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๋น„๊ต ํ›„ ๋” ํฌ๋ฉด ๊ฐฑ์‹  print(max(dp)) ๋ถ„์„ > LIS(Longest Increasing Subsequence) ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์›๋ฆฌ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ํ˜„์žฌ์˜ ์ˆ˜ (num[i]) ๊ฐ€ ๋น„๊ต๋Œ€์ƒ์ด ๋˜๋Š” ์ด์ „์˜ ์ˆ˜ (num[j]) ๋ณด๋‹ค ํด ๋•Œ๋งŒ, ๋น„๊ต ๋Œ€์ƒ์ด ๋˜๋Š” ์ˆ˜์˜ dp๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ์ด์ „์˜ ์ˆ˜๋“ค์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋ฉด, ์ตœ์ข…์ ์œผ๋กœ ํ˜„์žฌ ์ˆ˜์˜ dp๊ฐ’์€ ์ด..
[BOJ] ์˜ฌ๋ฆผํ”ฝ ์ฝ”๋“œ a,b = map(int,input().split()) world = [] for _ in range(a): num = list(map(int,input().split())) world.append(num) world.sort(key = lambda x: (x[1],x[2],x[3]),reverse=True) for i in range(len(world)): if world[i][0] == b: index = i for i in range(len(world)): if world[index][1:] == world[i][1:]: print(i+1) break ๋ถ„์„ > ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜๋Š”, ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์ค€์„ ์ •ํ•ด์„œ ์ •๋ ฌ์„ ํ•  ๋•Œ (x[1],x[2],x[3]) ์™€ ๊ฐ™์ด ๋žŒ๋‹คํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด..
[BOJ] ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋ฌธ์ œ ๋‘ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์— ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์–ด๋–ค ๋ฌธ์ž์—ด s์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด t๋ž€, s์— t๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด ABRACADABRA์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ABRA, RAC, D, ACADABRA, ABRACADABRA, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด๋‹ค. ํ•˜์ง€๋งŒ, ABRC, RAA, BA, K๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋‹ค. ๋‘ ๋ฌธ์ž์—ด ABRACADABRA์™€ ECADADABRBCRDARA์˜ ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ CA, CADA, ADABR, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด ์žˆ๋‹ค. ์ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ADABR์ด๋ฉฐ, ๊ธธ์ด๋Š” 5์ด๋‹ค. ๋˜, ๋‘ ๋ฌธ์ž์—ด์ด UPWJCIRUCAXIIRGL์™€ SBQNYBSBZDFNEV์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๊ธด ๊ณต..