파이썬 144

[프로그래머스] 다단계 칫솔 판매 python, 2021 Dev-Matching: 웹 백엔드 개발자(상반기)

https://programmers.co.kr/learn/courses/30/lessons/77486?language=python3 코딩테스트 연습 - 다단계 칫솔 판매 민호는 다단계 조직을 이용하여 칫솔을 판매하고 있습니다. 판매원이 칫솔을 판매하면 그 이익이 피라미드 조직을 타고 조금씩 분배되는 형태의 판매망입니다. 어느정도 판매가 이루어진 후, programmers.co.kr def solution(enroll, referral, seller, amount): answer = [0] * (len(enroll)) graph_dict = dict(zip(enroll,range(len(enroll)))) for i in range(len(seller)): man = seller[i] price = amo..

STUDY/Algorithm 2021.05.04

[백준] 1074 Z python

www.acmicpc.net/problem/1074 1074번: Z 한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. 만약, N > 1이 라서 www.acmicpc.net N, r, c = map(int, input().split()) def z(N, r, c): result = 0 while N > 0: if N == 1: result += r * 2 + c break else: flag = 4 ** (N - 1) half = (2 ** N) >> 1 N -= 1 if r < half and c < half: continue elif r < half: result += ..

STUDY/Algorithm 2021.05.04

[백준] 11724 연결 요소의 개수 python

www.acmicpc.net/problem/11724 11724번: 연결 요소의 개수 첫째 줄에 정점의 개수 N과 간선의 개수 M이 주어진다. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) 둘째 줄부터 M개의 줄에 간선의 양 끝점 u와 v가 주어진다. (1 ≤ u, v ≤ N, u ≠ v) 같은 간선은 한 번만 주 www.acmicpc.net import sys input = sys.stdin.readline N, M = map(int, input().split()) # 방향 없는 그래프 G = [list() for _ in range(N + 1)] visit = [1] + [0] * N for _ in range(M): u, v = map(int, input().split()) G[u]..

STUDY/Algorithm 2021.05.04

[백준] 11279 최대힙 python

www.acmicpc.net/problem/11279 11279번: 최대 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 www.acmicpc.net import sys;input = sys.stdin.readline from heapq import heappop, heappush h = [] for i in range(int(input())): x = int(input()) if x: heappush(h, -x) else: print(-heappop(h) if h else 0) heapq는 기본이 minheap 이기때문에 마이너스 기호를..

STUDY/Algorithm 2021.05.04

[백준] 9095 1, 2, 3 더하기 C python

www.acmicpc.net/problem/9095 9095번: 1, 2, 3 더하기 각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 출력한다. www.acmicpc.net #define _CRT_SECURE_NO_WARNINGS #include int main() { int arr[11] = { 0, 1, 2, 4 }; int T, n; for (int i = 4; i < 11; i++) { arr[i] += arr[i - 1] + arr[i - 2] + arr[i - 3] * 1; } scanf("%d", &T); for (int i = 0; i < T; i++) { scanf("%d", &n); printf("%d\n", arr[n]); } return 0; } arr = ..

STUDY/Algorithm 2021.05.04

[백준] 2630 색종이 만들기 python

www.acmicpc.net/problem/2630 2630번: 색종이 만들기 첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다. www.acmicpc.net import sys; input = sys.stdin.readline N = int(input()) color_paper = [list(map(int, input().split())) for _ in range(N)] def check(color, r, c, size): for i in range(r, r + size, visit[r][c]): for j in range(c, c + ..

STUDY/Algorithm 2021.05.01

[백준] 1916 최소비용 구하기 python

www.acmicpc.net/problem/1916 1916번: 최소비용 구하기 첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 www.acmicpc.net import sys input = sys.stdin.readline from heapq import heappop, heappush N = int(input()) M = int(input()) link_arr = [list() for _ in range(N + 1)] for _ in range(M): st, en, we = map(int, input().split()) link..

STUDY/Algorithm 2021.04.29

[백준] 1240 노드사이의 거리 python

www.acmicpc.net/problem/1240 1240번: 노드사이의 거리 N(2≤N≤1,000)개의 노드로 이루어진 트리가 주어지고 M(M≤1,000)개의 두 노드 쌍을 입력받을 때 두 노드 사이의 거리를 출력하라. www.acmicpc.net # import sys # input = sys.stdin.readline from collections import deque N, M = map(int, input().split()) link_arr = [list() for _ in range(N + 1)] for _ in range(N - 1): n1, n2, w = map(int, input().split()) link_arr[n1].append((n2, w)) link_arr[n2].append(..

STUDY/Algorithm 2021.04.29

[백준] 2644 촌수계산 python

www.acmicpc.net/problem/2644 2644번: 촌수계산 사람들은 1, 2, 3, …, n (1≤n≤100)의 연속된 번호로 각각 표시된다. 입력 파일의 첫째 줄에는 전체 사람의 수 n이 주어지고, 둘째 줄에는 촌수를 계산해야 하는 서로 다른 두 사람의 번호가 주어진 www.acmicpc.net import sys input = sys.stdin.readline from collections import deque N = int(input()) s, e = map(int, input().split()) link_arr = [list() for _ in range(N + 1)] M = int(input()) for _ in range(M): n1, n2 = map(int, input().s..

STUDY/Algorithm 2021.04.29

[백준] 1107 리모컨 python

www.acmicpc.net/problem/1107 1107번: 리모컨 첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼 www.acmicpc.net import sys input = sys.stdin.readline want_ch = input().rstrip() want_ch_list = list(map(int, list(want_ch))) want_ch = int(want_ch) use_set = set(range(10)) M = int(input()) if M: crashed_set = set(map(int, input().split(..

STUDY/Algorithm 2021.04.28