728x90
반응형

Algorithm Problems 125

[백준][Python] 15828번 Router - 원형 큐

https://www.acmicpc.net/problem/15828 15828번: Router 인터넷을 사용하기 위해서는 컴퓨터에 인터넷 회선을 연결하거나 Wi-Fi를 연결해야 한다. 이렇게 연결된 네트워크를 통해 컴퓨터에는 통신이 가능하다. 마음에 드는 노래나 동영상이 있는 곳에 www.acmicpc.net Python import sys input = sys.stdin.readline n = int(input()) router = [0]*n front, rear = 0, -1 length = 0 x = int(input()) while x != -1: if x == 0: front = (front+1)%n length -= 1 elif length < n: rear = (rear+1)%n router..

Algorithm Problems 2021.08.01

[백준][Python] 2751번 수 정렬하기2 - MergeSort

https://www.acmicpc.net/problem/2751 2751번: 수 정렬하기 2 첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 절댓값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다. www.acmicpc.net Python import sys input = sys.stdin.readline n = int(input()) arr = [int(input()) for _ in range(n)] def merge(low, mid, high): i, j = low, mid+1 sort = [] while i

Algorithm Problems 2021.07.29

[백준][Python] 18258번 큐2, 10866번 덱 - 연결 리스트

https://www.acmicpc.net/problem/18258 18258번: 큐 2 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 www.acmicpc.net Python import sys input = sys.stdin.readline class Node: def __init__(self, data, link): self.data = data self.link = link head = None tail = None length = 0 for _ in range(int(input())): cmd = input().split()..

Algorithm Problems 2021.07.29

[백준/Python] 14717번 앉았다 - BruteForce

https://www.acmicpc.net/problem/14717 14717번: 앉았다 영학이의 패를 뜻하는 두 개의 정수 A, B가 주어진다. (1 ≤ A, B ≤ 10) www.acmicpc.net Python import sys input = sys.stdin.readline a, b = map(int, input().split()) total = 9*17 ans = 0 if a == b: ans = total - (10-a) else: mypoint = (a+b) % 10 for i in range(1, 11): for j in range(i+1, 11): if mypoint > (i+j) % 10: if i == a and j == b: continue elif i == a or j == a ..

Algorithm Problems 2021.07.20

[백준/Python] 숫자 정사각형 - BruteForce

https://www.acmicpc.net/problem/1051 1051번: 숫자 정사각형 N*M크기의 직사각형이 있다. 각 칸은 한 자리 숫자가 적혀 있다. 이 직사각형에서 꼭짓점에 쓰여 있는 수가 모두 같은 가장 큰 정사각형을 찾는 프로그램을 작성하시오. 이때, 정사각형은 행 또는 www.acmicpc.net Python import sys imput = sys.stdin.readline n, m = map(int, input().split()) arr = [list(map(int, list(input()))) for _ in range(n)] ans = 1 for i in range(n): for j in range(m): row, col = [], [] for t in range(i+1, n):..

Algorithm Problems 2021.07.20

[백준] [Python] 2606번 바이러스 - BFS

https://www.acmicpc.net/problem/2606 2606번: 바이러스 첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어 www.acmicpc.net Python from collections import deque import sys input = sys.stdin.readline n = int(input()) graph = [ [] for _ in range(n+1) ] for _ in range(int(input())): s, e = map(int, input().split()) # 양방향 그래프일 때 graph[s].append(e) grap..

Algorithm Problems 2021.07.06

[백준] [Python] 11053번 가장 긴 증가하는 부분 수열 (LIS) - DP

https://www.acmicpc.net/problem/11053 11053번: 가장 긴 증가하는 부분 수열 수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이 www.acmicpc.net Python import sys input = sys.stdin.readline n = int(input()) arr = list(map(int, input().split())) dp = [1]*n for i in range(n): for j in range(i): if arr[i] > arr[j]: dp[i] = ma..

Algorithm Problems 2021.06.28

[백준] [Python] 2294번 동전2 - DP

https://www.acmicpc.net/problem/2294 2294번: 동전 2 첫째 줄에 n, k가 주어진다. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) 다음 n개의 줄에는 각각의 동전의 가치가 주어진다. 동전의 가치는 100,000보다 작거나 같은 자연수이다. 가치가 같은 동전이 여러 번 주 www.acmicpc.net Python #import math import sys input = sys.stdin.readline n, m = map(int, input().split()) coins = list(set([ int(input()) for _ in range(n)])) coins.sort() nums = [float("inf")]*(m+1) nums[0] = 0 for x in ..

Algorithm Problems 2021.06.27

[백준] [Python] 1389번 케빈 베이컨의 6단계 법칙 - BFS/플로이드 워샬

https://www.acmicpc.net/problem/1389 1389번: 케빈 베이컨의 6단계 법칙 첫째 줄에 유저의 수 N (2 ≤ N ≤ 100)과 친구 관계의 수 M (1 ≤ M ≤ 5,000)이 주어진다. 둘째 줄부터 M개의 줄에는 친구 관계가 주어진다. 친구 관계는 A와 B로 이루어져 있으며, A와 B가 친구라는 뜻 www.acmicpc.net Python BFS 메모리 초과 제한이 128MB이라 ans를 2차원으로 모두 남기려 하면 초과될까봐 한 줄씩 계산했다. 그런데 N과 M이 각각 100, 5000 이하라 이 정도는 2차원 배열로 해도 괜찮았다. 그러면 플로이드 워샬 알고리즘을 정확하게 적용하여 풀 수 있다. 아래 답안은 BFS로 푼 답안이다. import sys input = sys..

Algorithm Problems 2021.06.22

[백준] [Python] 18258번 큐2

https://www.acmicpc.net/problem/18258 18258번: 큐 2 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 www.acmicpc.net Python class Node: def __init__(self, elem, link=None): self.data = elem self.link = link class LinkedQueue: def __init__(self): self.head = None self.tail = None self.length = 0 def isEmpty(self): return self..

Algorithm Problems 2021.06.20
728x90
반응형