-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_67258.java
More file actions
34 lines (33 loc) ยท 1006 Bytes
/
JW_67258.java
File metadata and controls
34 lines (33 loc) ยท 1006 Bytes
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
import java.util.HashMap;
import java.util.HashSet;
class JW_67258 {
public int[] solution(String[] gems) {
int[] rs = { 0, 100_001 };
int len = gems.length, cnt = 0;
int l = 0, r = 0;
{
HashSet<String> hs = new HashSet<>();
for (String gem : gems)
hs.add(gem);
cnt = hs.size(); // ์ด ๋ณด์์ ์
}
HashMap<String, Integer> hm = new HashMap<>();
// ํฌ ํฌ์ธํฐ
while (r < len) {
String gem = gems[r++];
hm.put(gem, hm.getOrDefault(gem, 0) + 1);
while (hm.size() == cnt) {
// ์ธ๋ฑ์ค ๊ฐฑ์
if (rs[1] - rs[0] > r - l - 1) {
rs[0] = l + 1;
rs[1] = r;
}
gem = gems[l++];
hm.put(gem, hm.get(gem) - 1);
if (hm.get(gem) == 0)
hm.remove(gem);
}
}
return rs;
}
}