본문 바로가기

Coding Test/Programmers

(122)
[프로그래머스/파이썬] 방문 길이 나의 풀이 def solution(dirs): direction = {'L': (-1, 0), 'U': (0, -1), 'R': (1, 0), 'D': (0, 1)} visited = set() x, y = 0, 0 for d in dirs: nx, ny = x + direction[d][0], y + direction[d][1] if -5
[프로그래머스/파이썬] 쿼드압축 후 개수 세기 나의 풀이 def solution(arr): answer = [0, 0] n, k = len(arr), 1 while n > 0: for x in range(k): for y in range(k): nx, ny = n * x, n * y temp = arr[ny][nx] if temp == -1: continue for i in range(nx, nx + n): for j in range(ny, ny + n): if temp != arr[j][i]: break else: continue break else: for i in range(nx, nx + n): for j in range(ny, ny + n): arr[j][i] = -1 answer[temp] += 1 n //= 2 k *= 2 return ..
[프로그래머스/파이썬] n^2 배열 자르기 나의 풀이 def solution(n, left, right): array = [] for i in range(left, right + 1): array.append(max(i // n, i % n) + 1) return array divmod를 이용한 풀이 def solution(n, left, right): answer = [] for i in range(left, right+1): q, r = divmod(i, n) answer.append(max(q, r) + 1) return answer
[프로그래머스/파이썬] 3 x n 타일링 나의 풀이 def solution(n): if n % 2: return 0 k = 1000000007 n //= 2 dp = [3, 11] if n > 3: for _ in range(n - 2): dp.append((dp[-1] * 4 - dp[-2] + k) % k) return dp[-1] - 홀수일때 불가하니 0 - 짝수일때만 비교해서 dp로 해결 - 규칙 구하는게 어려웠던 문제 배열 안쓰는 풀이 def solution(n): if n % 2: return 0 front = back = 1 for _ in range(n//2): front, back = back, (4*back - front) % 1000000007 return back
[프로그래머스/파이썬] 점프와 순간 이동 나의 풀이 def solution(n): answer = 0 while n > 0: if n % 2: n -= 1 answer += 1 else: n //= 2 return answer 이진법을 활용한 풀이 def solution(n): return bin(n).count('1')
[프로그래머스/파이썬] 이진 변환 반복하기 나의 풀이 def solution(s): answer = [0, 0] while s != '1': s = list(s) idx = 0 while idx < len(s): if s[idx] == '0': s.pop(idx) answer[1] += 1 else: idx += 1 s = bin(len(s))[2:] answer[0] += 1 return answer 1의 개수를 활용한 풀이 def solution(s): a, b = 0, 0 while s != '1': a += 1 num = s.count('1') b += len(s) - num s = bin(num)[2:] return [a, b]
[프로그래머스/파이썬] [1차] 캐시 나의 풀이 from collections import deque def solution(cacheSize, cities): if not cacheSize: return 5 * len(cities) cache = deque() answer = 0 for c in cities: c = c.lower() if c in cache: cache.remove(c) answer += 1 else: if len(cache) == cacheSize: cache.popleft() answer += 5 cache.append(c) return answer deque의 maxlen을 활용한 풀이 def solution(cacheSize, cities): import collections cache = collections.d..
[프로그래머스/파이썬] 스킬트리 나의 풀이 def solution(skill, skill_trees): skill = list(skill) answer = 0 for tree in skill_trees: stack = [] flag = True for x in tree: if x in skill: for y in skill[0:skill.index(x)]: if y not in stack: flag = False break if flag: stack.append(x) else: break if flag: answer += 1 return answer for-else를 활용한 풀이 def solution(skill, skill_trees): answer = 0 for skills in skill_trees: skill_list = lis..
[프로그래머스/파이썬] 모음사전 나의 풀이 from itertools import product def solution(word): pool = [] count = 1 for i in range(1, 6): for x in product(['A', 'E', 'I', 'O', 'U'], repeat=i): pool.append(x) pool.sort() return pool.index(tuple(word)) + 1 계산한 풀이 def solution(word): answer = 0 for i, n in enumerate(word): answer += (5 ** (5 - i) - 1) / (5 - 1) * "AEIOU".index(n) + 1 return answer 정성스러운 풀이 def solution(word): arr=['A', '..
[프로그래머스/파이썬] 전력망을 둘로 나누기 나의 풀이 def get_children(tree, index): global children children[index] = 1 for child in tree[index]: if not children[child]: get_children(tree, child) children[index] += children[child] def solution(n, wires): global children tree = [[] for _ in range(n)] children = [0] * n for wire in wires: a, b = wire tree[a - 1].append(b - 1) tree[b - 1].append(a - 1) get_children(tree, 0) return abs(n - 2 * m..

반응형