-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding_practise_time.js
More file actions
71 lines (63 loc) · 1.91 KB
/
Copy pathcoding_practise_time.js
File metadata and controls
71 lines (63 loc) · 1.91 KB
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
57
58
59
60
61
62
63
64
65
66
67
// Time Complexity => O(nlogn)
// Space Complexity => O(1)
function runProgram(input){
let input_arr = input.trim().split("\n")
let [_, days] = input_arr[0].trim().split(" ").map(Number)
let array = input_arr[1].trim().split(" ").map(Number)
let total_time = array.reduce((acc, i) => acc + i)
let low = 0
let high = total_time
let time;
while(low <= high){
let mid = Math.floor(low + ((high - low) / 2))
if(possibility_check(mid, days, array)){
time = mid
high = mid - 1
}
else{
low = mid + 1
}
}
console.log(time)
function possibility_check(time, days, array){
let count = 0
for(let i = 0; i < array.length; i++){
let time_taken = 0
let flag = false
while(time_taken + array[i] <= time){
flag = true
time_taken += array[i]
// console.log(time_taken + " " + time)
i = i + 1
}
if(flag){
i = i - 1
}
// console.log(i)
count++
}
if(count <= days){
return true
}
else{
return false
}
}
}
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$/,"")
runProgram(read);
});
process.on("SIGINT", function () {
read = read.replace(/\n$/,"")
runProgram(read);
process.exit(0);
});
runProgram(`52 17
9542 10181 11764 26836 11414 11803 1374 26005 26268 17266 32353 17841 533 26079 16336 29061 28539 1864 27142 26058 24371 25289 9922 21527 28680 16727 27446 11431 8664 29091 8282 30045 24717 30529 9606 4613 15183 13578 24896 25053 7161 5823 21587 22100 4809 32239 25377 26385 2651 30234 18745 1847`)