[프로그래머스] 124 나라의 숫자
·
Algorithm
문제 풀이from collections import dequedef solution(n): #리스트 앞쪽에 넣기 위해서 deque 선언 res = deque() def recur_124(n) : quo = n // 3 rem = n % 3 #재귀함수 내부에서 res 사용위해 nonlocal nonlocal res #재귀함수 종료 조건 : 3으로 나누다가 몫이 0일때 if n == 0 : return ''.join(res) if rem == 1 : res.appendleft('1') return recur_124(quo) elif rem =..
[프로그래머스] 프렌즈4블록
·
Algorithm
문제쉽게 말하자면, 애니팡 블록을 재현하는 알고리즘을 구현하는 것이다. 풀이def solution(m, n, board): board = [list(row) for row in board] cnt = 0 while True: check_board = [[0] * n for _ in range(m)] #4개의 블록 겹치는지 검사 for i in range(m-1): for j in range(n-1): if board[i][j] is not None and board[i][j] == board[i + 1][j] == board[i][j+1] == board [i+1][j+1] : ..
[프로그래머스] 마법의 엘리베이터
·
Algorithm
문제 마법의 엘리베이터라는 설정이 붙어있긴 하지만, 각 자릿수를 0으로 만드는 최소 이동횟수를 구하는 문제이다. 풀이def solution(storey): num = storey cnt = 0 while num : #현재 1의 자리 digit = num % 10 #다음에 들어갈 num num = num // 10 #num % 10 : 다음 10의 자릿수 #자릿수가 5보다 크면 : 올라가기가 유리(상위 자릿수에 1 더함) if digit > 5 or (digit == 5 and (num % 10 + 1) > 5): cnt += (10 - digit) num += ..
[프로그래머스] 오픈채팅방
·
Algorithm
문제 풀이문제를 봤을 때 들었던 생각 : 닉네임을 변경해서 모든 메세지를 출력하려면, 메세지가 나올 record만 따로 저장을 하고, change나 enter가 들어왔을 때 닉네임을 업데이트 하면 되겠구나 ! 하고 생각했다.from collections import defaultdictdef solution(record): access_list = [] member_dict = defaultdict(str) res = [] #access_list 저장, 멤버 이름 업데이트 for rec in record : parts = rec.split() #change, enter 경우 if len(parts) == 3 : ..
[프로그래머스] 프로세스
·
Algorithm
문제 풀이from collections import deque#큐를 순회하여 최대 우선순위 찾는 함수def max_queue(queue) : max = float('-inf') #queue 순회 for i, (_, priority) in enumerate(queue) : if priority > max : max = priority return maxdef solution(priorities, location): # location(key) : priorities(value)값을 가지는 딕셔너리 생성 priorities_dict = {index : value for index, value in enumerate(priorities)..
[프로그래머스] 피로도
·
Algorithm
문제 풀이완전 탐색 알고리즘을 구상해야겠다고 생각이 들어서, 이중 for문을 사용해 해결했다. break를 활용해 피로도가 부족하면 바로 도는걸 중지하도록 하여 시간을 조금 줄여봤다.from itertools import permutationsdef solution(k, dungeons): #0 ~ 던전의 개수 - 1까지 모든 값을 가지는 순열 모두 반환 orders = list(permutations(range(len(dungeons)))) max_dungeon_cnt = 0 #모든 던전 가는 경우의 수 순회 for order in orders : my_tired = k current_dungeon_cnt = 0 ..