Data Structures & Algorithms 15

Top-Down vs. Bottom-Up (DP)

동적 프로그래밍의 두 가지 접근 방식: Top-Down vs. Bottom-Up동적 프로그래밍을 사용할 때, 두 가지 방식으로 문제를 해결할 수 있다..탑다운(Top-Down): 재귀(Recursion) + 메모이제이션(Memoization)보텀업(Bottom-Up): 반복문(Iteration)1. 탑다운(Top-Down) 방식: 재귀 + 메모이제이션private static int[] memo = new int[100];public static int fibTopDownMemo(int n) { if (n == 0) return 0; if (n == 1) return 1; if (memo[n] != 0) return memo[n]; // 이미 계산된 값이면 반환 memo[n] = f..

2 conditions for Dynamic Programming

1. 중복되는 부분 문제 (Overlapping Subproblems)✅ 개념문제를 해결하는 과정에서 동일한 부분 문제가 반복적으로 등장해야 합니다.이전에 계산한 값을 저장하여 재사용하면 연산을 줄일 수 있어야 합니다.🔹 예제: 피보나치 수열 (Fibonacci Sequence)피보나치 수열을 재귀적으로 구하는 코드를 보면, 같은 값이 반복적으로 계산됩니다.public static int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n - 1) + fib(n - 2);}❌ fib(5)를 구할 때 fib(3)이 두 번 계산됨.❌ fib(3)을 계산할 때 fib(2)도 중복 계산됨.✅ 중복된 계산을 저장하여 다시 ..

RBST: Convert a Sorted Linked List to a Height-Balanced BST

📌 Problem RequirementsConvert a sorted singly linked list into a height-balanced binary search tree (BST).A height-balanced BST is a binary tree in which the depth of the two subtrees of any node never differs by more than one.📌 Constraints & ConditionsBinary Search Tree (BST) Property:For each node, all values in the left subtree must be less than the node’s value.All values in the right subt..

Priority Queue (Heap): Implement a Task Scheduler (Comparable & Comparator)

Problem StatementYou are given a list of tasks, each with a priority. Your job is to schedule the tasks in order of highest priority first using a priority queue (heap).The priority queue should be implemented using a min-heap or max-heap.Each task has a name and a priority value (lower number = higher priority).The program should process the tasks in order of priority.Example Input & OutputInpu..