Notice
Recent Posts
Recent Comments
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

I'm pine thank you and you?

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„๋‘‘์งˆ (Java) ๋ณธ๋ฌธ

Algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„๋‘‘์งˆ (Java)

SollyJ 2023. 3. 28. 20:00

๐Ÿ’œ ๋ฌธ์ œ


๐Ÿค” ๋ฌธ์ œ ๋ถ„์„

์ง‘์ด ์›ํ˜•์œผ๋กœ ๋ฐฐ์น˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ ๋ฒˆ์งธ ์ง‘ ์œ„์น˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์—†๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ money ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด ์ฒซ ๋ฒˆ์งธ ์ง‘์ด๋ผ๊ณ  ๋‘๊ณ  ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ์œผ๋กœ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

a. ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ฐ๋Š” ๊ฒฝ์šฐ

b. ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ธ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์ด ๋‘ ๊ฒฝ์šฐ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ Dynamic Programming์„ ํ•ด๋ณด์ž.


๐Ÿ“ ํ’€์ด

1. ํ…Œ์ด๋ธ” ์ •์˜

n๋ฒˆ์งธ ์ง‘์„ ํ„ธ์—ˆ์„ ๋•Œ ์ด ํ›”์นœ ๋ˆ์˜ ์ตœ๋Œ“๊ฐ’์ด ๋“ค์–ด๊ฐˆ ๋ฐฐ์—ด ์ •์˜

int[] dp_first = new int[len];    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ฐ๋Š” ๊ฒฝ์šฐ
int[] dp_second = new int[len];    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ธ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

2. ์ดˆ๊ธฐ๊ฐ’ ์„ธํŒ…

๊ฐ ์ง‘์— ์žˆ๋Š” ๋ˆ์„ ๋„ฃ์–ด์ค€๋‹ค.

for (int i = 0; i < len; i++) {
	dp_first[i] = money[i];
	dp_second[i] = money[i];
}

a. ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ฐ๋Š” ๊ฒฝ์šฐ

์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ฐ๋‹ˆ๊นŒ ๋‘๋ฒˆ์งธ ์ง‘์€ ๋ชป ํ„ด๋‹ค.

์ง‘์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ์†Œ 3๊ฐœ ์ด๋ฏ€๋กœ, dp_first[2]์˜ ์ดˆ๊ธฐ๊ฐ’๋„ ๋„ฃ์–ด์ค€๋‹ค.

dp_first[1] = -1;
dp_first[2] += dp_first[0];

b. ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ธ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์ฒซ๋ฒˆ์งธ ์ง‘ ์•ˆ ํ„ธ๊ณ , ๋‘๋ฒˆ์งธ์ง‘๋ถ€ํ„ฐ ํ„ธ๊ธฐ

dp_second[0] = -1;

3. ์ ํ™”์‹ ์„ ์–ธ

์ธ์ ‘ํ•˜์ง€ ์•Š์€ ๋‘ ์ง‘(i - 2, i - 3๋ฒˆ์งธ ์ง‘)์„ ๋น„๊ตํ•ด ๋” ํฐ ๊ฐ’์„ ๋ˆ„์ ํ•ด ๋”ํ•œ๋‹ค.

for (int i = 3; i < len; i++) {
	dp_first[i] += Math.max(dp_first[i - 3], dp_first[i - 2]);
	dp_second[i] += Math.max(dp_second[i - 3], dp_second[i - 2]);
}

4. ๊ฒฐ๊ณผ๊ฐ’ ๋„์ถœ

ํ›”์นœ๋ˆ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ,

๋งจ ๋งˆ์ง€๋ง‰ ๋‘์ง‘์„ ๋น„๊ตํ•ด dp_first ๋ฐฐ์—ด๊ณผ dp_second ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๊ณ 

๊ทธ ๋‘˜์ค‘์—์„œ ์ตœ๋Œ“๊ฐ’์ด answer!

