Skip to content

Commit e43b602

Browse files
committed
Quartz sync: Aug 31, 2025, 11:00 PM
1 parent dfad6c2 commit e43b602

File tree

9 files changed

+734
-3
lines changed

9 files changed

+734
-3
lines changed

content/Computer Science/1 Foundations & Theory/Algorithms/DP.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description:
33
aliases:
44
created: 2025-05-18
5-
modified: 2025-05-23
5+
modified: 2025-08-31
66
tags:
77
- review
88
references:
@@ -187,8 +187,19 @@ references:
187187
- `boolean` 대신 숫자로 한다면, 그 점수에 도달하는 가짓수 도출 가능
188188
- 단 한번만 사용은 위에 행에서 `[i - 1][j - v] | [i - 1][j]`
189189
- 한번만 사용이니, 이전 행에 영향을 받는다
190-
- 무제한 사용은 자기 행에서 `[i][j - v] + [i - 1][j]`,
191-
- 번호 중복 사용, 순서 상관 없는 경우(`{1, 1, 2} == {2, 1, 1}` 즉 같은 집합인 경우)
190+
- 무제한 사용, 순서 상관 있는 경우 (`{1, 1, 2} != {2, 1, 1}`)
191+
- 자기 행에서 `[i][j - v] + [i - 1][j]`
192+
- 무제한 사용, 순서 상관 없는 경우의 타겟($k$) 만드는 갯수
193+
- ```cpp
194+
dp[0] = 1;
195+
for (int i = 0; i < n; ++i) { // n = 동전 개수
196+
int coin = coins[i];
197+
for (int j = coin; j <= k; j++) {
198+
dp[j] += dp[j - coin];
199+
}
200+
}
201+
cout << dp[k];
202+
192203
- [[다이내믹 프로그래밍 완전 정복]] p.152
193204

194205
## 최장 공통 부분 수열 길이 구하기 (LCS)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
tags: ["ps/boj/silver/3", "ps/boj/silver", "cs/algorithms/dynamic-programming/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/1003](https://www.acmicpc.net/problem/1003)
7+
8+
<br/>
9+
10+
# Logic
11+
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
15+
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
25+
26+
### *구현*
27+
- 로직 검증
28+
29+
### *테스트*
30+
- 예외 케이스 고려
31+
32+
<br/>
33+
34+
# My Code
35+
## cpp
36+
```cpp title="boj/1003.cpp"
37+
// https://www.acmicpc.net/problem/1003
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/1003/1003
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
void fast_io() {
53+
ios_base::sync_with_stdio(false);
54+
cin.tie(nullptr);
55+
}
56+
57+
//--------------------------------------------------------------------------------------------------
58+
59+
#define MAX (1234567891)
60+
#define MIN (-1234567891)
61+
62+
#include <iostream>
63+
#include <vector>
64+
65+
struct base {
66+
int zero_count;
67+
int one_count;
68+
};
69+
70+
int main() {
71+
fast_io();
72+
73+
// logic
74+
int t;
75+
cin >> t;
76+
while (t--) {
77+
int n;
78+
cin >> n;
79+
80+
vector<base> dp(n + 1);
81+
dp[0] = { 1, 0 };
82+
dp[1] = { 0, 1 };
83+
84+
if (n >= 2) {
85+
dp[2] = { 1, 1 };
86+
87+
for (int i = 3; i <= n; ++i) {
88+
dp[i] = { dp[i - 1].zero_count + dp[i - 2].zero_count,
89+
dp[i - 1].one_count + dp[i - 2].one_count };
90+
}
91+
}
92+
93+
cout << dp[n].zero_count << " " << dp[n].one_count << "\n";
94+
}
95+
}
96+
97+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
tags: ["ps/boj/silver/3", "ps/boj/silver", "cs/algorithms/dynamic-programming/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/1463](https://www.acmicpc.net/problem/1463)
7+
8+
<br/>
9+
10+
# Logic
11+
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
15+
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
25+
26+
### *구현*
27+
- 로직 검증
28+
29+
### *테스트*
30+
- 예외 케이스 고려
31+
32+
<br/>
33+
34+
# My Code
35+
## cpp
36+
```cpp title="boj/1463.cpp"
37+
// https://www.acmicpc.net/problem/1463
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/1463/1463
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
void fast_io() {
53+
ios_base::sync_with_stdio(false);
54+
cin.tie(nullptr);
55+
}
56+
57+
//--------------------------------------------------------------------------------------------------
58+
59+
#define MAX (1234567891)
60+
#define MIN (-1234567891)
61+
62+
#include <iostream>
63+
#include <vector>
64+
65+
int main() {
66+
fast_io();
67+
68+
// logic
69+
int n;
70+
cin >> n;
71+
72+
vector<int> dp(n + 1, MAX);
73+
dp[0] = 0;
74+
dp[1] = 0;
75+
dp[2] = 1;
76+
dp[3] = 1;
77+
78+
for (int i = 4; i <= n; ++i) {
79+
if (i % 3 == 0) {
80+
dp[i] = min(dp[i], dp[i / 3] + 1);
81+
}
82+
83+
if (i % 2 == 0) {
84+
dp[i] = min(dp[i], dp[i / 2] + 1);
85+
}
86+
87+
dp[i] = min(dp[i], dp[i - 1] + 1);
88+
}
89+
90+
cout << dp[n];
91+
}
92+
93+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
tags: ["ps/boj/silver/2", "ps/boj/silver", "cs/algorithms/dynamic-programming/ps","cs/algorithms/maximum-subarray/ps"]
3+
---
4+
5+
# Problem
6+
- [https://www.acmicpc.net/problem/1912](https://www.acmicpc.net/problem/1912)
7+
8+
<br/>
9+
10+
# Logic
11+
12+
### *분석*
13+
- 문제 유형 (알고리즘...)
14+
- 제약 조건 (인풋 크기, 예외 값, 시공간 복잡도...)
15+
16+
### *설계*
17+
1. 알고리즘 선택
18+
2. 자료구조 선택
19+
3. 수도 코드 작성
20+
4. 정합판단
21+
- `1 ~ 3`과정으로 도출된 로직이 문제를 해결하는가
22+
- 그렇다 -> **구현**
23+
- 잘 모르겠다 -> **구현**
24+
- 아니다 -> 1번부터 다시 점검
25+
26+
### *구현*
27+
- 로직 검증
28+
29+
### *테스트*
30+
- 예외 케이스 고려
31+
32+
<br/>
33+
34+
# My Code
35+
## cpp
36+
```cpp title="boj/1912.cpp"
37+
// https://www.acmicpc.net/problem/1912
38+
// https://codeyoma.github.io/Computer-Science/1-Foundations--and--Theory/Algorithms/ps/boj/1912/1912
39+
#include <iostream>
40+
using namespace std;
41+
42+
#ifdef LOCAL
43+
# define LOG clog
44+
#else
45+
struct nullstream : ostream {
46+
nullstream()
47+
: ostream(nullptr) {}
48+
};
49+
nullstream LOG;
50+
#endif
51+
52+
void fast_io() {
53+
ios_base::sync_with_stdio(false);
54+
cin.tie(nullptr);
55+
}
56+
57+
//--------------------------------------------------------------------------------------------------
58+
59+
#define MAX (1234567891)
60+
#define MIN (-1234567891)
61+
62+
#include <iostream>
63+
64+
int main() {
65+
fast_io();
66+
67+
// logic
68+
69+
// solution 1
70+
// 1차원 dp
71+
72+
// solution 2
73+
// 2차원 dp
74+
75+
// solution 3
76+
// 카데인 알고리즘 (엄밀히 말하자면 dp가 아님)
77+
// 계속 더한 값이, 현재 값보다 작다면 포기하고 현재값부터 다시 더하기
78+
79+
int n;
80+
cin >> n;
81+
82+
int answer = MIN;
83+
int cur_max = 0;
84+
85+
while (n--) {
86+
int temp;
87+
cin >> temp;
88+
89+
cur_max = max(cur_max + temp, temp);
90+
answer = max(answer, cur_max);
91+
}
92+
93+
cout << answer;
94+
}
95+
96+
```

0 commit comments

Comments
 (0)