Maximum Average Patient Cohort Score
At Innovaccer, we frequently analyze patient data to identify cohorts with specific characteristics, often aiming to optimize care pathways or identify high-performing groups. One common challenge is to find a group of patients (a 'cohort') that not only meets a minimum size requirement but also exhibits a diverse set of medical conditions, ensuring the cohort is representative or medically interesting. Simultaneously, we want to find the cohort with the best health outcomes, represented by an average score.
This problem asks you to identify a contiguous group of patients in a given dataset that satisfies both a minimum size and a minimum diversity of diagnoses, and among all such valid groups, determine the one with the highest average patient score.
Implement the function maxAverageScore that takes four parameters:
patientScores: An array of numbers representing the health outcome scores for a sequence of patients.diagnosisIds: An array of numbers, where diagnosisIds[i] is the unique ID for the primary diagnosis of patientScores[i].minPatients: An integer specifying the minimum number of patients required in a valid cohort.minDistinctDiagnoses: An integer specifying the minimum number of unique diagnosis IDs required within a valid cohort.The function should return the maximum average score found among all contiguous patient cohorts that satisfy both minPatients and minDistinctDiagnoses criteria. If no such cohort exists, return -1.
Input:
patientScores: number[] - An array of non-negative integers representing patient health scores. patientScores[i] is the score for the i-th patient.diagnosisIds: number[] - An array of positive integers representing diagnosis IDs. diagnosisIds[i] is the diagnosis ID for the i-th patient, corresponding to patientScores[i].minPatients: number - A positive integer representing the minimum required length of a patient cohort.minDistinctDiagnoses: number - A positive integer representing the minimum required count of distinct diagnosis IDs within a patient cohort.Output:
number - The maximum average score (a floating-point number) among all valid cohorts. Return -1 if no valid cohort is found.patientScores and diagnosisIds will always be equal.sum_of_scores / number_of_patients_in_cohort.patientScores are non-negative. All diagnosisIds are positive integers.Examples
Example 1
Input:
patientScores = [10, 20, 30, 40, 50], diagnosisIds = [1, 2, 1, 3, 2], minPatients = 3, minDistinctDiagnoses = 2
Output:
40
Note:
Let's analyze cohorts:
Example 2
Input:
patientScores = [5, 10, 15, 20], diagnosisIds = [100, 100, 200, 100], minPatients = 2, minDistinctDiagnoses = 2
Output:
17.5
Note:
Let's analyze cohorts:
Example 3
Input:
patientScores = [1, 2, 3], diagnosisIds = [10, 10, 10], minPatients = 1, minDistinctDiagnoses = 2
Output:
-1
Note:
No contiguous cohort in this dataset can have 2 or more distinct diagnosis IDs, as all patients share the same diagnosis ID (10). Therefore, no valid cohort exists, and the function should return -1.
Constraints
1 <= patientScores.length <= 10^5patientScores.length == diagnosisIds.length0 <= patientScores[i] <= 10001 <= diagnosisIds[i] <= 10^91 <= minPatients <= patientScores.length1 <= minDistinctDiagnoses <= patientScores.lengthminPatients or minDistinctDiagnoses values (assume valid positive integers).diagnosisIds if the number of distinct IDs is also very large (standard hash map performance is expected).minPatients or minDistinctDiagnoses are at their limits.Loading editor...