forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.js
More file actions
58 lines (48 loc) · 1.53 KB
/
Solution.js
File metadata and controls
58 lines (48 loc) · 1.53 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
//Problem: https://www.hackerrank.com/challenges/hackerland-radio-transmitters
//JavaScript
/*
Initial thoughts:
First, sort the array to so duplicate houses dont cause
any errors when looking for where to place the transmitter.
Then, use greedy algorithm to always place the transmitter
at the house furthest to the right possible to cover
the range.
Time complexity: O(n log(n)) //Finding the furthest transmitter range
Space complexity: O(1) //No additional space was used
*/
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n_temp = readLine().split(' ');
var n = parseInt(n_temp[0]);
var k = parseInt(n_temp[1]);
var houses = readLine().split(' ').map(Number).sort(function(a, b) { return a - b; });
var house = houses[0], transmitter = houses[0], i = 0, towers = 0;
while( i < n) {
transmitter = houses[i];
house = houses[i];
towers++;
while(i < n && (houses[i] - house) <= k){
i++;
}
transmitter = houses[i-1];
while(i < n && houses[i] <= transmitter + k){
i++;
}
}
console.log(towers);
}