본문 바로가기

알고리즘141

[LEET CODE] 215. Kth Largest Element in an Array # Problem Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Constraints: 1 int: return sorted(nums, reverse=True)[k - 1] # 힙 ( heap ) 최소힙 - 부모가 항상 자식보다 작거나 같다 ( 좌우 트리는 정렬된 구조는 아님) 최대힙 - 부모가 항상 자식보다 크거나 같다 거의 완전한 트리인 특수한 트리 기반 자료구조 우선순위 큐 구현 2021. 5. 26.
[LEET CODE] 105. Construct Binary Tree from Preorder and Inorder Traversal # Problem Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. Constraints: 1 2021. 5. 24.
[LEET CODE] 783. Minimum Distance Between BST Nodes # Problem Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree. Constraints: The number of nodes in the tree is in the range [2, 100]. 0 int: if root.left: self.minDiffInBST(root.left) self.result = min(self.result, root.val - self.prev) self.prev = root.val if root.right: self.minDiffInBST(root.right) return self.r.. 2021. 5. 24.
[LEET CODE] 938. Range Sum of BST # Problem Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Constraints: The number of nodes in the tree is in the range [1, 2 * 104]. 1 L: stack.append(node.left) if node.val < R: stack.append(node.right) if L L: stack.append(node.left) if node.val < R: stack.append(node.right) if L O.. 2021. 5. 24.
[LEET CODE] 1038. Binary Search Tree to Greater Sum Tree # Problem Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. As a reminder, a binary search tree is a tree that satisfies these constraints: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of.. 2021. 5. 24.
[Baekjoon] 2014. 소수의 곱 ( 탐욕 알고리즘 ) # Problem K개의 소수가 있다. 이때, 이 소수들 중에서 몇 개를 곱해서 얻게 되는 수들이 있을 것이다. 소수들을 선택할 때에는 같은 수를 선택해도 되며, 주어지는 소수 자체도 포함시키자. 예를 들어 세 소수가 2, 5, 7이었다면, 이러한 곱들을 오름차순으로 나타내 보면, 2, 4, 5, 7, 8, 10, 14, 16, 20, 25, 28, 32, 35, 등이 된다. K개의 소수가 주어졌을 때, 이러한 소수의 곱들 중에서 N번째 수를 구해 보자. 단 정답은 231보다 작은 자연수이다. 입력 첫째 줄에 K(1 ≤ K ≤ 100), N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 K개의 소수가 오름차순으로 주어진다. 같은 소수가 여러 번 주어지는 경우는 없으며, 주어지는 소수는 모두 54.. 2021. 5. 16.
반응형