-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject algos4.js
More file actions
120 lines (106 loc) · 2.93 KB
/
object algos4.js
File metadata and controls
120 lines (106 loc) · 2.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Given an array of ailments (illnesses), and an array of medication objects that have a nested array of treatedSymptoms
return the medication name(s) that treats the most given symptoms
*/
const medications = [
{
name: "Sulforaphane",
treatableSymptoms: [
"dementia",
"alzheimer's",
"cancer",
"inflammation",
"neuropathy",
],
},
{
name: "Longvida Curcumin",
treatableSymptoms: [
"pain",
"inflammation",
"depression",
"arthritis",
"anxiety",
],
},
{
name: "Hericium erinaceus",
treatableSymptoms: ["anxiety", "cognitive decline", "depression"],
},
{
name: "Nicotinamide mononucleotide",
treatableSymptoms: [
"ageing",
"low NAD",
"obesity",
"mitochondrial myopathy",
"diabetes",
],
},
{
name: "PainAssassinator",
treatableSymptoms: [
"pain",
"inflammation",
"cramps",
"headache",
"toothache",
"back pain",
"fever",
],
},
];
/*
Input: ailments1, medications
Output: ["PainAssassinator", "Longvida Curcumin"]
*/
const ailments1 = ["pain"];
/*
Input: ailments2, medications
Output: ["Longvida Curcumin"]
*/
const ailments2 = ["pain", "inflammation", "depression"];
/*
Input: ailments3, medications
Output: []
*/
const ailments3 = ["existential dread"];
function getMeds(ailments, meds) {
// set a max
let maxSymptomMatchCount = 0;
// set a map
const ailmentsMap = {};
let matchedMeds = [];
// create map of ailments to avoid relooping
for(const ailment of ailments) {
ailmentsMap[ailment] = true;
}
// loop your meds..
for (const med of meds) {
let symptomsMatchCount = 0;
// loop the symptoms of each med...
for (const symptom of med.treatableSymptoms) {
// check if symptom is in our ailmentsMap
if(ailmentsMap.hasOwnProperty(symptom)) { // 0(1)
symptomsMatchCount++;
}
}
// so we matched symptoms from our ailment to this med...
if (symptomsMatchCount > 0) {
// if they're equal to our max, add the med, it's good
if (symptomsMatchCount === maxSymptomMatchCount){
matchedMeds.push(med.name);
// if they're more than our max...
}else if (symptomsMatchCount > maxSymptomMatchCount) {
// update the max, toss the entire old array of matched meds
// start a new array with this better med as the first element
maxSymptomMatchCount = symptomsMatchCount;
matchedMeds = [med.name];
}
}
}
return matchedMeds;
}
// runtime O(n * m)
// where n is the ailments
// and m is the number of medications