-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
이것이 취업을 위한 코딩테스트다 Ch08. 다이나믹 프로그래밍 - 개미 전사
- Loading branch information
1 parent
bbf6735
commit 4cf375d
Showing
4 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 | ||
1 3 1 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4 | ||
1 3 1 5 |