import sys
from collections import deque

def push(queue, x):
    queue.append(x)
    
def pop(deque):
    return deque.popleft() if deque else -1

def size(queue):
    return len(queue)

def empty(queue):
    return 1 if not queue else 0

def front(queue):
    return queue[0] if queue else -1

def back(queue):
    return queue[-1] if queue else -1

deque = deque()

N = int(sys.stdin.readline().rstrip())

for _ in range(N):
    order = sys.stdin.readline().rstrip().split()
    
    command = order[0]
    
    if(command == "push"):
        push(deque, order[1])
    elif(command == "pop"):
        sys.stdout.write(str(pop(deque)) + "\n")
    elif(command == "size"):
        sys.stdout.write(str(size(deque)) + "\n")
    elif(command == "empty"):
        sys.stdout.write(str(empty(deque)) + "\n")
    elif(command == "front"):
        sys.stdout.write(str(front(deque)) + "\n")
    elif(command == "back"):
        sys.stdout.write(str(back(deque)) + "\n")

문제는 쉬운데 역시나 시간 맞추기가 힘든 문제.

우리가 잘 알고 있는 pop(0)은 빼고 다시 옮기는 O(n)의 시간이 소요되기 때문에 해당 문제에서는 바로 시간초과가 발생한다.

pop 부분을 이것저것 손봐서 해보긴했지만, 시간 더 소비하지 않고 다른 사람처럼 deque를 사용했다....

결론: collections의 deque 도움을 받자.

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 2680번(python)  (0) 2020.05.03
백준 2164번(python)  (0) 2020.05.03
백준 4949번(python)  (0) 2020.05.02
백준 10773번(python)  (0) 2020.05.02
백준 2004번(python)  (0) 2020.04.28


def push(stack, x):
    stack.append(x)
    
def pop(stack):
    return stack.pop()

def top(stack):
    return stack[-1] if stack else -1

while(True):
    sentence = input()
    
    if(sentence == "."):
        break
    else:
        opend_stack = []
        sentence_check = True
        
        for i in sentence:
            if(i == "(" or i == "["):
                push(opend_stack, i)
            elif i == ")":
                if(top(opend_stack) == "("):
                    pop(opend_stack)
                else:
                    if(not opend_stack or top(opend_stack) == "["):
                        sentence_check = False
                        break
            elif i == "]":
                if(top(opend_stack) == "["):
                    pop(opend_stack)
                else:
                    if(not opend_stack or top(opend_stack) == "("):
                        sentence_check = False
                        break

        if(sentence_check and not opend_stack):
            print("yes")
        else:
            print("no")

중간중간 비어있는 스택이나 문제에서 제공된 예제를 전부 통과하고,

마지막에 "((((" 반례정도만 생각해주면 쉽게 해결 가능할 것입니다.

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 2164번(python)  (0) 2020.05.03
백준 18258번(python)  (0) 2020.05.03
백준 10773번(python)  (0) 2020.05.02
백준 2004번(python)  (0) 2020.04.28
백준 1676번(python)  (0) 2020.04.27


def push(x):
    stack.append(x)

def pop():
    stack.pop()

K = int(input())
stack = []

for _ in range(K):
    num = int(input())
    pop() if num == 0 else push(num)
    
print(sum(stack))

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 18258번(python)  (0) 2020.05.03
백준 4949번(python)  (0) 2020.05.02
백준 2004번(python)  (0) 2020.04.28
백준 1676번(python)  (0) 2020.04.27
백준 9375번(python)  (0) 2020.04.20

 


def div_number(k, n):
    count = 0
    while(k != 0):
        k = k // n
        count += k
    return count

n, m = list(map(int, input().split()))

div_five = div_number(n, 5) - div_number(m, 5) - div_number(n-m, 5)
div_two = div_number(n, 2) - div_number(m, 2) - div_number(n-m, 2)

print(min(div_five, div_two))

 

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 4949번(python)  (0) 2020.05.02
백준 10773번(python)  (0) 2020.05.02
백준 1676번(python)  (0) 2020.04.27
백준 9375번(python)  (0) 2020.04.20
백준 11050번(python)  (0) 2020.04.19


