Skip to content

Commit

Permalink
이것이 취업을 위한 코딩테스트다 Ch08. 다이나믹 프로그래밍 - 개미 전사
Browse files Browse the repository at this point in the history
  • Loading branch information
allrightDJ0108 committed Jul 20, 2023
1 parent bbf6735 commit 4cf375d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
Binary file added CodingTestStudy1.0/bin/ThisIsCT/ch08_03.class
Binary file not shown.
2 changes: 2 additions & 0 deletions CodingTestStudy1.0/bin/ThisIsCT/example/ch08_03.txt
@@ -0,0 +1,2 @@
4
1 3 1 5
39 changes: 39 additions & 0 deletions CodingTestStudy1.0/src/ThisIsCT/ch08_03.java
@@ -0,0 +1,39 @@
package ThisIsCT;

import java.io.*;
import java.util.*;

public class ch08_03 {
// Ch.08 다이나믹 프로그래밍 Dynamic Programming DP
// 개미 전사
// 유튜브 풀이 영상 : https://www.youtube.com/watch?v=5Lu34WIx2Us

static int[] d = new int[100 + 1];

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];

StringTokenizer str = new StringTokenizer(br.readLine(), " ");
for (int i=0; i<N; i++) {
arr[i] = Integer.parseInt(str.nextToken());
}

/*
* (바로 앞까지의 최적의 해)와
* (두칸 앞까지의 최적의 해 + 현재 위치의 해)를 비교하여 더 높은 값을 d[]에 저장함
*/


//0과 1번째는 항상 아래와 같고, 반복문은 2부터(세번째) 돌려줌
d[0] = arr[0];
d[1] = Math.max(arr[1], arr[0]);
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 changes: 2 additions & 0 deletions CodingTestStudy1.0/src/ThisIsCT/example/ch08_03.txt
@@ -0,0 +1,2 @@
4
1 3 1 5

0 comments on commit 4cf375d

Please sign in to comment.