/**
* @param {number[]} happiness
* @param {number} k
* @return {number}
*/
var maximumHappinessSum = function (happiness, k) {
let res = 0
happiness.sort((a, b) => b - a)
for (let i = 0; i < k; i++) {
res += Math.max(happiness[i] - i, 0)
}
return res
};
One tip from this question is:
Math.max(happiness[i] - i, 0)
Subtracting the value from it's index and making the results that are less than 0 to be 0 just solves the question.
Still feels like magic to me.
Top comments (0)