Skip to content

Commit

Permalink
[Silver II] Title: 연속합, Time: 816 ms, Memory: 108460 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
shinjaewon99 committed Aug 24, 2023
1 parent a55842c commit 84d4fab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 백준/Silver/1912. 연속합/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# [Silver II] 연속합 - 1912

[문제 링크](https://www.acmicpc.net/problem/1912)

### 성능 요약

메모리: 108460 KB, 시간: 816 ms

### 분류

다이나믹 프로그래밍

### 문제 설명

<p>n개의 정수로 이루어진 임의의 수열이 주어진다. 우리는 이 중 연속된 몇 개의 수를 선택해서 구할 수 있는 합 중 가장 큰 합을 구하려고 한다. 단, 수는 한 개 이상 선택해야 한다.</p>

<p>예를 들어서 10, -4, 3, 1, 5, 6, -35, 12, 21, -1 이라는 수열이 주어졌다고 하자. 여기서 정답은 12+21인 33이 정답이 된다.</p>

### 입력

<p>첫째 줄에 정수 n(1 ≤ n ≤ 100,000)이 주어지고 둘째 줄에는 n개의 정수로 이루어진 수열이 주어진다. 수는 -1,000보다 크거나 같고, 1,000보다 작거나 같은 정수이다.</p>

### 출력

<p>첫째 줄에 답을 출력한다.</p>

31 changes: 31 additions & 0 deletions 백준/Silver/1912. 연속합/연속합.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

// 1. 정수의 갯수 입력
int size = in.nextInt();
int[] numbers = new int[size + 1];
int[] dp = new int[size + 1];

// 2. 정수의 갯수만큼 숫자 입력
for (int i = 1; i <= size; i++) {
numbers[i] = in.nextInt();
}

// 3. dp 배열 값 할당
dp[1] = numbers[1];
int max = numbers[1];

// 4. 연속된 최대값 찾기
// 입력값 : 2 1 -4 3 4 -4 6 5 -5 1
// dp 배열 : 2 3 -1 2 6 2 8 13 8 9
for (int i = 2; i <= size; i++) {
dp[i] = Math.max(dp[i - 1] + numbers[i], numbers[i]);
max = Math.max(max, dp[i]);
}

System.out.println(max);
}
}

0 comments on commit 84d4fab

Please sign in to comment.