Skip to main content
added 24 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
  • Encapsulate the solution in a function.
  • Minimize allocations
  • Simplify by using simple loops.
  • Use the same loops for totals summation and subject summation and low average.
  • The order of an average, sum/n, and a sum is the same, we don't need to calculate sum/n.
  • At the end, we already have the student marks sum, so simply subtract the low average mark for the returned total.
  • Encapsulate the solution in a function.
  • Simplify by using simple loops.
  • Use the same loops for totals summation and subject summation and low average.
  • The order of an average, sum/n, and a sum is the same, we don't need to calculate sum/n.
  • At the end, we already have the student marks sum, so simply subtract the low average mark for the returned total.
  • Encapsulate the solution in a function.
  • Minimize allocations
  • Simplify by using simple loops.
  • Use the same loops for totals summation and subject summation and low average.
  • The order of an average, sum/n, and a sum is the same, we don't need to calculate sum/n.
  • At the end, we already have the student marks sum, so simply subtract the low average mark for the returned total.
added 4 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
'use strict';

function totalMarks(N, M, students) {
    let totals = Array(N).fill(0);

    let lowIndex = 0, lowValue = Number.MAX_VALUE;
    for (let i = 0; i < M; i++) {
        let subject = 0; 

        for (let j = 0; j < N; j++) {
            let mark = students[j][i];
            subject += mark;
            totals[j] += mark; 
        } 

        if (lowValue > subject) {
            lowIndex = i, lowValue = subject;
        }
    }

    for (let i = 0; i < totals.length;N; i++) {
        totals[i]-= students[i][lowIndex];
    }

    return totals;
}
'use strict';

function totalMarks(N, M, students) {
    let totals = Array(N).fill(0);

    let lowIndex = 0, lowValue = Number.MAX_VALUE;
    for (let i = 0; i < M; i++) {
        let subject = 0;
        for (let j = 0; j < N; j++) {
            let mark = students[j][i];
            subject += mark;
            totals[j] += mark; 
        }
        if (lowValue > subject) {
            lowIndex = i, lowValue = subject;
        }
    }

    for (let i = 0; i < totals.length; i++) {
        totals[i]-= students[i][lowIndex];
    }

    return totals;
}
'use strict';

function totalMarks(N, M, students) {
    let totals = Array(N).fill(0);

    let lowIndex = 0, lowValue = Number.MAX_VALUE;
    for (let i = 0; i < M; i++) {
        let subject = 0; 

        for (let j = 0; j < N; j++) {
            let mark = students[j][i];
            subject += mark;
            totals[j] += mark; 
        } 

        if (lowValue > subject) {
            lowIndex = i, lowValue = subject;
        }
    }

    for (let i = 0; i < N; i++) {
        totals[i]-= students[i][lowIndex];
    }

    return totals;
}
deleted 3 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14

Using jsbench.meJSBench.me, your code is about 87.16%87% slower than my revised code.

Using jsbench.me, your code is about 87.16% slower than my revised code.

Using JSBench.me, your code is about 87% slower than my revised code.

Post Undeleted by peterSO
loop optimization
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading
Post Deleted by peterSO
added 1272 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading
added 524 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading
added 286 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading
added 21 characters in body
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading
Source Link
peterSO
  • 3.6k
  • 14
  • 14
Loading