목록파이썬 (170)
컴공생의 다이어리
[프로그래머스] 가장 큰 수 - 파이썬(Python) def solution(numbers): numbers = list(map(str, numbers)) numbers.sort(key=lambda x: x * 3, reverse=True) return str(int("".join(numbers))) https://school.programmers.co.kr/learn/courses/30/lessons/42746 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr
[프로그래머스] 이중우선순위큐 - 파이썬(Python) import heapq def solution(operations): heap = [] for op in operations: print(op) command, num = op.split() if command == 'D': if not heap: # 삭제할 데이터가 없다면 continue if num == '1': # 최댓값 삭제 heap.remove(heapq.nlargest(1, heap)[0]) else: # 최솟값 삭제 heapq.heappop(heap) else: heapq.heappush(heap, int(num)) return [heapq.nlargest(1, heap)[0], heap[0]] if heap else [0, 0] htt..
[프로그래머스] 올바른 괄호 - 파이썬(Python) from collections import deque def solution(s): stack = deque() for i in s: if i == ')' and not stack: # 스택에 아무것도 없는데 ')'가 있는 경우 return False elif i == ')' and stack[-1] == '(': # 괄호쌍 없애줌 stack.pop() else: # i가 '('일 때 stack.append(i) return True if not stack else False https://school.programmers.co.kr/learn/courses/30/lessons/12909 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭..
[프로그래머스] 같은 숫자는 싫어 - 파이썬(Python) def solution(arr): answer = [] for i in arr: if not answer or answer[-1] != i: answer.append(i) return answer https://school.programmers.co.kr/learn/courses/30/lessons/12906 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr
백준 10986번 : 나머지 합 (문제 바로가기) 내 코드 import sys input = sys.stdin.readline n, m = map(int, input().split()) # n : 숫자 갯수, m : 나눌 수 num = list(map(int, input().split())) + [0] # 숫자 입력 r = [0] * m # 누적합을 m으로 나눴을 때의 나머지가 index이고 그 값에 count for i in range(n): num[i] += num[i - 1] # 숫자 정보를 누적합으로 갱신 r[num[i] % m] += 1 # 해당 누적합을 m으로 나눴을 때의 나머지에 해당하는 값에 1추가 cnt = r[0] # 연속된 부분 구간의 합이 M으로 나누어 떨어지는 구간의 개수 for ..
백준 20040번 : 사이클 게임 (문제 바로가기) 내 코드 import sys def find_parent(parent, x): if x != parent[x]: parent[x] = find_parent(parent, parent[x]) return parent[x] def union(parent, a, b): a = find_parent(parent, a) b = find_parent(parent, b) if a < b: parent[b] = a else: parent[a] = b input = sys.stdin.readline n, m = map(int, input().split()) # n : 점의 개수, m : 진행된 차례의 수 parent = list(range(n)) for i in ran..
백준 1916번 : 최소비용 구하기 (문제 바로가기) 내 코드 import sys, heapq from collections import defaultdict input = sys.stdin.readline INF = int(1e9) n = int(input()) # 도시의 개수 m = int(input()) # 버스의 개수 graph = defaultdict(list) # 버스 정보 for _ in range(m): s, e, c = map(int, input().split()) graph[s].append((e, c)) start, end = map(int, input().split()) # 출발점과 도착점 도시번호 distance = [INF] * (n + 1) queue = [] heapq.hea..
백준 9421번 : 소수상근수 (문제 바로가기) 내 코드 from collections import defaultdict def get_prime(num): # 1부터 num까지의 범위에서 소수 찾아서 리스트로 반환 prime = [False, False] + [True] * (num - 1) for i in range(2, int(num ** 0.5) + 1): if not prime[i]: continue j = 2 while i * j = 1 and answer & visited[temp]): # 제곱의 합이 1이거나 이 합이 소수상근수가 되는 결과라면 answer.add(p) visited[temp].add(p) break elif len(visited[temp]) >= 1 and not (ans..
[프로그래머스] 양궁대회 - 파이썬(Python) from itertools import combinations_with_replacement def solution(n, info): answer = [-1] max_gap = -1 # 점수 차 for combi in combinations_with_replacement(range(11), n): # 중복 조합으로 0~10점까지 n개 뽑기 info2 = [0] * 11 # 라이언의 과녁 점수 for i in combi: # combi에 해당하는 화살들을 라이언 과녁 점수에 넣기 info2[10 - i] += 1 apeach, lion = 0, 0 for idx in range(11): if info[idx] == info2[idx] == 0: # 라이언과..
[프로그래머스] 교점에 별 만들기 - 파이썬(Python) from itertools import combinations def find_intersection_point(line1, line2): # 두 직선의 모든 좌표가 정수인 교점 구하기 a, b, e = line1 # ax + by + e = 0 c, d, f = line2 # cx + dy + f = 0 if a * d == b * c: # 기울기가 일치하거나 평행인 경우 return x = (b * f - e * d) / (a * d - b * c) y = (e * c - a * f) / (a * d - b * c) if x == int(x) and y == int(y): # 교점이 정수라면 return (int(x), int(y)) def ..