[프로그래머스] 코딩테스트 고득점 Kit/완전탐색
-
[Level 2] 피로도(permutations)[프로그래머스] 코딩테스트 고득점 Kit/완전탐색 2023. 5. 1. 00:54
from itertools import permutations def solution(k, dungeons): answer = 0 for p in permutations(dungeons, len(dungeons)): temp = k count = 0 for need, spend in p: if temp >= need: temp -= spend count += 1 answer = max(answer, count) return answer permutations 통해 모든 순서 경우의 수 구하고 하나씩 실행해본 뒤 최대 던전 수 return하는 방식
-
[Level 2] 카펫[프로그래머스] 코딩테스트 고득점 Kit/완전탐색 2023. 5. 1. 00:09
def solution(brown, yellow): answer = [] a = [] for i in range(1, int(yellow ** 0.5) + 1): if yellow % i == 0: a.append([yellow //i, i]) for element in a: if 2*(element[0] + element[1]) + 4 == brown: answer.append(element[0] + 2) answer.append(element[1] + 2) return answer 문제를 풀기 위한 아이디어: yellow와 brown의 관계에 대해 생각해보면 간단하게 해결할 수 있음 yellow 격자가 x * y 형태로 놓여 있다면 brown = 2x + 2y + 4의 관계식을 가짐. yellow 격..
-
[Level 2] 소수 찾기(permutations)[프로그래머스] 코딩테스트 고득점 Kit/완전탐색 2023. 4. 30. 23:46
from itertools import permutations def solution(numbers): answer = [] nums = [n for n in numbers] per = [] for i in range(1, len(numbers)+1): per += list(permutations(nums, i)) new_nums = [int(''.join(p)) for p in per] for n in new_nums: if n < 2: continue check = True for i in range(2, int(n**0.5) + 1): if n % i == 0: check = False break if check: answer.append(n) return len(set(answer)) permut..
-
[Level 1] 모의고사(enumerate)[프로그래머스] 코딩테스트 고득점 Kit/완전탐색 2023. 4. 30. 23:12
def solution(answers): supo_1 = [1, 2, 3, 4, 5] supo_2 = [2, 1, 2, 3, 2, 4, 2, 5] supo_3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] supo_count = [0, 0, 0] for i in range(len(answers)): if answers[i] == supo_1[i % 5]: supo_count[0] += 1 if answers[i] == supo_2[i % 8]: supo_count[1] += 1 if answers[i] == supo_3[i % 10]: supo_count[2] += 1 k = max(supo_count) answer = [] if supo_count[0] == k: answer.append(..
-
[Level 1] 최소직사각형[프로그래머스] 코딩테스트 고득점 Kit/완전탐색 2023. 4. 30. 22:43
def solution(sizes): big = [] small = [] for i in sizes: if i[0] > i[1]: big.append(i[0]) small.append(i[1]) else: big.append(i[1]) small.append(i[0]) answer = max(big) * max(small) return answer 명함 눕혀서 수납 가능 → 명함의 가로 길이, 세로 길이가 주어졌을 때 둘 중 더 큰 값과 작은 값으로 분류 큰 값들 중 max, 작은 값들 중 max 구해서 곱하기 대박 충격 위 내용을 두 줄로도 코딩 가능... def solution(sizes): return max(max(x) for x in sizes) * max(min(x) for x in sizes)