-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.js
62 lines (52 loc) · 1.63 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
//Problem: https://www.hackerrank.com/challenges/electronics-shop
//JavaScript
/*
Initial Thoughts:
We can sort and compare all pairs. If a pair is > max and <= s
then we set it as the new max. Then we eturn max after
checking all pairs.
Time Complexity: O(n * m) //Iterate over both arrays
Space Complexity: O(1) //No additional memory 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 getMoneySpent(keyboards, drives, s){
drives.sort((a, b) => a - b);
keyboards.sort((a, b) => a - b);
let max = -1;
for (let d of drives) {
for (let k of keyboards) {
if (d + k <= s) {
max = (d + k > max) ? d + k : max;
}
}
}
return max;
}
function main() {
var s_temp = readLine().split(' ');
var s = parseInt(s_temp[0]);
var n = parseInt(s_temp[1]);
var m = parseInt(s_temp[2]);
keyboards = readLine().split(' ');
keyboards = keyboards.map(Number);
drives = readLine().split(' ');
drives = drives.map(Number);
// The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
var moneySpent = getMoneySpent(keyboards, drives, s);
process.stdout.write(""+moneySpent+"\n");
}