[programmers] 뉴스 클러스터링
업데이트:
programmers lv2. 뉴스 클러스터링
자카드 유사도를 측정하는 문제다.
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
function solution(str1, str2) {
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
if (str1 === str2) {
return 1 * 65536;
}
//init arr1, arr2
let arr1 = [];
for (let i=0; i<str1.length-1; i++) {
let s = "" + str1[i] + str1[i + 1];
if (/[A-Z]{2}/.exec(s)) {
arr1.push(s);
}
}
let arr2 = [];
for (let i=0; i<str2.length-1; i++) {
let s = str2[i] + str2[i + 1];
if (/^[A-Z]{2}$/.exec(s)) {
arr2.push(s);
}
}
arr1 = arr1.sort();
arr2 = arr2.sort();
let equal = 0;
let diff = 0;
while (arr1.length && arr2.length) {
if (arr1[0] === arr2[0]) {
arr1.shift();
arr2.shift();
equal += 1;
}
else if (arr1[0] < arr2[0]) {
arr1.shift();
diff += 1;
} else {
arr2.shift();
diff += 1;
}
}
diff += arr1.length + arr2.length;
return Math.floor(equal / (equal + diff) * 65536);
}
풀이는 쉬운데.. 어떻게 최적화 할 수 있을까?
댓글남기기