본문 바로가기

Python265

[Python] 파이썬 자료구조 연산 시간복잡도 정리 리스트 자료형(list) 메서드의 시간복잡도 append, pop(), clear(), length 👉🏻 O(1) ==, !=, insert(), delete, pop(i), del, copy(), in, min(), max(), reverse() 👉🏻 O(n) sort() 👉🏻 O(nLog n) 집합 자료형(set) 메서드의 시간복잡도 add(), in, remove, pop(), clear() 👉🏻 O(1) copy() 👉🏻 O(n) 사전 자료형(dict) 메서드의 시간복잡도 store, len(), del, get, pop(k), clear(), keys(), values()👉🏻 O(1) 2023. 5. 13.
[HackerRank] Pairs def pairs(k, arr): # Write your code here # length = len(arr) count = 0 new_set = set(arr) for ele in arr: target_one = ele+k target_two = ele-k if target_one in new_set: count += 1 if target_two in new_set: count += 1 new_set.remove(ele) # for i in range(length): # for j in range(i+1,length): # if abs(arr[i]-arr[j]) == k: # count += 1 return count 이번 문제는 주어진 배열(arr)과 타겟 값(k)가 주어졌을 때, 두 수의 차가 k인.. 2023. 5. 13.
[HackerRank] Connected Cells in a Grid def bfs(x,y,area,row,column,visited): if area[x][y] != 1 or visited[x][y]: return 0 count = 1 visited[x][y] = True queue = [(x,y)] nx = [-1,1,0,0,-1,-1,1,1] ny = [0,0,-1,1,-1,1,-1,1] while queue: v = queue.pop(0) for i in range(8): temp_x = v[0] + nx[i] temp_y = v[1] + ny[i] if temp_x >= 0 and temp_x = 0 and temp_y < column: if area[temp_x][temp_y] == 1 and not visited[temp_x][.. 2023. 5. 12.
[HackerRank] KnightL on a Chessboard def findMinimum(a,b,n): area = [[0]*n for _ in range(n)] visited = [[False]*n for _ in range(n)] nx = [a, a, -a, -a, b, b, -b, -b] ny = [b, -b, b, -b, a, -a, a, -a] queue =[[0,0]] visited[0][0] = True while queue: v = queue.pop(0) for i in range(8): temp_x = v[0] + nx[i] temp_y = v[1] + ny[i] if temp_x >= 0 and temp_x =0 and temp_y < n: if not visited[temp_x][temp_y]: if area[tem.. 2023. 5. 12.
[백준 14502번] 연구소 import copy def dfs(graph, x, y): if graph[x][y] != 2: return for i in range(4): temp_x = x + nx[i] temp_y = y + ny[i] if temp_x >=0 and temp_x =0 and temp_y < m: if graph[temp_x][temp_y] == 0: graph[temp_x][temp_y] = 2 dfs(graph,temp_x,temp_y) def count_safe_area(graph): count = 0 for i in range(n): for j in range(m): if graph[i][j] == 0: count += 1 return count n, m = map(int,in.. 2023. 5. 10.
[백준 16194번] 카드 구매하기 2 n = int(input()) data = list(map(int,input().split())) p_count = len(data) dp = [[0]*(n+1) for _ in range(p_count+1)] for i in range(1,n+1): dp[1][i] = data[0]*i for i in range(2,p_count+1): for j in range(1,n+1): if i > j: dp[i][j] = dp[i-1][j] else: dp[i][j] = min(dp[i-1][j], data[i-1] + dp[i][j-i]) print(dp[p_count][n]) 이번 문제는 P1부터 PN까지 카드가 N개가 포함된 카드 팩의 가격이 주어졌을 때, N개의 카드를 구매하기 위해 지불해야 하는 금액.. 2023. 5. 4.