본문 바로가기

Coding test

(123)
[백준/1543/파이썬] 문서 검색 - Greedy 소스코드 document= input() search = input() pointer = 0 answer = 0 l_search = len(search) l_document = len(document) while pointer
[백준/20044/파이썬] Project Teams - Greedy 소스코드 n = int(input()) student = list(map(int,input().split())) student.sort() answer = [] for i in range (2*n): answer.append(student[i] + student[2*n-i-1]) print(min(answer)) 알고리즘 만약 친구들끼리 게임을 하는데 모두 다 다른 능력치를 가지고 2인 팀을 이루어서 게임을 한다면 어떤게 가장 공평한 팀일까? 위와 같은 생각을 모두 어렸을적 한번은 해봤을 것이다. 이번 문제가 이런 식으로 진행이 되었다. 1번부터 2n번까지의 친구들이 있고 n개의 팀이 나와야 하는데 국룰은 1번과 2n번, 2번과 2n -1번, 3번과 2n-2번 ... -> 이렇게 팀이 되는것이다. 이 문..
[백준/10974/파이썬] 모든 순열 소스코드 n = int(input()) per = [] def dfs() : if len(per) == n: print(*per) for i in range (1, n+1): if i not in per : per.append(i) dfs() per.pop() dfs() 알고리즘 1. for 문을 돌리면서 1부터 n까지 리스트에 넣는다. 2. 길이가 n이면 출력 실제로 per에 들어가는 과정은 다음과 같다 1번 루프 : 1 -> dfs() ------------------------------------> 다음에 2, 3을 넣음 2번 루프 : 1은 이미 있으니깐 2를 넣음 -> 1 ,2 -> dfs() ---> 다음에 3을 넣어야 함 3번 루프 : 1,2 이미 존재 -> 3을 넣고 dfs() 4번 루프 ..
[백준/15989/파이썬] 1,2,3 더하기 4 - dp 소스코드 n = int(input()) def dyPro(n): if n 1 1 이렇게 변하는 특성을 가지고 있고, 이는 i를 2로 나눈 값의 +1을 해준 값이다.
[백준/2775/파이썬] 부녀회장이 될테야 소스코드 N = int(input()) for _ in range (N): k = int(input()) n = int(input()) dp = [[i+1 for i in range (n+1)] for j in range(k+1)] for floor in range (1,k+1): for ho in range (1,n+1): dp[floor][ho] = dp[floor-1][ho] + dp[floor][ho-1] print(dp[k][n-1]) 알고리즘 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120], [1, 4, 10, 20, 35, 56, 84, 1..
[백준/1931/파이썬] 회의실 - Greedy 소스코드 N = int(input()) answer = 0 room = [] endTime = 0 for _ in range (N): room.append(list(map(int,input().split()))) room.sort(key = lambda x : (x[1],x[0])) for i in room: if endTime
[백준/17298/파이썬] 오큰수 - stack 소스코드 k = int(input()) ocs = list(map(int,input().split())) answer = [-1] * k stack = [] for i in range (k): while stack and ocs[i] > ocs[stack[-1]]: answer[stack[-1]] = ocs[i] stack.pop() stack.append(i) print(*answer) 알고리즘 자기보다 큰 수를 찾지 못하는 경우는 -1을 갖게 되므로 초기 세팅을 -1 * k로 해준다. for문을 돌며 자기보다 큰 수를 발견하면 스택에 있는 인덱스를 answer[인덱스]에 그 숫자를 넣어준다. 3 5 2 7 이 입력 되었다고 생각을 해보자 0 1 2 3 1. 3을 비교하는데 3의 인덱스인 0을 스택에 ..
[프로그래머스/주차 요금 계산/파이썬] 소스코드 def solution(fees, records): answer = [] parking = dict() feeTotal = dict() for record in records: recordToList = record.split(" ") if recordToList[2] == "IN": parking[recordToList[1]] = recordToList[0] elif recordToList[2] == "OUT": if recordToList[1] in feeTotal : feeTotal[recordToList[1]] += calculateTime(parking[recordToList[1]],recordToList[0]) else : feeTotal[recordToList[1]] = calcul..