-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawkwardparty.js
56 lines (43 loc) · 1.21 KB
/
awkwardparty.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const n = parseInt(readLine());
let g = readLine().split(' ');
let seats = {};
const calcDist = (o) => {
return o.end - o.start;
}
for (let i = 0; i < n; i++) {
const seat = parseInt(g[i]);
if (seats.hasOwnProperty(seat)) {
if (seats[seat].end !== -1) {
if (calcDist(seats[seat]) > i - seats[seat].end) {
seats[seat] = { start: seats[seat].end, end: i }
}
} else seats[seat].end = i;
} else seats[seat] = { start: i, end: -1 }
}
let akw = 100001;
for (let k in seats) {
if (seats[k].end !== -1) {
const c = calcDist(seats[k]);
if (c < akw) {
akw = c;
}
}
}
console.log(akw === 100001 ? n : akw);
}