본문 바로가기

728x90

분류 전체보기

(180)
프로그래머스 LV 2 땅따먹기 def solution(land): answer = 0 for i in range(1,len(land)) : for j in range(len(land[0])) : land[i][j] += max(land[i-1][:j] + land[i-1][j+1:]) return max(land[len(land)-1])풀이 및 회고처음에는 DFS 탐색을 통해 큰 값을 return 하는 풀이 인줄 알았다... 하지만 이중 for 문을 통해 간단하게 풀 수 있었습니다. 슬라이싱을 활용해 같은 index 를 더하지 않는 방법이 있었습니다.
프로그래머스 LV 2 롤케이크 자르기 from collections import Counterdef solution(topping): answer = 0; dict = Counter(topping); set_dict = set(); for i in topping : dict[i] -= 1; set_dict.add(i); if dict[i] == 0 : dict.pop(i); if len(dict) == len(set_dict) : answer += 1; return answer풀이 및 회고1. Counter 를 사용해 처음 토핑 개수를 구한다. (철수)2. set() 자료구조를 선언해 동생 토핑개수도 구한다.3. 반복문을 통해..
프로그래머스 LV 2 방문길이 def solution(dirs): answer = set(); dict = {'U': (1, 0), 'D': (-1, 0), 'R': (0, 1), 'L': (0, -1)} x = 0; y = 0; for i in dirs : dx,dy = dict[i]; nx = x + dx; ny = y + dy; if -5 풀이 및 회고1. 동서남북으로 이동하는 방향을 딕셔너리로 저장합니다.2. 반복문을통해 좌표를 이동시킵니다.3. 범위에 벗어나지 않는 선에서 이동전좌표 , 이동후좌표 그리고 역으로 저장을 합니다.4. 역으로 저장하는 이유는 '처음 가는 길' 에 대해만 카운트를 하기 때문입니다.5. 그러고 현재 좌표를 이동 시킵니..
프로그래머스 LV2 더 맵게 import heapqdef solution(scoville, K): answer = 0 heap = []; for i in scoville : heapq.heappush(heap, i) while heap[0] 풀이 및 회고파이썬에 heapq 라이브러리를 사용해 간단하게 해결햇습니다.heaqp 는 '최소힙' 기능을 가지고 있으며 자동으로 정렬을 해줍니다. heapq 메서드heappush(heap, item)해당하는 heap 에 item을 넣는 메서드 입니다. heappop(heap)해당하는 heap 에서 가장 작은 (최소) item을 빼는 메서드 입니다.
프로그래머스 LV1 공원산책 # 한칸씩 탐색하다가 이동가능한 피봇 여부def solution(park, routes): answer = [] x, y = 0, 0 # 시작 지점 저장하기 for i in range(len(park)): for j in range(len(park[i])): if park[i][j] == 'S': x, y = i, j # 이동하기 for route in routes: # 방향, 이동 횟수 direction, moveNumber = route.split(' ') moveNumber = int(moveNumber) dx = x; ..
프로그래머스 LV2 모음사전 answer = 0found = Falsedef solution(word): global answer, found answer = 0 found = False alpha = ['A','E','I','O','U'] def dfs(current_word): global answer, found if len(current_word) > 5: return if current_word == word: found = True return answer += 1 for i in alpha: if not found: ..
프로그래머스 LV2 [3차] 압축 import stringdef solution(msg): answer = []; alpha_dict = {char: index + 1 for index, char in enumerate(string.ascii_uppercase)} print(alpha_dict) index = 26; while msg : wc = "" for i in range(len(msg)) : wc = wc + msg[i]; if wc not in alpha_dict : # 사전에 새로운 단어 추가 index += 1; alpha_dict[wc] = ind..
프로그래머스 LV2 k진수에서 소수 개수 구하기 def solution(n, k): answer = 0; word = ''; while n : word = str(n%k) + word; n = n // k; word = word.split('0'); for i in word : prime = True; if len(i) == 0: continue; if int(i) 풀이 및 회고우선 k 진수로 변환합니다. 그리고 난 후 '0' 을 기준으로 나눕니다.split을 통해 얻은 배열에서 '' 인 경우랑 1인 경우 건너뜁니다.나머지에 대해서 소수인지 판별합니다.

728x90