문제풀이

·문제풀이/DP
https://www.acmicpc.net/problem/14267코드import sysinput = sys.stdin.readlinen, m = map(int, input().split())boss = [0]+list(map(int, input().split()))compliments = [0]*(n+1)for _ in range(m): i, w = map(int, input().split()) compliments[i] += wfor i in range(2, n+1): compliments[i] += compliments[boss[i]]print(*compliments[1:]) 해당 문제는 트리디피 알고리즘으로 풀 수 있는 문제에 속하긴 하지만, 문제의 조건이 쉬워서 DP만으로도 풀 ..
https://www.acmicpc.net/problem/1036 코드from collections import defaultdictn = int(input())nums = [input() for _ in range(n)]k = int(input())base36 = [str(x) for x in range(10)]+[chr(x) for x in range(65, 91)]def decimal_to_base36(num): if num == 0: return "0" result = "" while num != 0: result = base36[num % 36]+result num //= 36 return resultgap = defaultdict(in..
https://www.acmicpc.net/problem/1016코드from math import sqrt, floormin_val, max_val = map(int, input().split())length = max_val-min_val+1non_square = [True]*lengthfor i in range(2, floor(sqrt(max_val))+1): square_num = i**2 if min_val % square_num == 0: start = (min_val//square_num)*square_num else: start = (min_val//square_num+1)*square_num for j in range(start, min_val..
https://www.acmicpc.net/problem/1041코드from itertools import combinationsn = int(input())dice = list(map(int, input().split()))one_min = min(dice)two_min = int(1e9)for comb in combinations(range(6), 2): if sum(comb) == 5: continue total = dice[comb[0]]+dice[comb[1]] if total  5개의 면에서 어느 부분이 1면만 노출될지, 2면이 노출될지, 3면이 노출될지 생각해보면 다음과 같다. 빨간색으로 표시된 모서리는 3면, 민트색 표시는 2면, 그 외에는 1면이 노출되게 된다..
https://school.programmers.co.kr/learn/courses/30/lessons/12973?language=javascript코드function solution(s) { const stack = []; for (const ch of s) { if (stack.length > 0 && ch === stack[stack.length - 1]) { stack.pop(); } else { stack.push(ch); } console.log(stack); } return stack.length === 0 ? 1 : 0;} 처음에는 배열의 마지막 요소에 접근할 때 파이썬처럼 stack[-1]처럼 했다가 틀렸다....찾아보니 자바스크립트는 길이-..
딜레이레이
'문제풀이' 카테고리의 글 목록 (2 Page)