AcWing 799. 最长连续不重复子序列 ( JavaScript )
原题链接
简单
作者:
gaobowen
,
2019-11-14 16:04:55
,
所有人可见
,
阅读 727
let lcnsN = arr => {
let l = 0;
let s = new Int32Array(100010);
for (let i = 0, j = 0; i < arr.length; i++) {
s[arr[i]]++;
// j < i, j++, 之后, i == j
// 此题目变型为:求“最长连续不重复 n次 的子序列”只需更改 s[arr[i]] > n-1
while (j < i && s[arr[i]] > 1) s[arr[j++]]--;
l = Math.max(l, i - j + 1);
}
return l;
}
var buf = '';
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputArgs = line => {
return line.split(' ').filter(s => s !== '').map(x => parseInt(x));
}
process.stdin.on('end', function () {
let n = 0;
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
let firstline = getInputArgs(line);
n = firstline[0];
}
else if (lineIdx === 1)
console.log(lcnsN(getInputArgs(line)));
});
});