Python 28

완전탐색 Level 2 피로도 - python (프로그래머스)

소스 코드 from itertools import permutations def solution(k, dungeons): path_list = list(permutations(dungeons, len(dungeons))) answer_list = [] for path in path_list: cur_fatigue = k count = 0 for dungeon in path: if cur_fatigue < dungeon[0]: break cur_fatigue -= dungeon[1] count += 1 answer_list.append(count) return max(answer_list) 풀이 문제의 제한사항에서 던전의 개수는 1이상 8이하라고 하였으므로 이 또한 모든 던전의 형태를 구한 다음 각 던전을..

Python/알고리즘 2023.06.23

완전탐색 Level 2 카펫 - python (프로그래머스)

소스 코드 def solution(brown, yellow): total_size = brown + yellow for i in range(1,total_size + 1): if total_size % i == 0: width = i height = total_size // i if width >= height and width == yellow // (height - 2) + 2: return [width, height] 풀이 해당 문제는 모든 카펫 모양을 구해보면서 문제 설명에서 언급한 조건에 맞는 카펫만 찾으면 된다. 가운데에 노란색 카펫이 있고 그 주위를 갈색 카펫으로 빠짐없이 덮기 위해서는 높이가 가로 길이보다 더 커서는 안되며 yellow의 층이 높아짐에 따라 width 는 width == y..

Python/알고리즘 2023.06.23

완전탐색 Level 2 소수 찾기 - python (프로그래머스)

코드 from itertools import permutations def solution(numbers): numbers = list(numbers) p_numbers = [] for i in range(1, len(numbers)+1): permute = permutations(numbers, i) p_numbers+= permute answer_list = [] for permute in p_numbers: number = "" for num in permute: number += num number = int(number) answer_list.append(number) answer = 0 answer_list = set(answer_list) for num in answer_list: if nu..

Python/알고리즘 2023.06.23

완전탐색 Level 1 최소직사각형 - python (프로그래머스)

코드 def solution(sizes): width = [] height = [] answer = 0 for i in range (len(sizes)): width.append(max(sizes[i])) height.append(min(sizes[i])) print(width, height) answer=max(width)*max(height) return answer 풀이 해당 문제는 특정 조건에 따라서 가로, 세로로 명함을 회전시킬 수 있다는 조건이 있다. 따라서 해당 기준을 가로로 할 것인지 세로로 할 것인지 정해야 했다. 나는 여기서는 가로를 최대 길이가 오도록 정하였다.(보통 명함은 가로가 더 길기 때문에) 따라서 명함들을 순회하며 가로, 세로 길이 중 가장 긴 값을 width 배열에 저장하고..

Python/알고리즘 2023.06.23

힙 Level 3 이중 우선순위 큐 - python (프로그래머스)

https://school.programmers.co.kr/learn/courses/30/lessons/42628 소스 코드 from heapq import heappush, heappop def solution(operations): min_heap = [] max_heap = [] for operation in operations: [operator, value] = operation.split(' ') value = int(value) if operator == "I": heappush(min_heap, value) heappush(max_heap, -value) elif operator == "D": if value == 1 and len(max_heap) > 0: heappop(max_heap)..

Python/알고리즘 2023.05.08

힙 Level 2 더 맵게 - python (프로그래머스)

https://school.programmers.co.kr/learn/courses/30/lessons/42626 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 소스 코드 from heapq import heapify, heappush, heappop def solution(scoville, K): heapify(scoville) count = 0 while True: if len(scoville) = K: break low_2 = heappop(scoville) new_scoville = low_1 + (low_2 * 2) heappush(scovill..

Python/알고리즘 2023.05.08

정렬 Level 2 가장 큰 수 - python (프로그래머스)

소스 코드 def solution(numbers): numbers = list(map(str, numbers)) sort = sorted(numbers, key=lambda x :(x*4)[:4], reverse=True) result = ''.join(sort) if (int(result)) == 0: return '0' else: return str(result) 풀이 이번 문제의 핵심은 주어진 정수를 문자열로 바꾸어 정렬을 하는 것이다. 정수 형태의 비교는 값의 '크기'를 비교하지만 문자열의 비교는 사전식으로 어떤 문자열이 더 앞 순에 위치하는 지를 비교한다. 따라서 맨 처음에 리스트에 존재하는 정수를 모두 문자열로 바꾸었고 이후 sorted 함수를 사용하였다. 이때 key 값으로 lambda를 사..

Python/알고리즘 2023.05.08

해시 Level 1 완주하지 못한 선수 - python (프로그래머스)

https://school.programmers.co.kr/learn/courses/30/lessons/42576 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 소스 코드 def solution(participant, completion): participant.sort() completion.sort() for index, p in enumerate(participant): if index >= len(completion): return p if participant[index] != completion[index]: return p 풀이 맨 처음 시도..

Python/알고리즘 2023.05.01

그리디 Level 1 체육복 - python (프로그래머스)

https://school.programmers.co.kr/learn/courses/30/lessons/42862 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 소스 코드 def solution(n, lost, reserve): clothes_count = [1 for _ in range(n)] for r in reserve: clothes_count[r-1] += 1 for l in lost: clothes_count[l-1] -= 1 for index, value in enumerate(clothes_count): if value == 2: if in..

Python/알고리즘 2023.05.01