-
Notifications
You must be signed in to change notification settings - Fork 0
/
abandonedCity.js
51 lines (48 loc) · 1.22 KB
/
abandonedCity.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
function solve(arr, k) {
let min = Infinity;
let flag = false;
let left = 0;
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
while (sum >= k) {
min = Math.min(min, i - left + 1);
flag = true;
sum -= arr[left];
left++;
}
}
if (flag) return min;
else return -1;
}
function runProgram(input) {
let newInput = input.split("\n");
let t = Number(newInput[0]);
for (let i = 1; i < t * 2; i += 2) {
let [, k] = newInput[i].trim().split(" ").map(Number);
let arr = newInput[i + 1].trim().split(" ").map(Number);
console.log(solve(arr, k));
}
}
if (process.env.USERNAME === "Ajit") {
runProgram(`1
5 7
1 2 3 4 5`);
} else {
process.stdin.resume();
process.stdin.setEncoding("ascii");
let read = "";
process.stdin.on("data", function (input) {
read += input;
});
process.stdin.on("end", function () {
read = read.replace(/\n$/, "");
read = read.replace(/\n$/, "");
runProgram(read);
});
process.on("SIGINT", function () {
read = read.replace(/\n$/, "");
runProgram(read);
process.exit(0);
});
}