-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6265c0
commit b8861ed
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# DP | ||
|
||
- 하나의 큰 문제를 여러 개의 작은 문제로 나누어서 그 결과를 저장하여 다시 큰 문 | ||
제를 해결할 때 사용하는 것 | ||
|
||
 | ||
|
||
|
||
[알고리즘 - Dynamic Programming(동적 계획법)](https://hongjw1938.tistory.com/47) | ||
|
||
## 시간복잡도 | ||
|
||
- O(2^n) ⇒ O(n) | ||
|
||
## DP 사용 조건 | ||
|
||
- 겹치는 부분이 있어야 한다. | ||
- 계산한 값을 재사용해야 하기 때문에 작은 문제로 나눴을 때 동일한 문제가 많아 | ||
야 한다 | ||
- 최적 부분 구조 | ||
- 작은 문제의 최적 결과값이 큰 문제의 최적 결과값이 될 수 있는 경우에 적합하다 | ||
|
||
## 구현 방법 | ||
|
||
- Top-Down (Memoization 방식) | ||
- 재귀 사용 | ||
```jsx | ||
fuction fibonachi(n) { | ||
if (n === 1) return 1; | ||
if (n === 2) return 1; | ||
return fibonachi(n-2) + fibonachi(n-2); | ||
``` | ||
- Bottom-Up (Tabulation 방식) | ||
- 보통 반복문 사용 (예시는 재귀) | ||
```jsx const memory = [0]; | ||
function fibonachi(n) { | ||
if(n === 1) return 1; | ||
if(n === 2) return 1; | ||
if(memory[n] != null) return memory[n]; | ||
return memory[n] = fibonachi(n-1) + fibonachi(n-2); | ||
} | ||
``` | ||
```jsx | ||
memory = [0, 1, 1, 2, 3, 5, 8, 13, ...] | ||
``` | ||
## 점화식 | ||
```jsx | ||
fibonachi(n) = fibonachi(n-1) + fibonachi(n-2) | ||
``` |