-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.js
62 lines (53 loc) · 1.64 KB
/
Solution.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
57
58
59
60
61
62
//Problem: https://www.hackerrank.com/challenges/apple-and-orange
//JavaScript
/*
Initial Thoughts:
We can iterate through both sets of fruit and calculate the position
of a fruit by adding its distance from its tree to the position of
the tree. Count the fruit only falls on Sam's house.
i.e. s <= fruit's position <= t
Time complexity: O(n+m) //Iterate through both arrays
Space complexity: O(1)
*/
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 s_temp = readLine().split(' ');
var s = parseInt(s_temp[0]);
var t = parseInt(s_temp[1]);
var a_temp = readLine().split(' ');
var a = parseInt(a_temp[0]);
var b = parseInt(a_temp[1]);
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
apple = readLine().split(' ');
apple = apple.map(Number);
orange = readLine().split(' ');
orange = orange.map(Number);
console.log(fruitsInHouse(apple, a, [s, t]));
console.log(fruitsInHouse(orange, b, [s, t]));
}
function fruitsInHouse(fruits, start, house) {
let totalFruit = 0;
for (let fruit of fruits) {
if (house[0] <= start + fruit && start + fruit <= house[1]) {
totalFruit++;
}
}
return totalFruit;
}