코딩문제풀이/프로그래머스 55

[Python] 기능 개발

1. 문제 2. 풀이 과정 방법 1) 남은 날짜 계산할 때, 나머지가 있으면 올리도록 한다. 최대 날짜가 갱신되면 바로 출력한다. def solution(progresses, speeds): answer = [] max_day = 0 pre_idx = 0 for i in range(len(progresses)): temp = 100 - progresses[i] day = temp//speeds[i] if temp % speeds[i] != 0: day += 1 if max_day < day: #최대 걸리는 날보다 크면 그 앞의 기능들 먼저 배포 max_day = day if i != 0: answer.append(i-pre_idx) pre_idx = i if i==len(progresses)-1: #마지..

[Python] 위장

1. 문제 2. 풀이 과정 방법 1) hash 자료구조를 이용해서 옷 종류별로 몇 개씩 있는지 count한 후, 특정 종류는 선택 안 할 경우도 있으니 각 종류별로 안 입을 경우를 1씩 더해서 조합의 개수를 count한다. 여기서 모두 선택 안 할 경우 1가지를 빼주면 결과값이 된다. def solution(clothes): answer = 1 hash_map = {} #종류별로 개수 count for i in clothes: if i[1] in hash_map: hash_map[i[1]] += 1 else: hash_map[i[1]] = 1 #결과 계산 arr = list(hash_map.values()) for i in arr: answer *= (i+1) return answer-1 방법 2) 논리..

[Python] 전화번호 목록

1. 문제 2. 풀이 과정 방법 1) 정렬해서 인접한 요소만 비교하기 *str.starts.with("접두어") : 특정 문자열로 시작하는지 확인하는 함수 def solution(phone_book): phone_book.sort() for i in range(len(phone_book)-1): if phone_book[i+1].startswith(phone_book[i]): return False return True 방법 2) 해쉬 맵 사용하기 리스트에서 특정 요소가 있는지 확인하는 것보다 set, dict에서 확인하는 것이 더 빠르다. number in phone_book으로 해도 답은 나오지만, number in hash_map을 사용하는 이유이다. def solution(phone_book): ..

[Python] K번째 수

1. 문제 2. 풀이 과정 방법 1) sorted 함수를 써서 정렬된 결과를 반환받음 def solution(array, commands): answer = [] for command in commands: i, j, k = command temp = sorted(array[i-1:j]) answer.append(temp[k-1]) return answer 방법 2) 방법1과 같은 원리이며 사용한 함수만 다르다. 아래는 map, lambda 함수를 이용하여 더 짧게 표현하였다. def solution(array, commands): answer = [*(map(lambda x : sorted(array[x[0]-1:x[1]])[x[2]-1], commands))] return answer

[Python] 완주하지 못한 선수

1. 문제 2. 풀이 과정 방법 1) 아스키코드를 기준으로 정렬한 후, 앞에서부터 순차 탐색한다. def solution(participant, completion): participant.sort() completion.sort() answer = '' for i in range(len(completion)): if participant[i] != completion[i]: answer = participant[i] break if answer == '': answer = participant[-1] return answer 방법 2) collections 라이브러리 활용하기 collections.Counter(배열) => 사전 형태로 {'배열 속의 데이터' : 개수, ... } 를 반환한다.빼기 연산..