[μ΄κ²μ΄ μ½λ© ν μ€νΈλ€ with Python]_29_λ€μ΄λλ―Ή νλ‘κ·Έλλ° κΈ°μ΄ λ¬Έμ νμ΄
220211 μμ±
<λ³Έ λΈλ‘κ·Έλ γμ΄κ²μ΄ μ·¨μ μ μν μ½λ© ν μ€νΈλ€γ μ youtubeλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€>
https://www.youtube.com/watch?v=tWX6FZwwQMI&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=29
< λ¬Έμ 1 > κ°λ―Έμ μ¬
: μ΅μν ν μΉΈ μ΄μ λ¨μ΄μ§ μλμ°½κ³ λ₯Ό μ½ννμ!
: μΈμ ν μλμ°½κ³ κ³΅κ²©λ°μΌλ©΄ λ€ν¨λ€
: μλμ°½κ³ Nκ°μ λν μ λ³΄κ° μ£Όμ΄μ‘μ λ, μ»μ μ μλ μλμ μ΅λκ° κ΅¬νκΈ°
1) a(i) = iλ²μ§Έ μλμ°½κ³ κΉμ§μ μ΅μ μ ν΄ (μ»μ μ μλ μλμ μ΅λκ°)
2) k(i) = iλ²μ¨° μλμ°½κ³ μ μλ μλμ μ
- python
# μ μ Nμ μ
λ ₯ λ°κΈ°
n = int(input())
# λͺ¨λ μλ μ 보 μ
λ ₯ λ°κΈ°
array = list(map(int, input().split()))
# μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
d = [0] * 100
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν (보ν
μ
)
d[0] = array[0]
d[1] = max(array[0], array[1])
for i in range(2, n):
d[i] = max(d[i - 1], d[i - 2] + array[i])
# κ³μ°λ κ²°κ³Ό μΆλ ₯
print(d[n - 1])
- c++
#include <bits/stdc++.h>
using namespace std;
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int d[100];
int n;
vector<int> arr;
int main(void) {
// μ μ Nμ μ
λ ₯λ°κΈ°
cin >> n;
// λͺ¨λ μλ μ 보 μ
λ ₯λ°κΈ°
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = arr[0];
d[1] = max(arr[0], arr[1]);
for (int i = 2; i < n; i++) {
d[i] = max(d[i - 1], d[i - 2] + arr[i]);
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
cout << d[n - 1] << '\n';
}
- java
import java.util.*;
public class Main {
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
public static int[] d = new int[100];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// μ μ Nμ μ
λ ₯λ°κΈ°
int n = sc.nextInt();
// λͺ¨λ μλ μ 보 μ
λ ₯λ°κΈ°
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = arr[0];
d[1] = Math.max(arr[0], arr[1]);
for (int i = 2; i < n; i++) {
d[i] = Math.max(d[i - 1], d[i - 2] + arr[i]);
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
System.out.println(d[n - 1]);
}
}
< λ¬Έμ 2 > 1λ‘ λ§λ€κΈ°
: μ μ X κ° μ£Όμ΄μ‘μ λ μ°μ°μ μ¬μ©νλ νμμ μ΅μκ° κ΅¬νκΈ°
1) 5λ‘ λλμ΄ λ¨μ΄μ§λ©΄ 5λ‘ λλκΈ°
2) 3μΌλ‘ λλμ΄λ¨μ΄μ§λ©΄, 3μΌλ‘ λλκΈ°
3) 2λ‘ λλμ΄λ¨μ΄μ§λ©΄, 2λ‘ λλκΈ°
4) 1 λΉΌκΈ°
EX) 26 -> 25 -> 5 -> 1
- python
# μ μ Xλ₯Ό μ
λ ₯ λ°κΈ°
x = int(input())
# μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
d = [0] * 1000001
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for i in range(2, x + 1):
# νμ¬μ μμμ 1μ λΉΌλ κ²½μ°
d[i] = d[i - 1] + 1
# νμ¬μ μκ° 2λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if i % 2 == 0:
d[i] = min(d[i], d[i // 2] + 1)
# νμ¬μ μκ° 3μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if i % 3 == 0:
d[i] = min(d[i], d[i // 3] + 1)
# νμ¬μ μκ° 5λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if i % 5 == 0:
d[i] = min(d[i], d[i // 5] + 1)
print(d[x])
- c++
#include <bits/stdc++.h>
using namespace std;
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int d[30001];
int x;
int main(void) {
cin >> x;
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for (int i = 2; i <= x; i++) {
// νμ¬μ μμμ 1μ λΉΌλ κ²½μ°
d[i] = d[i - 1] + 1;
// νμ¬μ μκ° 2λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 2 == 0)
d[i] = min(d[i], d[i / 2] + 1);
// νμ¬μ μκ° 3μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 3 == 0)
d[i] = min(d[i], d[i / 3] + 1);
// νμ¬μ μκ° 5λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 5 == 0)
d[i] = min(d[i], d[i / 5] + 1);
}
cout << d[x] << '\n';
}
- java
import java.util.*;
public class Main {
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
public static int[] d = new int[30001];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
for (int i = 2; i <= x; i++) {
// νμ¬μ μμμ 1μ λΉΌλ κ²½μ°
d[i] = d[i - 1] + 1;
// νμ¬μ μκ° 2λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 2 == 0)
d[i] = Math.min(d[i], d[i / 2] + 1);
// νμ¬μ μκ° 3μΌλ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 3 == 0)
d[i] = Math.min(d[i], d[i / 3] + 1);
// νμ¬μ μκ° 5λ‘ λλμ΄ λ¨μ΄μ§λ κ²½μ°
if (i % 5 == 0)
d[i] = Math.min(d[i], d[i / 5] + 1);
}
System.out.println(d[x]);
}
}
< λ¬Έμ 3 > ν¨μ¨μ μΈ νν ꡬμ±
: N κ°μ§ μ’ λ₯μ νν μμ
: ννλ€ κ°μλ₯Ό μ΅μνμΌλ‘ μ΄μ©ν΄μ κ·Έ κ°μΉμ ν©μ΄ Mμμ΄ λλλ‘ νκΈ°
: M μμ λ§λ€κΈ° μν μ΅μνμ νν κ°μ ꡬνκΈ°
- python
# μ μ N, Mμ μ
λ ₯ λ°κΈ°
n, m = map(int, input().split())
# Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
array = []
for i in range(n):
array.append(int(input()))
# ν λ² κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
d = [10001] * (m + 1)
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0
for i in range(n):
for j in range(array[i], m + 1):
if d[j - array[i]] != 10001: # (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
d[j] = min(d[j], d[j - array[i]] + 1)
# κ³μ°λ κ²°κ³Ό μΆλ ₯
if d[m] == 10001: # μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
print(-1)
else:
print(d[m])
- c++
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> arr;
int main(void) {
// μ μ N, Mμ μ
λ ₯λ°κΈ°
cin >> n >> m;
// Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
// ν λ² κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
vector<int> d(m + 1, 10001);
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = arr[i]; j <= m; j++) {
// (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
if (d[j - arr[i]] != 10001) {
d[j] = min(d[j], d[j - arr[i]] + 1);
}
}
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
if (d[m] == 10001) { // μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
cout << -1 << '\n';
}
else {
cout << d[m] << '\n';
}
}
- java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// μ μ N, Mμ μ
λ ₯λ°κΈ°
int n = sc.nextInt();
int m = sc.nextInt();
// Nκ°μ νν λ¨μ μ 보λ₯Ό μ
λ ₯ λ°κΈ°
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int[] d = new int[m + 1];
Arrays.fill(d, 10001);
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = arr[i]; j <= m; j++) {
// (i - k)μμ λ§λλ λ°©λ²μ΄ μ‘΄μ¬νλ κ²½μ°
if (d[j - arr[i]] != 10001) {
d[j] = Math.min(d[j], d[j - arr[i]] + 1);
}
}
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
if (d[m] == 10001) { // μ΅μ’
μ μΌλ‘ Mμμ λ§λλ λ°©λ²μ΄ μλ κ²½μ°
System.out.println(-1);
}
else {
System.out.println(d[m]);
}
}
}
< λ¬Έμ 4 > κΈκ΄
: n x m ν¬κΈ°μ κΈκ΄μ΄ μλ€
: κ° μΉΈμ νΉμ ν ν¬κΈ°μ κΈμ΄ λ€μ΄κ° μλ°
: 맨 μ²μμλ 첫 μ΄ μ΄λ νμμλ μΆλ° κ°λ₯
: μ΄νμ m-1 λ²μ κ±Έμ³μ λ§€λ² μ€λ₯Έμͺ½ μ, μ€λ₯Έμͺ½, μ€λ₯Έμͺ½ μλ 3κ°μ§ μ€ νλμ μμΉλ‘ μ΄λν΄μΌν¨
: μ±κ΅΄μκ° μ»μ μ μλ κΈμ μ΅λ ν¬κΈ° μΆλ ₯
1) μΌμͺ½ μμμ μ€λ κ²½μ°
2) μΌμͺ½ μλμμ μ€λ κ²½μ°
3) μΌμ‘±μμ μ€λ κ²½μ°
=> κ°μ₯ λ§μ κΈμ κ°μ§κ³ μλ κ²½μ°λ₯Ό ν μ΄λΈμ κ°±μ
- python
for tc in range(int(input())) :
# κΈκ΄ μ 보
n, m = map(int, input().split())
array = list(map(int, input().split()))
# λ€μ΄λλ―Ή νλ‘κ·Έλλ° μν 2μ°¨μ dp ν
μ΄λΈ μ΄κΈ°γ
ν
dp = []
index = 0
for i in range(n) :
dp.append(array[index:index + m])
index += m
# λ€μ΄λλ―Ή νλ‘κ·Έλλ° μ§ν
for j in range(1, m) :
for i in range(n) :
# μΌμͺ½ μ
if i == 0: left_up = 0
else : left_up = dp[i-1][j-1]
# μΌμͺ½ μλ
if i == n - 1 : left_down = 0
else : left_down = dp[i+1][j-1]
# μΌμͺ½
left = dp[i][j-1]
dp[i][j] = dp[i][j] + max(left_up, left_down, left)
result = 0
for i in range(n) :
result = max(result, dp[i][m-1])
print(result)
< λ¬Έμ 5 > λ³μ¬ λ°°μΉνκΈ°
: N λͺ μ λ³μ¬κ° 무μμλ‘ λμ΄
: μ ν¬λ ₯μ΄ λμ λ³μ¬κ° μμͺ½μ μ€λλ‘ λ΄λ¦Όμ°¨μ!
: λ¨μ μλ λ³μ¬μ μκ° μ΅λκ° λλλ‘ νκΈ° μν΄ μ΄μΈμμΌμΌ νλ λ³μ¬μ μ μΆλ ₯
: κ°μ₯ κΈ΄ μ¦κ°νλ λΆλΆ μμ΄ (LIS)
: νΉμ μμΉ λ³μ¬λ μμΈ
- python
n = int(input())
array = list(map(int, input().split()))
# μμ λ€μ§μ΄μ μ΅μ₯ μ¦κ° λΆλΆ μμ΄λ‘ λ³ν
array.reverse()
# λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν 1μ°¨μ DP ν
μ΄λΈ μ΄κΈ°ν
dp = [1] * n
# κ°μ₯ κΈ΄ μ¦κ° LIS μκ³ λ¦¬μ¦
for i in range(1, n) :
for j in range(0, i) :
if array[j] < array[i] :
dp[i] = max(dp[i], dp[j] + 1)
# μ΄μΈ λ³μ¬
print(n-max(dp))