본문 바로가기

전체 글55

Numpy 배열에서 0이 아닌 값 찾기 Numpy 라이브러리를 불러와서 4*3 크기의 행렬 x 생성 In [1]: import numpy as np x = np.array([[0, 1, 0], [2, 5, 0], [3, 1, 2], [0, 0, 3]]) x Out[1]: array([[0, 1, 0], [2, 5, 0], [3, 1, 2], [0, 0, 3]]) n차원의 행렬 x에 0이 아닌 값이 k개 있다고 할때, np.nonzero(x)를 실행하면 길이 k인 array n개가 반환된다. 각 array는 0이 아닌 값들의 i번째 차원 index를 저장하고 있음 x의 0행 1열, 1행 0열, 1행 1열... 3행 2열까지 모두 0이 아님을 확인 가능 In [2]: np.nonzero(x) Out[2]: (array([0, 1, 1, 2, 2,.. 2023. 3. 29.
백준 24519번 : Bottleneck TSP (Large) (Python) 출처 : https://www.acmicpc.net/problem/24519 n, m = map(int, input().split()) INF = int(1e8) graph = [[INF] * n for _ in range(n)] for _ in range(m) : u, v, c = map(int, input().split()) graph[u-1][v-1] = c dp = [[0] * (1 2023. 3. 24.
백준 13701번 : 중복 제거 (Python) 출처 : https://www.acmicpc.net/problem/13701 import sys arr = bytearray(2**22) s = "" while True : c = sys.stdin.read(1) if c.isnumeric() : s += c else : n = int(s) d = n // 8 r = n % 8 if not arr[d] & (1 2023. 3. 20.
백준 19585번 : 전설 (Python) (트라이 없이) 출처 : https://www.acmicpc.net/problem/19585 import sys input = sys.stdin.readline c, n = map(int, input().split()) tree_c = dict() set_n = set() def insert(tree, word) : cur = tree for char in word : if char not in cur : cur[char] = dict() cur = cur[char] cur["*"] = len(word) def startswith(tree, word) : cur = tree for char in word : if char not in cur : return False cur = cur[char] if "*" in cur.. 2023. 3. 14.