์—ฌ๊ธฐ์„œ, dp_first๋Š” ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ธ์—ˆ์œผ๋‹ˆ๊นŒ ๋งจ ๋งˆ์ง€๋ง‰ ์ง‘์€ ๋ชป ํ„ด๋‹ค๋Š” ์ ์„ ๊ณ ๋ คํ•˜์ž.

int first_max = Math.max(dp_first[len - 2], dp_first[len - 3]);    
int second_max = Math.max(dp_second[len - 1], dp_second[len - 2]);    

answer = Math.max(first_max, second_max);

๐Ÿ‘€ ํšŒ๊ณ 

Dynamic Programming ์ ‘๊ทผ ๋ฐฉ๋ฒ•

1. ํ…Œ์ด๋ธ” ์ •์˜

2. ์ดˆ๊ธฐ๊ฐ’ ์„ธํŒ…

3. ์ ํ™”์‹ ์„ ์–ธ

์ด ์ˆœ์„œ ๊ธฐ์–ต!


๋”๋ณด๊ธฐ
public class ๋„๋‘‘์งˆ {
	public static void main(String[] args) {
		System.out.println(solution(new int[] {1, 2, 3, 1}));
	}

	private static int solution(int[] money) {
		int answer = 0;
		int len = money.length;

		// 1. n๋ฒˆ์งธ ์ง‘์„ ํ„ธ์—ˆ์„๋•Œ ํ›”์นœ ๋ˆ์˜ ์ตœ๋Œ“๊ฐ’์ด ๋“ค์–ด๊ฐˆ ๋ฐฐ์—ด ์„ ์–ธ
		int[] dp_first = new int[len];    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ฐ๋Š” ๊ฒฝ์šฐ
		int[] dp_second = new int[len];    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ์•ˆ ํ„ฐ๋Š” ๊ฒฝ์šฐ

		// 2. ๋ฐฐ์—ด ์ดˆ๊ธฐ๊ฐ’
		for (int i = 0; i < len; i++) {
			dp_first[i] = money[i];
			dp_second[i] = money[i];
		}

		dp_first[1] = -1;    // ๋‘๋ฒˆ์งธ์ง‘ ์•ˆํ„ธ๊ธฐ
		dp_second[0] = -1;    // ์ฒซ๋ฒˆ์งธ์ง‘ ์•ˆํ„ธ๊ธฐ
		dp_first[2] += dp_first[0];    // ์ง‘์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ์†Œ 3๊ฐœ์ด๋ฏ€๋กœ, dp_first[2]์˜ ์ดˆ๊ธฐ๊ฐ’๋„ ๋„ฃ์–ด์ค€๋‹ค.

		// 3. ์ ํ™”์‹
		for (int i = 3; i < len; i++) {
			// ์ธ์ ‘ํ•˜์ง€ ์•Š์€ ์ง‘(i - 2, i - 3)๊ณผ ๋น„๊ตํ•ด ๋” ํฐ ๊ฐ’์„ ๋ˆ„์ ํ•ด ๋”ํ•ด์คŒ
			dp_first[i] += Math.max(dp_first[i - 3], dp_first[i - 2]);
			dp_second[i] += Math.max(dp_second[i - 3], dp_second[i - 2]);
		}

		// ๋งจ ๋งˆ์ง€๋ง‰ ๋‘์ง‘์„ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’ ์ฐพ๊ธฐ
		int first_max = Math.max(dp_first[len - 2], dp_first[len - 3]);    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ํ„ธ์—ˆ์œผ๋‹ˆ๊นŒ ๋งจ ๋งˆ์ง€๋ง‰ ์ง‘์€ ์–ด์ฐจํ”ผ ๋ชป ํ„ด๋‹ค.
		int second_max = Math.max(dp_second[len - 1], dp_second[len - 2]);    // ์ฒซ๋ฒˆ์งธ ์ง‘์„ ์•ˆ ํ„ธ์—ˆ์œผ๋‹ˆ๊นŒ ๋งจ ๋งˆ์ง€๋ง‰ ์ง‘ ํ„ธ ์ˆ˜ ์žˆ๋‹ค.

		answer = Math.max(first_max, second_max);

		return answer;
	}
}