commit 30918954bf94cbc2049bf2c6aeb8f6470098a380
parent d269aa5065ed6b14a7eae2415c463f34eda0831a
Author: Jack Sangdahl <jack@pngu.org>
Date: Wed, 24 Dec 2025 21:03:52 -0700
3075. maximise happiness of selected children
Diffstat:
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/maximise_happiness_children.c b/maximise_happiness_children.c
@@ -15,11 +15,11 @@
int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
long long maximumHappinessSum(int *happiness, int happinessSize, int k) {
- long long ans = 0, sub = 0;
qsort(happiness, happinessSize, sizeof(int), cmp);
- for (int i = 0; i < happinessSize && k; i++, k--) {
- ans += happiness[i] - sub < 0 ? 0 : happiness[i] - sub;
- sub++;
+ long long ans = 0, i = 0;
+ while (k--) {
+ happiness[i] = fmax(happiness[i] - i, 0);
+ ans += happiness[i++];
}
return ans;
}
diff --git a/maximise_happiness_children.py b/maximise_happiness_children.py
@@ -21,7 +21,7 @@ class Solution(object):
"""
happiness.sort(reverse=True)
i, ans = 0, 0
- while k:
+ while k > 0:
happiness[i] = max(happiness[i] - i, 0)
ans += happiness[i]
i += 1