-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCT_4.java
More file actions
45 lines (40 loc) · 1.53 KB
/
CT_4.java
File metadata and controls
45 lines (40 loc) · 1.53 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @introduce
*
* 겹치지 않는 길이 2의 구간 중 합이 같은 구간의 최대 개수를 찾는 것
*
* 접근방식: 주어진 배열의 각 인덱스 쌍을 검사하여, 구간의 합이 같은 경우
* 겹치지 않는 구간으로 간주하고, 해당 구간의 최대 개수를 찾으면 됨
*/
class Solution {
public int solution(int[] A) {
// 각 구간합과 시작 인덱스를 저장할 맵 생성
Map<Integer, List<Integer>> sumToIndices = new HashMap<>();
// 각 구간의 합과 시작 인덱스를 맵에 저장
for (int i = 0; i < A.length - 1; i++) {
int sum = A[i] + A[i + 1];
if (!sumToIndices.containsKey(sum)) {
sumToIndices.put(sum, new ArrayList<>());
}
sumToIndices.get(sum).add(i);
}
int maxSegments = 0;
// 각 구간합에 대해 최대 겹치지 않는 구간 수 계산
for (List<Integer> indices : sumToIndices.values()) {
int count = 0;
int lastIndex = -2; // 초기값을 -2로 설정하여 첫 구간 선택을 유효하게 만듦
for (int index : indices) {
if (index > lastIndex + 1) { // 구간이 겹치지 않으면
count++;
lastIndex = index;
}
}
maxSegments = Math.max(maxSegments, count);
}
return maxSegments;
}
}