-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.cs
70 lines (65 loc) · 3 KB
/
Solution.cs
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
63
64
65
66
67
68
69
70
/*
Problem: https://www.hackerrank.com/challenges/electronics-shop
Language: C#
Thoughts:
1. Let Monica has total money MTotal.
2. Let maximum money spendable on electronics be MaxMoneySpendable. Initialize it to -1.
3. Consider ArrayKeyboardPrices and ArrayDrivePrices.
4. Sort ArrayKeyboardPrices in descending order.
5. Sort ArrayDrivePrices in ascending order.
6. Start iterating the elements in the sorted ArrayKeyboardPrices. Let the keyboard price being iterated is kbCurrent.
7. For kbCurrent start iterating the elements of sorted ArrayDrivePrices.
- let the drive price being iterated is driveCurrent.
- if sum of driveCurrent and kbCurrent is greater than MTotal then stop iterating sorted ArrayDrivePrices
- if sum of driveCurrent and kbCurrent is less than or equal to MTotal
then set MaxMoneySpendable to sum of driveCurrent and kbCurrent
and move to next element in sorted ArrayDrivePrices
8. Repeat step 7 for all elements of sorted ArrayKeyboardPrices
9. Print MaxMoneySpendable.
Time Complexity:
Worst case : O(n^2)
Best case : O(nlog n)
Space Complexity: O(1)
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int getMoneySpent(int[] keyboards, int[] drives, int totalMoneyAvailable)
{
var sortedKeyboardPrices = from keyboard in keyboards
orderby keyboard descending
select keyboard;
var sortedDrivePrices = from drive in drives
orderby drive ascending
select drive;
//default if not able to purchase anything
var maxMoneySpendable = -1;
foreach (var keyboard in sortedKeyboardPrices)
{
foreach (var drive in sortedDrivePrices)
{
if (keyboard + drive <= totalMoneyAvailable)
{
if (keyboard + drive > maxMoneySpendable)
maxMoneySpendable = keyboard + drive;
}
else
break;
}
}
return maxMoneySpendable;
}
static void Main(String[] args) {
string[] tokens_s = Console.ReadLine().Split(' ');
int s = Convert.ToInt32(tokens_s[0]);
string[] keyboards_temp = Console.ReadLine().Split(' ');
int[] keyboards = Array.ConvertAll(keyboards_temp,Int32.Parse);
string[] drives_temp = Console.ReadLine().Split(' ');
int[] drives = Array.ConvertAll(drives_temp,Int32.Parse);
// The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
int moneySpent = getMoneySpent(keyboards, drives, s);
Console.WriteLine(moneySpent);
}
}