Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

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 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.

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 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.

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 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.

deleted 97 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Challenge from Hacker RankHacker Rank -

More test cases are available on HackerRank .

I've attempted to solve this logically and with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or my GitHub repo my GitHub repo -

Challenge from Hacker Rank -

More test cases are available on HackerRank .

I've attempted to solve this logically and with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or my GitHub repo -

Challenge from Hacker Rank -

I've attempted to solve this logically and with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or my GitHub repo -

deleted 21 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Hacker Rank HackerRank: Electronics Shop

For example - with a budget of 10, two keyboards costing 3,1 & finally three drives available costing 5,2,8, the answer should be 9 as she is only able to purchase the keyboard for 3 &and a drive for 5.

I've attempted to solve this logically &and with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or My Githubmy GitHub repo -

using System;
using System.Collections.Generic;
using System.Linq;
namespace ElectronicsShop
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine(GetMoneySpent(new int[] { 3, 1 }, new int[] { 5, 2, 8 }, 10));
 Console.WriteLine(GetMoneySpent(new int[] { 5}, new int[] { 4 }, 5));
 Console.ReadLine();
 }
 static int GetMoneySpent(int[] keyboards, int[] drives, int budget)
 {
 if (budget == 0)
 return -1;
 // sort the two arrays so the highest values are at the front
 keyboards = SortArrayDescending(keyboards);
 drives = SortArrayDescending(drives);
 // delete any that are over our budget
 var affordableKeyboards = GetAffordableItems(keyboards, budget);
 var affordableDrives = GetAffordableItems(drives, budget);
 // make a list to contain the combined totals
 var combinedTotals = new List<int>();
 foreach (var keyboard in keyboards)
 {
 foreach (var drive in drives)
 {
 combinedTotals.Add(keyboard + drive);
 }
 }
 // sort the list & delete anything over budget
 combinedTotals.Sort();
 combinedTotals.Reverse();
 combinedTotals.RemoveAll(n => n > budget);
 return combinedTotals.Count == 0 ? -1 : combinedTotals[0];
 }
 static int[] SortArrayDescending(int[] array)
 {
 Array.Sort(array);
 Array.Reverse(array);
 return array;
 }
 static int[] GetAffordableItems(int[] array, int budget)
 {
 return array.Where(n => n < budget).ToArray();
 }
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ElectronicsShop
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine(GetMoneySpent(new int[] { 3, 1 }, new int[] { 5, 2, 8 }, 10));
 Console.WriteLine(GetMoneySpent(new int[] { 5}, new int[] { 4 }, 5));
 Console.ReadLine();
 }
 static int GetMoneySpent(int[] keyboards, int[] drives, int budget)
 {
 if (budget == 0)
 return -1;
 // sort the two arrays so the highest values are at the front
 keyboards = SortArrayDescending(keyboards);
 drives = SortArrayDescending(drives);
 // delete any that are over our budget
 var affordableKeyboards = GetAffordableItems(keyboards, budget);
 var affordableDrives = GetAffordableItems(drives, budget);
 // make a list to contain the combined totals
 var combinedTotals = new List<int>();
 foreach (var keyboard in keyboards)
 {
 foreach (var drive in drives)
 {
 combinedTotals.Add(keyboard + drive);
 }
 }
 // sort the list & delete anything over budget
 combinedTotals.Sort();
 combinedTotals.Reverse();
 combinedTotals.RemoveAll(n => n > budget);
 return combinedTotals.Count == 0 ? -1 : combinedTotals[0];
 }
 static int[] SortArrayDescending(int[] array)
 {
 Array.Sort(array);
 Array.Reverse(array);
 return array;
 }
 static int[] GetAffordableItems(int[] array, int budget)
 {
 return array.Where(n => n < budget).ToArray();
 }
 }
}

Hacker Rank : Electronics Shop

For example - with a budget of 10, two keyboards costing 3,1 & finally three drives available costing 5,2,8, the answer should be 9 as she is only able to purchase the keyboard for 3 & a drive for 5.

