본문 바로가기

⏳ 알고리즘/python 알고리즘 문제 풀이

(62)
프로그래머스 - LV2. 택배상자 문제 https://school.programmers.co.kr/learn/courses/30/lessons/131704# 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(order): answer = 0 package = list(range(1, len(order)+1)) stack = [] for o in order: if o in package: index = package.index(o) stack += package[:index] answer += 1 package = package[index+1:] elif o == ..
프로그래머스 - LV2. 롤케이크 자르기 문제 https://school.programmers.co.kr/learn/courses/30/lessons/132265 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(topping): answer = 0 for i in range(1, len(topping)): if len(set(topping[:i])) == len(set(topping[i:])): answer += 1 return answer 시간 초과! from collections import Counter def solution(topping): answer = 0..
프로그래머스 - LV1. 햄버거 만들기 문제 https://school.programmers.co.kr/learn/courses/30/lessons/133502# 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(ingredient): answer = 0 ingredient = ''.join(map(str,ingredient)) while 1: h = ingredient.count('1231') if h >= 1: answer += h ingredient = ingredient.replace('1231','',h) else: break return answer
프로그래머스 - LV3. 가장 먼 노드 문제 https://school.programmers.co.kr/learn/courses/30/lessons/49189 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 BFS 이용 from collections import deque def solution(n, edge): shortest_path = [1]+[0 for _ in range(n-1)] graph = [[] for _ in range(n)] for e in edge: graph[e[0]-1].append(e[1]-1) graph[e[1]-1].append(e[0]-1) print(gra..
프로그래머스 - LV3. 순위 문제 https://school.programmers.co.kr/learn/courses/30/lessons/49191 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 defaultdict(set) 이용 from collections import defaultdict def solution(n, results): answer = 0 win = defaultdict(set) lose = defaultdict(set) for r in results: win[r[0]-1].add(r[1]) lose[r[1]-1].add(r[0]) for i in range..
프로그래머스 - LV2. 연속 부분 수열 문제 https://school.programmers.co.kr/learn/courses/30/lessons/131701 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(elements): answer = [] e = elements + elements #길이가 1 answer += elements #길이가 2~len(elements)-1 for i in range(2, len(elements)): for j in range(len(elements)): answer.append(sum(e[j:j+i])) #길이가 len(elem..
프로그래머스 - LV5. 방의 개수 문제 https://school.programmers.co.kr/learn/courses/30/lessons/49190 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(arrows): answer = 0 arrived = [[0, 0]] move = [(0,1), (1,1), (1,0), (1,-1), (0, -1), (-1, -1), (-1, 0), (-1, 1)] x, y = 0, 0 for a in arrows: # - 처음 가는 곳 인데 # - 이미 가본 곳이라면? # => 방의 개수 + 1 bx, by = x, y x ..
프로그래머스 - LV2. 할인 행사 문제 https://school.programmers.co.kr/learn/courses/30/lessons/131127 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 from collections import Counter def solution(want, number, discount): answer = 0 d = {} for i in range(len(want)): d[want[i]] = number[i] for j in range(len(discount)-9): c = Counter(discount[j:j+10]) if c == d: answ..
프로그래머스 - LV3. 등굣길 문제 https://school.programmers.co.kr/learn/courses/30/lessons/42898 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 def solution(m, n, puddles): gp = [[0] * (m+1) for _ in range(n+1)] gp[1][1] = 1 for i in range(1, n+1): for j in range(1, m+1): if i == 1 and j == 1: continue if [j,i] in puddles: gp[i][j] = 0 else: gp[i][j] = gp[i-1..
프로그래머스 - LV3. 이중우선순위큐 문제 https://school.programmers.co.kr/learn/courses/30/lessons/42628 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 코드 import heapq def solution(operations): queue = [] def insert(queue, num): heapq.heappush(queue, num) return queue def max_delete(queue): queue.remove(max(queue)) heapq.heapify(queue) return queue def min_delete(queue)..