This repository was archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCollegeApps-Solution.cpp
More file actions
65 lines (58 loc) · 2.6 KB
/
Copy pathCollegeApps-Solution.cpp
File metadata and controls
65 lines (58 loc) · 2.6 KB
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
// Input and output was handled by Hacker Rank
// Testees had to only complete a single function that was passed in: vector<int> collegeSeatsArray, vector<int> studentScoresArray,
// vector<vector<int>> studentCollegePreferencesArray
// The indices of the vector<int> collegeSeatsArray indicate the college number - used to reference the college in the
// studentCollegePreferencesArray. The indices of the studentScoresArray correspond to their corresponding preferences vector in the
// studentCollegePreferencesArray. The results had to be returned in a vector<int> where the first element the number of seats left,
// and the second element indicated the number of students who didn't go to college
// Each student only gets allocated to one college.
// Helper functions
int sumElements(vector<int> vec)
{
int sum = 0;
for (int i = 0; i < vec.size(); i++)
sum += vec[i];
return sum;
}
vector<int> uniqueSort(vector<int> vec)
{
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
// The method used in this function ensures that in cases where students have the same scores, the student whose score
// was mentioed earlier in the array will get priority
vector<int> allocate(vector<int> collegeSeatsArray, vector<int> studentScoresArray, vector<vector<int>> studentCollegePreferencesArray)
{
int seatsLeft = sumElements(collegeSeatsArray);
int studentsUnallocated = studentScoresArray.size()'
unordered_map<string, vector<vector<int>>> scoresAndPreferences;
for (int i = 0; i < studentScoresArray.size(); i++)
{
if (scoresAndPreferences.find(studentScoresArray[i]) != scoresAndPreferences.end())
scoresAndPreferences[studentScoresArray[i]] = vector<vector<int>> preferences {studentCollegePreferencesArray[i]};
else
{
vector<vector<int>> preferences = scoresAndPreferences[studentScoresArray[i]];
preferences.push_back(studentCollegePreferencesArray[i]);
scoresAndPreferences[studentScoresArray[i]] = preferences;
}
}
studentScoresArray = uniqueSort(studentScoresArray);
for (int i = 0; i < studentScoresArray.size(); i++)
{
vector<vector<int>> currentStudentPreferences = scoresAndPreferences[studentScoresArray[i]];
for (int j = 0; j < currentStudentPreferences.size(); j++)
for (int k = 0; k < currentStudentPreferences[j].size(); k++)
{
if (currentStudentPreferences[j][k] > 0)
{
currentStudentPreferences[j][k]--;
studentsUnallocated--;
seatsLeft--;
break;
}
}
}
return vector<int> results {seatsLeft, studentsUnallocated};
}