I've attempted to solve this logically & with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or My Github repo -

using System;
using System.Collections.Generic;
using System.Linq;
namespace ElectronicsShop
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine(GetMoneySpent(new int[] { 3, 1 }, new int[] { 5, 2, 8 }, 10));
 Console.WriteLine(GetMoneySpent(new int[] { 5}, new int[] { 4 }, 5));
 Console.ReadLine();
 }
 static int GetMoneySpent(int[] keyboards, int[] drives, int budget)
 {
 if (budget == 0)
 return -1;
 // sort the two arrays so the highest values are at the front
 keyboards = SortArrayDescending(keyboards);
 drives = SortArrayDescending(drives);
 // delete any that are over our budget
 var affordableKeyboards = GetAffordableItems(keyboards, budget);
 var affordableDrives = GetAffordableItems(drives, budget);
 // make a list to contain the combined totals
 var combinedTotals = new List<int>();
 foreach (var keyboard in keyboards)
 {
 foreach (var drive in drives)
 {
 combinedTotals.Add(keyboard + drive);
 }
 }
 // sort the list & delete anything over budget
 combinedTotals.Sort();
 combinedTotals.Reverse();
 combinedTotals.RemoveAll(n => n > budget);
 return combinedTotals.Count == 0 ? -1 : combinedTotals[0];
 }
 static int[] SortArrayDescending(int[] array)
 {
 Array.Sort(array);
 Array.Reverse(array);
 return array;
 }
 static int[] GetAffordableItems(int[] array, int budget)
 {
 return array.Where(n => n < budget).ToArray();
 }
 }
}

HackerRank: Electronics Shop

For example - with a budget of 10, two keyboards costing 3,1 & finally three drives available costing 5,2,8, the answer should be 9 as she is only able to purchase the keyboard for 3 and a drive for 5.

I've attempted to solve this logically and with good performance in mind. I'm not sure if I should be happy with my solution. I would appreciate any feedback.

My solution (which works) or my GitHub repo -

using System;
using System.Collections.Generic;
using System.Linq;
namespace ElectronicsShop
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine(GetMoneySpent(new int[] { 3, 1 }, new int[] { 5, 2, 8 }, 10));
 Console.WriteLine(GetMoneySpent(new int[] { 5}, new int[] { 4 }, 5));
 Console.ReadLine();
 }
 static int GetMoneySpent(int[] keyboards, int[] drives, int budget)
 {
 if (budget == 0)
 return -1;
 // sort the two arrays so the highest values are at the front
 keyboards = SortArrayDescending(keyboards);
 drives = SortArrayDescending(drives);
 // delete any that are over our budget
 var affordableKeyboards = GetAffordableItems(keyboards, budget);
 var affordableDrives = GetAffordableItems(drives, budget);
 // make a list to contain the combined totals
 var combinedTotals = new List<int>();
 foreach (var keyboard in keyboards)
 {
 foreach (var drive in drives)
 {
 combinedTotals.Add(keyboard + drive);
 }
 }
 // sort the list & delete anything over budget
 combinedTotals.Sort();
 combinedTotals.Reverse();
 combinedTotals.RemoveAll(n => n > budget);
 return combinedTotals.Count == 0 ? -1 : combinedTotals[0];
 }
 static int[] SortArrayDescending(int[] array)
 {
 Array.Sort(array);
 Array.Reverse(array);
 return array;
 }
 static int[] GetAffordableItems(int[] array, int budget)
 {
 return array.Where(n => n < budget).ToArray();
 }
 }
}
Rollback to Revision 2
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
typo in description
Source Link
Webbarr
  • 323
  • 2
  • 11
Loading
added improved(ish) code
Source Link
Webbarr
  • 323
  • 2
  • 11
Loading
Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1150148021151248384
tag
Link
dfhwze
  • 14.1k
  • 3
  • 40
  • 101
Loading
Source Link
Webbarr
  • 323
  • 2
  • 11
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /