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

๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ

(159)
[Programmers] ๊ตฌ๋ช…๋ณดํŠธ ํ•ด์„ค ์ฝ”๋“œ def solution(people, limit): people.sort() i = 0 j = len(people) - 1 cnt = 0 while i
[Programmers] ์†Œ์ˆ˜ ์ฐพ๊ธฐ ํ•ด์„ค ์ฝ”๋“œ def solution(n): num = set(range(2,n+1)) for i in range(2,n+1): if i in num: num -= set(range(i*2,n+1,i)) return len(num) ๋ถ„์„ > set ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋จผ์ € ๋ฒ”์œ„ ๋‚ด์˜ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  i์˜ ๋ฐฐ์ˆ˜์ธ ์ง‘ํ•ฉ์„ ๋นผ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•˜์˜€๋‹ค. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์†Œ์ˆ˜ ์ฐพ๊ธฐ 1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž n ์‚ฌ์ด์— ์žˆ๋Š” ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ์†Œ์ˆ˜๋Š” 1๊ณผ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ๋งŒ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. (1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.) ์ œํ•œ ์กฐ๊ฑด n์€ 2์ด์ƒ programmers.co.kr
[Programmers] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ ์ฝ”๋“œ def keypad(current_N,next_N): dic = {'1':[0,0],'2':[0,1],'3':[0,2],'4':[1,0],'5':[1,1],'6':[1,2],'7':[2,0],'8':[2,1],'9':[2,2],'*':[3,0],'0':[3,1],'#':[3,2]} x1,y1 = dic[current_N] x2,y2 = dic[next_N] return abs(int(x1) - int(x2)) + abs(int(y1) - int(y2)) def solution(numbers,hand): ans = '' left = '*' right = '#' for i in numbers: if i in [1,4,7]: left = str(i) ans += 'L' elif i in [3,6,9]: ..
[Programmers] ์œ„์žฅ ์ฝ”๋“œ def solution(arr): dict = {} for i in range(len(arr)): if arr[i][1] not in dict: dict[arr[i][1]] = [arr[i][0]] else: dict[arr[i][1]].append(arr[i][0]) k = list(dict.values()) cnt = 1 for i in k: cnt *= len(i) + 1 return cnt - 1 ๋ถ„์„ > ํ•ด์‹œ๋งต์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๊นŒ์ง€๋Š” ํ–ˆ๋Š”๋ฐ, ์กฐํ•ฉ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์—์„œ ๋ง‰ํ˜”๋˜ ๋ฌธ์ œ์˜€๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌ๊ธ€๋ง์œผ๋กœ ์•„์ด๋””์–ด๋ฅผ ์–ป์—ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ๋ฐฐ์—ด[[2,3,4],[1,0]] ์ด ์žˆ๋‹ค๋ฉด ์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด ๋‚ด์—์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ์กฐํ•ฉ์˜ ์ˆ˜๋Š” ์ด๋ ‡๊ฒŒ ..
[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-..