목록알고리즘 (53)
눈송이의 개발생활
문제 https://school.programmers.co.kr/learn/courses/30/lessons/42584 내 코드 - 효율성 테스트 실패 def solution(prices): answer = [] while prices: temp = prices.pop(0) cnt = 0 for i in range(len(prices)): if prices[i] >= temp: cnt += 1 else: cnt += 1 break answer.append(cnt) return answer 다른 사람 풀이 from collections import deque def solution(prices): answer = [] p = deque(prices) while p: temp = p.popleft() cnt..
문제 https://programmers.co.kr/learn/courses/30/lessons/42747 내 코드 def solution(citations): count = 0 citations.sort() for i in range(citations[-1] + 1): new_c = [j for j in range(len(citations)) if citations[j] >= i] if len(new_c) >= i: count = i else: break return count 다른 사람 풀이 def solution(citations): citations = sorted(citations) l = len(citations) for i in range(l): if citations[i] >= l-i: r..
문제 https://programmers.co.kr/learn/courses/30/lessons/42576 내 코드 #1 (실패) def solution(participant, completion): for i in range(len(completion)): if completion[i] in participant: participant.remove(completion[i]) answer = participant[0] if len(participant) != 0 else ' ' return answer 내 코드 #2 (성공) def solution(participant, completion): participant.sort() completion.sort() for p, c in zip(participa..
문제 https://programmers.co.kr/learn/courses/30/lessons/42748 내 코드 def solution(answers): students = [ [1, 2, 3, 4, 5], [2, 1, 2, 3, 2, 4, 2, 5], [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] ] answer = [0, 0, 0] result = [] for i in range(len(answers)): if answers[i] == students[0][i % 5]: answer[0] += 1 if answers[i] == students[1][i % 8]: answer[1] += 1 if answers[i] == students[2][i % 10]: answer[2] += 1 m =..
문제 https://programmers.co.kr/learn/courses/30/lessons/42748 내 코드 def solution(array, commands): answer = [] for index in range(len(commands)): temp = [] i = commands[index][0] - 1 j = commands[index][1] k = commands[index][2] - 1 temp = array[i:j] temp.sort() answer.append(temp[k]) return answer 다른 사람 풀이 def solution(array, commands): answer = [] for command in commands: i,j,k = command answer.a..
문제 https://programmers.co.kr/learn/courses/30/lessons/42862 내 코드 def solution(n, lost, reserve): s_res = set(reserve) - set(lost) s_lost = set(lost) - set(reserve) for i in s_res: if i - 1 in s_lost: s_lost.remove(i - 1) elif i + 1 in s_lost: s_lost.remove(i + 1) return n - len(s_lost) 다른 사람 풀이 def solution(n, lost, reserve): _reserve = [r for r in reserve if r not in lost] _lost = [l for l in l..
문제 https://programmers.co.kr/learn/courses/30/lessons/17682 내 코드 def solution(dartResult): dartResult = dartResult.replace('10', 'k') bonus = {'S': 1, 'D': 2, 'T': 3} temp = [] for i in dartResult: if i.isdigit(): temp.append(int(i)) elif i == 'k': temp.append(10) elif i in bonus: temp[-1] = temp[-1] ** bonus[i] elif i == '#': temp[-1] *= -1 elif i == '*': temp[-1] *= 2 if len(temp) > 1: temp[-2..
문제 https://programmers.co.kr/learn/courses/30/lessons/42889 내 코드 def solution(N, stages): answer = {} stages.sort() for i in range(1, N + 1): # 스테이지 성공 사람 수 c = stages.count(i) if c == 0: answer[i] = 0 # 스테이지 성공한 사람 수 / 스테이지 모든 사람 수 else: answer[i] = c / len(stages) # 성공한 사람만큼 앞에서부터 stack에서 없애기 for _ in range(c): stages.pop(0) # 딕셔너리 정렬하기 st = dict(sorted(answer.items(), key=lambda item: item[1]..