Electronics Shop

Electronics Shop

Monica wants to buy a keyboard and a USB drive from her favorite electronics store. The store has several models of each. Monica wants to spend as much as possible for the 2 items, given her budget.

Given the price lists for the store’s keyboards and USB drives, and Monica’s budget, find and print the amount of money Monica will spend. If she doesn’t have enough money to both a keyboard and a USB drive, print -1 instead. She will buy only the two required items.

For example, suppose she has b = 60 to spend. Three types of keyboards cost keyboards = [40,50,60]. Two USB drives cost drives = [5, 8, 12]. She could purchase a 40 keyboards + 12 USB drive = 52, or a 50 keyboards + 8 USB drive = 58. She chooses the latter. She can’t buy more than 2 items so she can’t spend exactly 60.

Function Description

Complete the getMoneySpent function in the editor below. It should return the maximum total price for the two items within Monica’s budget, or -1 if she cannot afford both items.

getMoneySpent has the following parameter(s):

  • keyboards: an array of integers representing keyboard prices
  • drives: an array of integers representing drive prices
  • b: the units of currency in Monica’s budget

Input Format

The first line contains three space-separated integers b, n, and m, her budget, the number of keyboard models and the number of USB drive models.

The second line contains n space-separated integers keyboard[i], the prices of each keyboard model.

The third line contains m space-separated integers drives, the prices of the USB drives.

Constraints

  • 1 <= n,m <= 100
  • 1 <= b <= 105
  • The price of each item is in the inclusive range [1, 105]

Output Format

Print a single integer denoting the amount of money Monica will spend. If she doesn’t have enough money to buy one keyboard and one USB drive, print -1 instead.

Sample Input 0

1
2
3
10 2 3
3 1
5 2 8

Sample Output 0

1
9

Explanation 0

She can buy the 2nd keyboard and the 3rd USB drive for a total cost of 8 + 1 = 9.

Sample Input 1

1
2
3
5 1 1
4
5

Sample Output 1

1
-1

Explanation 1

There is no way to buy one keyboard and one USB drive because 4 + 5 > 5, so we print -1.



Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
* Complete the getMoneySpent function below.
*/
function getMoneySpent(keyboards, drives, b) {
/*
* Write your code here.
*/
let max = 0;

(keyboards || []).reduce((target, keyboard) => {
drives.reduce((target, drive) => {
(keyboard + drive > max && keyboard + drive <= b) && (max = keyboard + drive);

return target;
}, [])
return target;
}, []);

return !max ? -1 : max;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×