Implement different algorithm to find dominant object with several parameter in typescript

82 Views Asked by At

I have some student's profile with values on several subjects like physics, chemistry and math. I need find a list of dominant students depending on individual score on subjects. For example:

let students = [{name: "A", phy: 70, chem: 80, math: 90},
              {name: "B", phy: 75, chem: 85, math: 60},
              {name: "C", phy: 78, chem: 81, math: 92},
              {name: "D", phy: 75, chem: 85, math: 55}]; 

A student will be dominant over another if he meets following two conditions. 1. student_1 >= student_2 for all parameter 2. student_1 > student_2 for at least one parameter

I have tried using nested loop. May be it's the brute-force algorithm. I added another parameter named "passed" to track if it's dominant over other. Here's the code:


let students = [{ name: "A", phy: 70, chem: 80, math: 90, passed: true },
{ name: "B", phy: 75, chem: 85, math: 60, passed: true },
{ name: "C", phy: 78, chem: 81, math: 92, passed: true },
{ name: "D", phy: 75, chem: 85, math: 55, passed: true }];

let weak_student: any;

for (let student_1 of students) {

    if (student_1.passed == false ||
        students[students.length] === student_1) {
        continue;
    }

let compareList = students.filter(i => i.name != student_1.name && i.passed == true);

    for (let student_2 of compareList) {

        if ((student_1.phy >= student_2.phy &&
            student_1.chem >= student_2.chem &&
            student_1.math >= student_2.math)
            &&
            (student_1.phy > student_2.phy ||
                student_1.chem > student_2.chem ||
                student_1.math > student_2.math)
        ) {
            weak_student = students.find(i => i.name === student_2.name);
            weak_student.passed = false;

        } else if (student_1.phy < student_2.phy &&
            student_1.chem < student_2.chem &&
            student_1.math < student_2.math) {

            student_1.passed = false;
            break;
        }
    }
}
console.log(students);

I found expected result as student A & D 's flag "passed"== false. Now I need the same result by using different algorithms like Divide & Conquer, Nearest Neighbor, Branch & Bound etc or any other efficient ways. I need to compare between algorithm's of time & space complexity for large datasets.

1

There are 1 best solutions below

6
On

You could sort the array by taking the keys, get the delta of all values and normalize the value to [-1, 0, 1] by using Math.sign, sum the values and return this sum as result for sorting.

The most top group is the one with the greatest values.

let students = [{ name: "A", phy: 70, chem: 80, math: 90 }, { name: "B", phy: 75, chem: 85, math: 60 }, { name: "C", phy: 78, chem: 81, math: 92 }, { name: "D", phy: 75, chem: 85, math: 55 }];

students.sort((a, b) => ['phy', 'chem', 'math']
    .map(k => Math.sign(b[k] - a[k]))
    .reduce((a, b) => a + b)
);

console.log(students);
.as-console-wrapper { max-height: 100% !important; top: 0; }