본문 바로가기

Algorithm220

[Baekjoon] 1904. 01타일 ( 동적 프로그래밍 ) # Problem 지원이에게 2진 수열을 가르쳐 주기 위해, 지원이 아버지는 그에게 타일들을 선물해주셨다. 그리고 이 각각의 타일들은 0 또는 1이 쓰여 있는 낱장의 타일들이다. 어느 날 짓궂은 동주가 지원이의 공부를 방해하기 위해 0이 쓰여진 낱장의 타일들을 붙여서 한 쌍으로 이루어진 00 타일들을 만들었다. 결국 현재 1 하나만으로 이루어진 타일 또는 0타일을 두 개 붙인 한 쌍의 00타일들만이 남게 되었다. 그러므로 지원이는 타일로 더 이상 크기가 N인 모든 2진 수열을 만들 수 없게 되었다. 예를 들어, N=1일 때 1만 만들 수 있고, N=2일 때는 00, 11을 만들 수 있다. (01, 10은 만들 수 없게 되었다.) 또한 N=4일 때는 0011, 0000, 1001, 1100, 1111 등 .. 2021. 4. 22.
[Baekjoon] 11053. 가장 긴 증가하는 부분 수열 ( 동적 프로그래밍 ) # Problem 수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이고, 길이는 4이다. 입력 첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (1 ≤ Ai ≤ 1,000) 출력 첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다. # Solution n= int(input()) array = list(map(int,input().split())) dp=[1]*n m=0 for i in range(1,n): for .. 2021. 4. 22.
❗️[LEET CODE] 310. Minimum Height Trees # Problem A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree .. 2021. 4. 20.
[LEET CODE] 297. Serialize and Deserialize Binary Tree # Problem Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserial.. 2021. 4. 20.
[LEET CODE] 110. Balanced Binary Tree # Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: Constraints: The number of nodes in the tree is in the range [0, 5000]. -104 1: return False return a and b # 서브 트리도 모두 균형인지 확인 # Solution 1 - 재귀 구조로 높이 차이 계산 class Solution: def isBalanced(self, root: TreeNode) -> bool: def check(root): if not root: return 0 left = c.. 2021. 4. 20.
[LEET CODE] 617. Merge Two Binary Trees # Problem You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the no.. 2021. 4. 20.
반응형