본문 바로가기

Algorithm101

[BOJ]1018 | 체스판 다시 칠하기 1018 | 체스판 다시 칠하기 https://www.acmicpc.net/problem/1018 작성 코드 M, N = map(int, input().split()) board_input = [] for _ in range(M): board_input.append(input()) count = [] first = 'B' for a in range(M-8): for b in range(N-8): board = board_input[a:a+9][b:b+9] for i in range(8): for j in range(8): if (i+j) % 2 == 0: if board[i][j] != first: w_count += 1 else: if board[i][j] == first: w_count += 1 co.. 2022. 2. 14.
[BOJ] 2839 | 설탕배달 2839 | 설탕배달 https://www.acmicpc.net/problem/2839 작성 코드 N = int(input()) N = int(input()) ex = False for i in range(N // 3 + 1): for j in range(N // 5 + 1): if N == 3 * i + 5 * j: ex = True print(i + j) break if ex: break if not ex: print(-1) for 문 2개 사용 이상 코드 N = int(input()) bag = 0 while N >= 0: # If dived into 5, div and print bag if N % 5 == 0: bag += N // 5 print(bag) break # If not dived i.. 2022. 2. 11.
[알고리즘] 파이썬 DFS | 깊이 우선 탐색 DFS - Depth first search - 깊이 우선 탐색 - 그래프 자료에서 데이터를 탐색하는 알고리즘 graph = dict() graph['A'] = ['B', 'C'] graph['B'] = ['A', 'D'] graph['C'] = ['A', 'G', 'H', 'I'] graph['D'] = ['B', 'E', 'F'] graph['E'] = ['D'] graph['F'] = ['D'] graph['G'] = ['C'] graph['H'] = ['C'] graph['I'] = ['C', 'J'] graph['J'] = ['I'] 핵심 방문이 필요한 노드와 방문한 노드 분리 구현 코드 stack 구현 def dfs(graph, start_node): need_visited, visited =.. 2022. 2. 10.
[BOJ] 11659 | 구간 합 구하기 4 구간 합 구하기 4 https://www.acmicpc.net/problem/11659 작성 코드 import sys n, m = map(int, sys.stdin.readline().split()) nums = list(map(int, sys.stdin.readline().split())) for _ in range(m): i, j = map(int, sys.stdin.readline().split()) print(sum(nums[i-1:j])) 인덱스 슬라이싱을 통해 부분 배열을 구한 후 sum 시간 초과 이상 코드 누적 합 import sys n, m = map(int, sys.stdin.readline().split()) nums = list(map(int, sys.stdin.readline()... 2022. 2. 9.
[BOJ] 18111 | 마인크래프트 18111 | 마인크래프트 https://www.acmicpc.net/problem/18111 작성 코드 import sys n, m, b = map(int, sys.stdin.readline().split()) mine = [] for _ in range(n): mine += map(int, sys.stdin.readline().split()) times = [] mine_maxs = [] mine_max = max(mine) for _ in range(256): time = 0 need = 0 over = 0 for i in mine: if mine_max > i: need += mine_max - i elif mine_max over .. 2022. 2. 8.
[프로그래머스] 62048 | 멀쩡한 사각형 62048 | 멀쩡한 사각형 https://programmers.co.kr/learn/courses/30/lessons/62048 작성 코드 def gcd(a: int, b: int) -> int: a, b = max(a, b), min(a, b) while b != 0: a, b = b, a % b return a def solution(w: int, h: int) -> int: gcd_res = gcd(w, h) if gcd_res == 1: return w * h - (w + h -1) else: return w * h - (((w // gcd_res) + (h // gcd_res) - 1) * gcd_res) 2022. 2. 8.
반응형