본문 바로가기

전체 글93

프로그래머스 합승 택시 요금 파이썬 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/72413 코딩테스트 연습 - 합승 택시 요금 6 4 6 2 [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]] 82 7 3 4 1 [[5, 7, 9], [4, 6, 4], [3, 6, 1], [3, 2, 3], [2, 1, 6]] 14 6 4 5 6 [[2,6,6], [6,3,7], [4,6,7], [6,5,11], [2,5,12], [5,3,20], [2,4 programmers.co.kr 2. 풀이 다익스트라 3. 구현 import math import hea.. 2021. 8. 14.
프로그래머스 순위검색 파이썬 1. 문제 https://programmers.co.kr/learn/courses/30/lessons/72412 2021. 8. 14.
python: @lru_cache(None) 데코레이터 재귀함수 위에 @lru_cache(None) 데코레이터를 사용하면 함수가 반환하는 값을 메모이제이션(memoization)할 수 있다. class Solution: def minFlipsMonoIncr(self, s: str) -> int: s = [int(i) for i in s]+[1] @lru_cache(None) def dp(i, k): if i==-1: return 0 return min([dp(i-1, j)+int(k!=s[i]) for j in range(k+1)]) return dp(len(s)-1,1) 2021. 8. 11.
[테스트케이스추가] 백준 1715번: 카드 정렬하기 1. 문제 https://www.acmicpc.net/problem/1715 1715번: 카드 정렬하기 정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장 www.acmicpc.net 2. 풀이 우선순위 큐 3. 구현 import sys # sys.stdin = open('input.txt') import heapq lis = [] n = int(sys.stdin.readline().strip()) while n: n-=1 tmp = int(sys.stdin.readline().strip()) heapq.heappush(lis, tmp) answer=0.. 2021. 8. 6.
[테스트케이스 추가] 백준 14503번: 로봇 청소기 , python 파이썬 1. 문제 https://www.acmicpc.net/problem/14503 14503번: 로봇 청소기 로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어 www.acmicpc.net 2. 풀이 시뮬레이션 주의할 점은 뒤로 후진 할 때 벽을 마주치면 바로 리턴해야하는 것이다. 모든 곳을 돌면 안되고 위의 케이스가 있는 지 확인해야한다. 3. 구현 import sys sys.stdin = open('input.txt') sys.setrecursionlimit(10**8) dx=[0,1,0,-1] dy=[-1,0,1,0] n,m = map(int, sys.stdin.r.. 2021. 8. 6.
[테스트케이스추가] 백준 3079번: 입국심사 python 파이썬 1. 문제 https://www.acmicpc.net/problem/3079 3079번: 입국심사 첫째 줄에 N과 M이 주어진다. (1 ≤ N ≤ 100,000, 1 ≤ M ≤ 1,000,000,000) 다음 N개 줄에는 각 심사대에서 심사를 하는데 걸리는 시간인 Tk가 주어진다. (1 ≤ Tk ≤ 109) www.acmicpc.net 2. 풀이 이분탐색 3. 구현 import sys #sys.stdin=open('input.txt') #@profile def main(): n,m = map(int, sys.stdin.readline().strip().split()) tmp=n lis=[] while tmp: tmp-=1 num = int(sys.stdin.readline().strip()) lis.ap.. 2021. 8. 6.
백준 16139번: 인간-컴퓨터 상호작용 python 파이썬 1. 문제 https://www.acmicpc.net/problem/16139 16139번: 인간-컴퓨터 상호작용 첫 줄에 문자열 $S$가 주어진다. 문자열의 길이는 $200,000$자 이하이며 알파벳 소문자로만 구성되었다. 두 번째 줄에는 질문의 수 $q$가 주어지며, 문제의 수는 $1\leq q\leq 200,000$을 만족한다. 세 번째 www.acmicpc.net 2. 풀이 시간초과 나면 print를 sys.stdout.write()로 바꾸기 3. 구현 50점: import sys # sys.stdin=open('input.txt') s = sys.stdin.readline() n = int(sys.stdin.readline()) lis=[] for c in 'abcdefghijklmnopqrst.. 2021. 8. 6.
백준 10211번: Maximum Subarray python 파이 1. 문제 https://www.acmicpc.net/problem/10211 10211번: Maximum Subarray 크기 N인 정수형 배열 X가 있을 때, X의 부분 배열(X의 연속한 일부분) 중 각 원소의 합이 가장 큰 부분 배열을 찾는 Maximum subarray problem(최대 부분배열 문제)은 컴퓨터 과학에서 매우 잘 알려져 있 www.acmicpc.net 2. 풀이 3. 구현 import sys import math # sys.stdin = open('input.txt') t = int(sys.stdin.readline()) while t: t-=1 n = int(sys.stdin.readline()) lis = list(map(int, sys.stdin.readline().spli.. 2021. 8. 6.
백준 1991번 트리순회 python 파이썬 1. 문제 https://www.acmicpc.net/problem/1991 1991번: 트리 순회 첫째 줄에는 이진 트리의 노드의 개수 N(1≤N≤26)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 노드와 그의 왼쪽 자식 노드, 오른쪽 자식 노드가 주어진다. 노드의 이름은 A부터 차례대로 영문자 www.acmicpc.net 2. 풀이 3. 구현 import sys #sys.stdin=open('input.txt') n = int(sys.stdin.readline().strip()) tree={} while n: n-=1 root,left,right = sys.stdin.readline().strip().split() tree[root] = [left, right] def preorder(root): .. 2021. 8. 6.
백준 11725번: 트리의 부모찾기 python 파이썬 1. 문제 https://www.acmicpc.net/problem/11725 11725번: 트리의 부모 찾기 루트 없는 트리가 주어진다. 이때, 트리의 루트를 1이라고 정했을 때, 각 노드의 부모를 구하는 프로그램을 작성하시오. www.acmicpc.net 2. 풀이 3. 구현 import sys # sys.stdin = open('input.txt') sys.setrecursionlimit(10**8) # @profile def main(): n = int(sys.stdin.readline()) tree=[[] for _ in range(n+2)] tmp=n while tmp>1: tmp-=1 n_1, n_2 = map(int, sys.stdin.readline().split()) tree[n_1]... 2021. 8. 6.
백준 2250번: 트리의 높이와 너비 python 파이썬 1.문제 https://www.acmicpc.net/problem/2250 2250번: 트리의 높이와 너비 첫째 줄에 노드의 개수를 나타내는 정수 N(1 ≤ N ≤ 10,000)이 주어진다. 다음 N개의 줄에는 각 줄마다 노드 번호와 해당 노드의 왼쪽 자식 노드와 오른쪽 자식 노드의 번호가 순서대로 주어진다. www.acmicpc.net 2. 풀이 3. 구현 import sys # sys.stdin=open('input.txt') sys.setrecursionlimit(10**7) class node: def __init__(self): self.left=0 self.right=0 self.depth=0 self.order=0 max_depth=-1 order=0 # @profile def main():.. 2021. 8. 6.
[반례모음] 백준 2839번: 설탕 배달 python 파이썬 1. 문제 https://www.acmicpc.net/problem/2839 2839번: 설탕 배달 상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그 www.acmicpc.net 2. 풀이 DP 3. 구현 import sys # sys.stdin = open('input.txt') import math target = int(sys.stdin.readline().strip()) lis = [math.inf]*5002 lis[3] = 1 lis[5] = 1 for i in range(6, target+1): lis[i] = min(lis[i-3]+1, lis[i-5]+1) .. 2021. 8. 6.
반응형