Problem · Greedy
Maximize Memory Points (India Intern :)
Learn this problemProblem statement
The course has n chapters, each chapter has memory[i] memory points,
which a student gains or loses while reading that chapter.
The course has a requirement: in order to study the ith chapter, the student must
revisit all the previous chapters. A student gains memory[0] + memory[1] + ... + memory[i]
memory points for reading the ith chapter. The total memory points is the sum of memory
points gained while reading each chapter.
Students can read the chapters in any order, and want to maximize their total memory points. Find the maximum total memory points a student can score ensuring that all the chapters are read.
Function
maximizeMemoryPoints(memory: int[]) → intExamples
Example 1
memory = [3, 4, 5]return = 26🍒🍒
Example 2
memory = [1, 2, 3]return = 14The cumulative values are [1, 1 + 2, 1 + 2 + 3], which gives 1 + 3 + 6 = 10.
The best arrangement is [3, 2, 1]:
Step 1: 3 (cumulative sum = 3)
Step 2: 3 + 2 = 5 (cumulative sum = 8)
Step 3: 3 + 2 + 1 = 6 (cumulative sum = 14)
Total sum: 3 + 5 + 6 = 14 :)
POSSIBLE SOLUTION HINT (Just a probability; Accuracy cant be guaranteed 🥰 HHHHHappiiiiii 2025 new year! wish you happi coding!!)-
Sorted the input array in descending order, then calculated the cumulative sums :)
Constraints
🍓