Total
부대복귀 (BFS, duque)
from collections import deque def solution(n, roads, sources, destination): q = deque([destination]) graph = [[] for _ in range(n+1)] for s, e in roads: graph[s].append(e) graph[e].append(s) visit = [-1]*(n+1) visit[destination] = 0 while q: now = q.popleft() for x in graph[now]: if visit[x] == -1: visit[x] = visit[now]+1 q.append(x) return [visit[i] for i in sources]
기사단원의 무기 (약수의 개수)
def solution(number, limit, power): answer = 0 for x in range(1,number+1): cnt=0 for i in range(1, int(x**(1/2))+1): if x%i==0: cnt+=1 if i**2 != x: #제곱이 되는 약수 중복 방지 cnt+=1 if cnt>limit: cnt=power answer+=cnt return answer
마법의 엘리베이터
def solution(storey): answer = 0 while storey: r = storey % 10 print(storey,r) if r > 5:# 6 ~ 9 answer += (10-r) storey += 10 else: if r==5 and (storey // 10) % 10 > 4: storey += 10 answer+=r storey //= 10 return answer
시저암호(알파벳, 리스트 인덱스 밀기)
from string import ascii_uppercase, ascii_lowercase a = ascii_uppercase b = ascii_lowercase def solution(s, n): answer='' for x in s: if x.islower(): answer+=b[(b.index(x)+n)%len(b)] elif x.isupper(): answer+=a[(a.index(x)+n)%len(a)] else: answer += x return answer
테이블 해시 함수 (정렬)
def solution(data, col, row_begin, row_end): answer = 0 data.sort(key=lambda x: (x[col-1], -x[0])) for i in range(row_begin, row_end+1): result = 0 for x in data[i-1]: result += x % i answer ^= result return answer
H-index
def solution(citations): answer = 0 n=len(citations) citations.sort() for i in range(n): if citations[i]>=n-i: answer += 1 return answer