Skip to content

Commit ded863e

Browse files
committed
[level 3] Title: 연속 펄스 부분 수열의 합, Time: 20.27 ms, Memory: 123 MB -BaekjoonHub
1 parent b1e8b1e commit ded863e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [level 3] 연속 펄스 부분 수열의 합 - 161988
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/161988?language=java)
4+
5+
### 성능 요약
6+
7+
메모리: 123 MB, 시간: 20.27 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 05월 15일 15:01:17
20+
21+
### 문제 설명
22+
23+
<p>어떤 수열의 연속 부분 수열에 같은 길이의 펄스 수열을 각 원소끼리 곱하여 연속 펄스 부분 수열을 만들려 합니다. 펄스 수열이란 [1, -1, 1, -1 …] 또는 [-1, 1, -1, 1 …] 과 같이 1 또는 -1로 시작하면서 1과 -1이 번갈아 나오는 수열입니다.<br>
24+
예를 들어 수열 [2, 3, -6, 1, 3, -1, 2, 4]의 연속 부분 수열 [3, -6, 1]에 펄스 수열 [1, -1, 1]을 곱하면 연속 펄스 부분수열은 [3, 6, 1]이 됩니다. 또 다른 예시로 연속 부분 수열 [3, -1, 2, 4]에 펄스 수열 [-1, 1, -1, 1]을 곱하면 연속 펄스 부분수열은 [-3, -1, -2, 4]이 됩니다.<br>
25+
정수 수열 <code>sequence</code>가 매개변수로 주어질 때, 연속 펄스 부분 수열의 합 중 가장 큰 것을 return 하도록 solution 함수를 완성해주세요.</p>
26+
27+
<hr>
28+
29+
<h5>제한 사항</h5>
30+
31+
<ul>
32+
<li>1 ≤ <code>sequence</code>의 길이 ≤ 500,000</li>
33+
<li>-100,000 ≤ <code>sequence</code>의 원소 ≤ 100,000
34+
35+
<ul>
36+
<li><code>sequence</code>의 원소는 정수입니다.</li>
37+
</ul></li>
38+
</ul>
39+
40+
<hr>
41+
42+
<h5>입출력 예</h5>
43+
<table class="table">
44+
<thead><tr>
45+
<th>sequence</th>
46+
<th>result</th>
47+
</tr>
48+
</thead>
49+
<tbody><tr>
50+
<td>[2, 3, -6, 1, 3, -1, 2, 4]</td>
51+
<td>10</td>
52+
</tr>
53+
</tbody>
54+
</table>
55+
<hr>
56+
57+
<h5>입출력 예 설명</h5>
58+
59+
<p>주어진 수열의 연속 부분 수열 [3, -6, 1]에 펄스 수열 [1, -1, 1]을 곱하여 연속 펄스 부분 수열 [3, 6, 1]을 얻을 수 있고 그 합은 10으로서 가장 큽니다.</p>
60+
61+
62+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int n;
5+
long[] plus, minus;
6+
public long solution(int[] sequence) {
7+
n = sequence.length;
8+
plus = new long[n + 1];
9+
minus = new long[n + 1];
10+
11+
long answer = findMax(sequence);
12+
return answer;
13+
}
14+
15+
long findMax(int[] sequence){
16+
long answer = -50000000000L;
17+
int pulse, now;
18+
19+
pulse = 1;
20+
for(int i = 1; i < n + 1; i++){
21+
now = sequence[i - 1] * pulse;
22+
plus[i] = Math.max(plus[i - 1] + now, now);
23+
24+
now *= -1;
25+
minus[i] = Math.max(minus[i - 1] + now, now);
26+
27+
answer = Math.max(plus[i], Math.max(answer, minus[i]));
28+
pulse *= -1;
29+
}
30+
31+
return answer;
32+
}
33+
}

0 commit comments

Comments
 (0)