-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_67258.java
More file actions
79 lines (58 loc) ยท 1.64 KB
/
DH_67258.java
File metadata and controls
79 lines (58 loc) ยท 1.64 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* ๋ณด์ ์ผํ
* HashMap & ํฌํฌ์ธํฐ
* ๋นํธ ์ฐ์ฐ์ ํ์์ ใ
ใ
โ gems ํฌ๊ธฐ 100,000 ์ดํ์ด๊ธฐ ๋๋ฌธ์ ์๋จ
*/
import java.util.*;
public class DH_67258 {
static HashMap<String, Integer> gToIdx = new HashMap<String, Integer>();
static HashSet<Integer> set = new HashSet<Integer>();
static int[] solution(String[] gems) {
initHashMap(gems);
// ๋ณด์๋ค์ ์ข
๋ฅ
int gCnt = gToIdx.size();
return twoPointer(0, 0, gems, gCnt);
}
static int[] twoPointer(int s, int e, String[] gems, int type) {
int[] result = new int[2];
int[] cnt = new int[type];
int idx = gToIdx.get(gems[0]);
int length = Integer.MAX_VALUE;
cnt[idx] += 1; // ๊ฐ์ ๋๋ ค์ฃผ๊ณ
set.add(idx);
while(s <= e) {
// ๋ค ๊ณ ๋ฅธ ์ํ๋ผ๋ฉด
if(set.size() == type) {
if(e - s < length) {
length = e - s;
result[0] = s + 1;
result[1] = e + 1;
}
// ๋ง์ฝ ๋งจ ์ผ์ชฝ ๋ณด์ ๊ฐ์๊ฐ ํ ๊ฐ ๋ณด๋ค ๋ง์ผ๋ฉด
idx = gToIdx.get(gems[s]);
cnt[idx] -= 1;
// 0๊ฐ๋ผ๋ฉด set์์ ์์ ์ฃผ๊ธฐ
if(cnt[idx] == 0) set.remove(idx);
s += 1;
} else {
// ๋ค ๊ณ ๋ฅด์ง ์์ ์ํ๋ผ๋ฉด
e += 1;
if(e >= gems.length) break;
idx = gToIdx.get(gems[e]);
cnt[idx] += 1;
set.add(idx);
}
}
return result;
}
static void initHashMap(String[] gems) {
// ๋ณด์๋ค์ idx๋ฅผ ๋ง๋ค์ด์ค
for(String g: gems) {
if(!gToIdx.containsKey(g)) gToIdx.put(g, gToIdx.keySet().size());
}
}
public static void main(String[] args) throws Exception {
String[] gems = {"A","A","A"};
System.out.println(Arrays.toString(solution(gems)));
}
}