N = int(input())
print(N//5 + N//25 + N//125)

팩토리얼로 얻을 수 있는 수를 인수 분해 했을때, 0이 늘어나는 순간은 10(2x5)가 곱해지는 순간이다.

따라서 5의 개수를 찾으면 쉽다. 예를 들어, 10!이면 5에서 한번, 10에서 한번 --> 그래서 답이 2

원래는 for-loop를 활용해서 조건식은 5를 계속 곱해주고, for-loop 안에서는 5로 나눠주어 5의 갯수를 찾아야 한다.

하지만 문제에서 주어진 N의 범위(0<N<500)가 크지 않으니 그냥 print 문으로 해결.

125도 넣어줘야하는 이유는 N의 범위가 125를 포함하기 때문. 또, 5를 3번 포함하기 때문.

625는 500 범위 밖이니 제외.

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 10773번(python)  (0) 2020.05.02
백준 2004번(python)  (0) 2020.04.28
백준 9375번(python)  (0) 2020.04.20
백준 11050번(python)  (0) 2020.04.19
백준 3036번(python)  (0) 2020.04.19

sudo systemctl status ssh를 입력하면, 대략 아래와 같은 메시지가 나타나는데,

위 에러를 만나면 ssh 통신이 원활하지 않을 수 있다.

에러의 원인은 ssh_key들의 권한이 너무 열려있기 때문인데, 권한을 낮춰주면 이를 해결할 수 있다.

600으로 해보고 안되면 400으로 낮춰서 설정하면 된다.
혹시 모르니 key, key.pub 전부 바꾸길.

chmod 600 ./"your key"

또는

만약, 디렉토리에 존재하는 ssh_key 전부의 권한을 바꾸고 싶다면,
-R 속성을 이용한다.

chmod 600 -R "your directory"

바꾸고 안된다면, 재부팅해보고 다시 시도해보세요.


- (headgear + 1) * (eyewear + 1)으로 문제 해결
- (옷의 종류 + 1) * (...) * ...

첫 번째 예제의 경우,
(headgear) hat turban x
(eyewear) sunglasses x

x는 해당 종류를 입지 않는 것을 의미함.
모든 경우를 구하면 (3x2)-1, -1은 아무것도 입지 않는 경우 제외

n = int(input())
# test case
for _ in range(n):
    _n = int(input())
    
    # each case
    clothes_type = {}
    for _ in range(_n):
    	# 의류 이름과 종류
        name, type = input().split()
        # 의류 종류를 dict의 key로 사용
        if(type in clothes_type):
            clothes_type[type] += 1
        else:
            clothes_type[type] = 1
    
    case = 1
    # 옷의 종류 + 1
    for key in clothes_type.keys():
        case = case * (clothes_type[key] + 1)
    
    print(case - 1)

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 2004번(python)  (0) 2020.04.28
백준 1676번(python)  (0) 2020.04.27
백준 11050번(python)  (0) 2020.04.19
백준 3036번(python)  (0) 2020.04.19
백준 2981번(python)  (1) 2020.04.18


N, K = list(map(int, input().split()))

up = 1
for i in range(N, N-K, -1):
    up *= i

down = 1
for i in range(1, K+1):
    down *= i

print(up // down)

math.factorial 활용

from math import factorial

N, K = list(map(int, input().split()))

print(factorial(N)//(factorial(K) * factorial(N-K)))

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 1676번(python)  (0) 2020.04.27
백준 9375번(python)  (0) 2020.04.20
백준 3036번(python)  (0) 2020.04.19
백준 2981번(python)  (1) 2020.04.18
백준 2609번(python)  (0) 2020.04.10


def get_gca(a, b):
    if(b > a):
        num_1 = b
        num_2 = a
    else:
        num_1 = a
        num_2 = b
    
    if(num_1 % num_2 == 0):
        return num_2
    
    while(num_1 % num_2):
        gca = num_1 % num_2
        num_1 = num_2
        num_2 = gca
        
    return gca

N = int(input())
ring_radius = list(map(int, input().split()))

for i in ring_radius[1:]:
    gca = get_gca(ring_radius[0], i)
    print(f'{int(ring_radius[0]/gca)}/{int(i/gca)}')

 

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 9375번(python)  (0) 2020.04.20
백준 11050번(python)  (0) 2020.04.19
백준 2981번(python)  (1) 2020.04.18
백준 2609번(python)  (0) 2020.04.10
백준 1037번(python)  (0) 2020.04.09


def get_gcd(a, b):
    if(b > a):
        num_1 = b
        num_2 = a
    else:
        num_1 = a
        num_2 = b
        
    if(num_1 % num_2 == 0):
        return num_2
        
    while(num_1 % num_2):
        gcd = num_1 % num_2
        num_1 = num_2
        num_2 = gcd
        
    return gcd

N = int(input())
num_list = [int(input()) for _ in range(N)]
num_list = sorted(num_list, reverse = True)

diff_list = [a - b for a, b in zip(num_list, num_list[1:] + [0])][:-1]

init_gcd = diff_list[0]
for i in range(1, len(diff_list)):
    init_gcd = get_gcd(init_gcd, diff_list[i])

results = []

for i in range(2, int(init_gcd ** (1/2)) + 1):
    if(init_gcd % i == 0):
        results.append(i)
        if(init_gcd // i != i):
            results.append(init_gcd // i)
results.append(init_gcd)
results = sorted(set(results))

print(''.join([str(i) + ' ' for i in results])[:-1])

각 리스트의 차이를 구하고, 그 차이들의 최대 공약수의 약수를 출력.

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 11050번(python)  (0) 2020.04.19
백준 3036번(python)  (0) 2020.04.19
백준 2609번(python)  (0) 2020.04.10
백준 1037번(python)  (0) 2020.04.09
백준 5086번(python)  (0) 2020.04.09