CS/알고리즘_문제풀이(자바)

연속합

Jedy_Kim 2021. 10. 20. 15:26
728x90

https://www.acmicpc.net/problem/1912

 

1912번: 연속합

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

www.acmicpc.net

 

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
import java.io.*;
 
class Main{ 
  public static void main(String[] args) throws Exception{
    // Please Enter Your Code Here
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    int N     = Integer.parseInt(br.readLine());
    int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    int[] dp  = new int[N];
    
    int max = arr[0];
    dp[0]   = arr[0];
    for(int i=1; i<N; ++i) {
      dp[i] = Math.max(dp[i - 1+ arr[i], arr[i]);  
      max = Math.max(max, dp[i]);
    }
    
    bw.write(String.valueOf(max));
    bw.flush();
    bw.close();
    br.close();
  } 
cs

 

반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

제곱수의 합  (0) 2021.10.21
연속합 2  (0) 2021.10.21
가장 긴 바이토닉 부분 수열  (0) 2021.10.20
가장 긴 감소하는 부분 수열  (0) 2021.10.20
가장 큰 증가 부분 수열  (0) 2021.10.19