Tags
- Python
- sort
- ๊น์ด ์ฐ์ ํ์
- SpringBoot
- ๊ทธ๋ํ ์ด๋ก
- ๋ฐฑํธ๋ํน
- ๋ฌธ์์ด
- ์ ์๋ก
- ๋๋น ์ฐ์ ํ์
- DP
- dfs
- ๊ทธ๋ํ ํ์
- Java
- BOJ
- greedy
- Brute Force Algorithm
- ์ ๋ ฌ
- ๊ต์ฌ
- BFS
- PGM
- stack
- LV2
- queue
- ๊ตฌํ
- ์ํ
- Study
- ์๋ฃ๊ตฌ์กฐ
- ์๋ฎฌ๋ ์ด์
- CodingTest
- Dynamic Programming
Archives
๊ธฐ๋ก๋ฐฉ
BOJ_14888 : ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ ๋ณธ๋ฌธ
๐ธ ๋ฌธ์ ๋ถ์ ๐ธ
- ๊ณ ์ ๋ ์์น์ ์ซ์์, ๊ทธ ์ฌ์ด์ ํ๋ ์ฉ ๋ค์ด๊ฐ ์ ์๋ 4์ข
๋ฅ์ ์ฐ์ฐ์๋ค์ ๊ฐ์๊ฐ ์ฃผ์ด์ง๋ค.
- ๊ณ์ฐ์ผ๋ก ๋ง๋ค ์ ์๋ ๊ฐ์ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ์ถ๋ ฅํ๋ค.
- ์ฐ์ฐ์ ์๋ฆฌ๋ฅผ ๋ฐ๊พธ๋ฉด ๊ฐ์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ ์์ด๋ก ํ์ดํ ์ ์๋ค.
- ๊ฐ์ ์ข ๋ฅ์ ์ฐ์ฐ์๋ ์๋ฆฌ๋ฅผ ๋ฐ๊ฟ๋ ์ค๋ณต๊ณ์ฐ์ด๊ธฐ ๋๋ฌธ์ dfs๋ nextpermutation์ผ๋ก ์ค๋ณต์ ์ ๊ฑฐํ ์ ์๋ค.
๐ธ ์์ด ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static int n, max, min;
private static int[] numbers, orders, picks;
private static boolean[] used;
private static void perm(int idx) { // ์ฐ์ฐ์ ์์ ์์ด, idx : orders์์ ํ์ฌ ๊ณ ๋ฅธ ์ฐ์ฐ์ ์
if (idx == n - 1) { // ์ฐ์ฐ์ ๋ชจ๋ ๊ณ ๋ฆ
// System.out.println(Arrays.toString(picks));
cal_all();
} else {
for (int i = 0; i < n - 1; i++) {
if (!used[i]) { // orders์ i๋ฒ์งธ ์ฐ์ฐ์ ์ฌ์ฉ ์ํ์ผ๋ฉด ์ฌ์ฉ
used[i] = true;
picks[idx] = orders[i]; // ์ฌ์ฉ ์ฐ์ฐ์ ๊ธฐ๋ก
perm(idx + 1); // ์์ด ์ฌ๊ท
used[i] = false;
}
}
}
}
private static void cal_all() {
// print();
int num = numbers[0];
for (int i = 1; i < n; i++) {
num = calc(num, numbers[i], picks[i - 1]);
}
max = Math.max(max, num);
min = Math.min(min, num);
}
private static int calc(int num1, int num2, int order) {
switch (order) {
case 0:
return num1 + num2;
case 1:
return num1 - num2;
case 2:
return num1 * num2;
case 3:
return num1 / num2;
}
return 0;
}
private static void print() {
char[] os = {'+', '-', '*', '/'};
System.out.print(max + " " + min + " " + numbers[0]);
for (int i = 1; i < n; i++) {
System.out.print(" " + os[picks[i - 1]] + " " + numbers[i]);
}
System.out.println();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
numbers = new int[n];
orders = new int[n - 1];
picks = new int[n - 1];
used = new boolean[n - 1];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
numbers[i] = Integer.parseInt(st.nextToken());
}
int idx = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
int count = Integer.parseInt(st.nextToken());
for (int j = 0; j < count; j++) {
orders[idx++] = i;
}
}
perm(0);
System.out.println(max + "\n" + min);
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- perm() ๋ฉ์๋์์ n-1๊ฐ์ ์ฐ์ฐ์๋ฅผ ์ธ๋ฑ์ค๋ก ๊ตฌ๋ถํด์ ์์ด๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ค.
- n-1๊ฐ๋ฅผ ๋ค ๊ณจ๋์ผ๋ฉด, cal_all() ๋ฉ์๋์์ ๊ฐ์ ๊ณ์ฐํ๊ณ ์ต๋๊ฐ, ์ต์๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.
๐ธ DFS ๐ธ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main { // dfs ํ์ด
static int[] numbers, cards;
static int N, ansMax, ansMin;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
numbers = new int[N];
cards = new int[4]; // ์ฐ์ฐ์๋ ์ซ์๋ณด๋ค ํ ๊ฐ ์ ์
ansMax = Integer.MIN_VALUE;
ansMin = Integer.MAX_VALUE;
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
numbers[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
cards[i] = Integer.parseInt(st.nextToken());
}
dfs(0, numbers[0], cards[0], cards[1], cards[2], cards[3]);
System.out.println(ansMax);
System.out.println(ansMin);
}
private static void dfs(int idx, int tmp, int a, int b, int c, int d) {
if (idx == N - 1) {
ansMax = Math.max(tmp, ansMax);
ansMin = Math.min(tmp, ansMin);
return;
}
if (a > 0) {
dfs(idx + 1, tmp + numbers[idx + 1], a - 1, b, c, d);
}
if (b > 0) {
dfs(idx + 1, tmp - numbers[idx + 1], a, b - 1, c, d);
}
if (c > 0) {
dfs(idx + 1, tmp * numbers[idx + 1], a, b, c - 1, d);
}
if (d > 0) {
dfs(idx + 1, tmp / numbers[idx + 1], a, b, c, d - 1);
}
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- dfs() ๋ฉ์๋์์ 4๊ฐ์ง ์ฐ์ฐ์(+, -, *, /)์ ๊ฐ์๊ฐ ๋จ์์์ผ๋ฉด ํ๋์ฉ ์ฌ์ฉํ๋ฉฐ ์ฌ๊ทํ๋ ๋ฐฉ์์ผ๋ก ์ค๋ณต์์ด ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋น๊ตํด๋ณธ๋ค.
- ๊ณ์ฐ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ๋ค๊ณ ๋ค๋๋ ๊ฒฝ์ฐ๊ฐ, ๊ฐ์ ๋ฐ๋ก ์ ์ฅํ ๋ํ๊ณ ๋นผ๋ ๋ฐฉ์๋ณด๋ ์กฐ๊ธ ๋ ๋นจ๋๋ค.
๐ธ NP๐ธ
import java.util.Arrays;
import java.util.Scanner;
public class Main { // np ํ์ด
static int[] cards;
static int ansMax, ansMin, N;
static int[] numbers;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
numbers = new int[N];
cards = new int[N - 1]; // ์ฐ์ฐ์๋ ์ซ์๋ณด๋ค ํ ๊ฐ ์ ์
ansMax = Integer.MIN_VALUE;
ansMin = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
numbers[i] = sc.nextInt();
}
int cardIdx = 0;
for (int i = 0; i < 4; i++) {
int count = sc.nextInt();
for (int j = 0; j < count; j++) {
cards[cardIdx++] = i;
}
}
// System.out.println(Arrays.toString(cards));
Arrays.sort(cards);
do {
//print();
cal_all();
} while (np());
System.out.println(ansMax);
System.out.println(ansMin);
}
private static boolean np() {
// step1. ๋ค์ชฝ๋ถํฐ ๊ผญ๋๊ธฐ๋ฅผ ์ฐพ๋๋ค. (๊ผญ๋๊ธฐ ๋ฐ๋ก ์์ด ๊ตํํ ์๋ฆฌ)
int n = cards.length;
int i = n - 1;
while (i > 0 && cards[i - 1] >= cards[i])
i--;
if (i == 0) // ์ด๋ฏธ ๋ง์ง๋ง ์์ด์
return false;
// stemp2. ๊ผญ๋๊ธฐ ๋ฐ๋ก ์(i-1) ์๋ฆฌ์ ๊ตํํ ๊ฐ์ ๋ค์ชฝ๋ถํฐ ์ฐพ๋๋ค.
int j = n - 1;
while (cards[i - 1] >= cards[j]) --j; // ๋ฌด์กฐ๊ฑด ํ๋๋ฅผ ์ฐพ๋๋ค(๊ผญ๋๊ธฐ๊ฐ ์๊ธฐ ๋๋ฌธ)
// step3. ๊ผญ๋๊ธฐ ๋ฐ๋ก ์(i-1)์๋ฆฌ์ ๊ทธ ์๋ฆฌ๊ฐ๋ณด๋ค ํ ๋จ๊ณ ํฐ ์๋ฆฌ(j) ์์ ๊ตํ
swap(cards, i - 1, j);
// step4. ๊ผญ๋๊ธฐ๋ถํฐ ๋งจ ๋ค๊น์ง ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
int k = n - 1;
while (i < k) {
swap(cards, i++, k--);
}
return true;
}
private static void swap(int[] input, int i, int j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
private static void cal_all() {
int num = numbers[0];
for (int i = 1; i < N; i++) {
num = calc(num, numbers[i], cards[i - 1]);
}
ansMax = Math.max(ansMax, num);
ansMin = Math.min(ansMin, num);
}
private static int calc(int num1, int num2, int order) {
switch (order) {
case 0:
return num1 + num2;
case 1:
return num1 - num2;
case 2:
return num1 * num2;
case 3:
return num1 / num2;
}
return 0;
}
private static void print() {
char[] os = {'+', '-', '*', '/'};
System.out.print(ansMax + " " + ansMin + " " + numbers[0]);
for (int i = 1; i < N; i++) {
System.out.print(" " + os[cards[i - 1]] + " " + numbers[i]);
}
System.out.println();
}
}
๐ธ ์ฝ๋ ํด์ ๐ธ
- Next Permutaion ์ ํ์ฉํด์ ์ค๋ณต์์ด ์ฐ์ฐ์๋ฅผ ๊ณ ๋ฅธ ๋ฐฉ๋ฒ์ด๋ค.
- ์ ๋ ฅ๋ ์ฐ์ฐ์ ๋ฒํธ๋ฅผ 1์ฐจ์ ๋ฐฐ์ด cards์ ์ ์ฅํ๊ณ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๋ค.
- np๋ก ์ฐ์ฐ์ ๋ฒํธ๋ฅผ ๋ฐ๊ฟ๊ฐ๋ฉฐ ๋์ฌ ์ ์๋ ์ค๋ณต ์ ๊ฑฐ ๋ ์์ด๋ก ๊ณ์ฐํ๊ณ ์ต๋ ์ต์๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.
- ๋ฉ์๋ ํธ์ถ์ด dfs๋ณด๋ค ์ ์ผ๋ฏ๋ก ๋ ๋น ๋ฅด๋ค.
๐ธ end ๐ธ
- ๊ฐ๋จํ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ๋๋ฐ, dfs์ np๋ก ํ์ด๋ฅผ ํด๋ณผ ์ ์์ด์ ์ข์ ๊ณต๋ถ๊ฐ ๋์๋ค.
- ์์๋๋ก ์์ด, dfs, np ํ์ด ๊ฒฐ๊ณผ์ด๋ค.
728x90
'CodingTest > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ_17281 : โพ (0) | 2023.02.20 |
---|---|
BOJ_12865 : ํ๋ฒํ ๋ฐฐ๋ญ (0) | 2023.02.20 |
BOJ_19621 : ํ์์ค ๋ฐฐ์ 2 (0) | 2023.02.20 |
BOJ_1697 : ์จ๋ฐ๊ผญ์ง (0) | 2023.02.12 |
BOJ_1954 : ํํ์คํ (0) | 2023.02.10 |