-
Notifications
You must be signed in to change notification settings - Fork 0
/
n04.js
35 lines (34 loc) · 820 Bytes
/
n04.js
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
function solution(k, room_number) {
let answer = [];
let link = new Map();
let tmp;
let tmp_list;
room_number.forEach(e => {
if (!link.has(e)) {
link.set(e, e+1);
answer.push(e);
} else {
tmp = e;
tmp_list = [];
while (link.has(tmp)) {
tmp_list.push(tmp);
tmp = link.get(tmp);
}
link.set(tmp, tmp + 1);
tmp_list.forEach(t => {
link.set(t, tmp + 1);
});
answer.push(tmp);
}
console.log(link);
});
return answer;
}
// console.log(solution( // result [1,3,4,2,5,6]
// 10,
// [1,3,4,1,3,1]
// ));
console.log(solution( // result [1,2,3,4,5,6,7,8,9]
100,
[1,1,1,1,5,5,5,5,3]
));