Algorithm/Problem Solving 155

[프로그래머스/Programmers] 완주하지 못한 선수

완주하지 못한 선수 📝문제 👨‍💻코드 def solution(participant, completion): participant.sort() completion.sort() for i in range(len(completion)): if participant[i] != completion[i]: return participant[i] return participant[-1] 🤔Review 처음엔 일반 간단히 for를 이용해서 풀었더니 시간초과가 발생했다. 정렬한 뒤 두개의 배열을 비교하고 만약 동명이인이 있다면 그 두명 중 한명은 완주를 못할 수 있기 때문에 같은 인덱스에 같은 값이 있지 않으면 그 값을 return해주었고 만약 동명이인이 없다면 정렬한 뒤 맨 마지막에 있는 사람이 완주하지 못한 사람일 ..

[프로그래머스/Programmers] 신규 아이디 추천

신규 아이디 추천 📝문제 👨‍💻코드 def solution(new_id): answer = '' special = '~!@#$%^&*()=+[{]}:?,/' special = list(special) new_id = new_id.lower() a = list(new_id) result = [] for i in a: if i not in special: result.append(i) result = ''.join(result) while '..' in result: result = result.replace('..', '.') result = list(result) if result: if result[0] == '.': del result[0] if result: if result[-1] == '.': ..

[프로그래머스/Programmers] 체육복

체육복 📝문제 👨‍💻코드 def solution(n, lost, reserve): answer = 0 for i in range(1, n + 1): if i not in lost: answer += 1 continue if i in lost and i in reserve: answer += 1 reserve.remove(i) continue if i in lost: if i - 1 in reserve: answer += 1 reserve.remove(i - 1) continue if i + 1 in reserve: if i + 1 in lost: continue else: answer += 1 reserve.remove(i + 1) continue return answer 🤔Review 체육 수업을 최대..

[프로그래머스/Programmers] 로또의 최고 순위와 최저 순위

로또의 최고 순위와 최저 순위 📝문제 👨‍💻코드 def solution(lottos, win_nums): answer = [] low = 0 high = 0 for i in range(6): if lottos[i] in win_nums: low += 1 if lottos[i] == 0: high += 1 if low + high < 2: answer.append(6) else: answer.append(7 - (low + high)) if low < 2: answer.append(6) else: answer.append(7 - low) return answer 🤔Review 최저 순위는 일단 모르는 숫자를 제외하고 맞은 숫자만으로 판단할 수 있다. 모르는 숫자는 최고 순위를 구할 때만 고려했다. 그렇게 ..

[프로그래머스/Programmers] 소수 만들기

소수 만들기 📝문제 👨‍💻코드 from itertools import combinations def solution(nums): answer = 0 arr = list(combinations(nums, 3)) for i in range(len(arr)): count = 0 result = sum(list(arr[i])) for j in range(2, result): count += 1 if result % j == 0: count = 0 break if count == result - 2: answer += 1 count = 0 return answer 🤔Review combinations를 사용하여 nums안에 들어있는 수를 3가지로 조합 한 경우를 arr이라는 list에 넣었다. 그 후 arr에 있..

[프로그래머스/Programmers] 키패드 누르기

키패드 누르기 📝문제 👨‍💻코드 def solution(numbers, hand): answer = [] leftHand = &#39;*&#39; rightHand = &#39;#&#39; left = [1, 4, 7, &#39;*&#39;] right = [3, 6, 9, &#39;#&#39;] center = [2, 5, 8, 0] for i in range(len(numbers)): if numbers[i] in left: answer.append(&#39;L&#39;) leftHand = numbers[i] continue if numbers[i] in right: answer.append(&#39;R&#39;) rightHand = numbers[i] continue else: if leftHa..

[프로그래머스/Programmers] 크레인 인형뽑기 게임

크레인 인형뽑기 게임 📝문제 👨‍💻코드 def solution(board, moves): answer = 0 basket = [] for i in moves: for j in range(len(board)): if board[j][i - 1] == 0: continue else: basket.append(board[j][i - 1]) board[j][i - 1] = 0 break if len(basket) >= 2: for k in range(1, len(basket)): if basket[k] == basket[k - 1]: basket.pop() basket.pop() answer += 2 return answer 🤔Review 간단한 그래프 문제였다. moves에 있는 곳으로 움직여서 0이 아닌 숫..

[프로그래머스/Programmers] 숫자 문자열과 영단어

숫자 문자열과 영단어 📝문제 👨‍💻코드 def solution(s): answer = [] arr = list(s) result = [] number = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} dict = {'zero' : '0', 'one' : '1', 'two' : '2', 'three' : '3', 'four' : '4', 'five' : '5', 'six' : '6', 'seven' : '7', 'eight' : '8', 'nine' : '9'} for i in range(len(arr)): if arr[i] in number: answer.append(arr[i]) else: result.append(arr[i]) a = ''.join(resu..

[프로그래머스/Programmers] 모의고사

모의고사 📝문제 👨‍💻코드 def solution(answers): result = [] check = [] arr = [[1, 2, 3, 4, 5], [2, 1, 2, 3, 2, 4, 2, 5], [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]] for i in range(3): count = 0 k = 0 for j in range(len(answers)): if k > len(arr[i]) - 1: k = 0 if answers[j] == arr[i][k]: count += 1 k += 1 check.append(count) for i in range(len(check)): if check[i] == max(check): result.append(i + 1) return result 🤔Rev..