Maximize Memory Points
Updated
Problem statement
A course has n chapters. Chapter i has memory[i] memory points. A student gains these points while reading the chapter, or loses them if the value is negative.
The course has one rule: to study a chapter, the student must first revisit all the chapters read before it. So the k-th chapter you read gives the sum of the memory points of the first k chapters you read. The total memory points is the sum of the points from every chapter.
You can read the chapters in any order, but you must read all of them. Return the largest total memory points you can get.
Function
maximizeMemoryPoints(memory: int[]) → int
Complete the function maximizeMemoryPoints in the editor.
maximizeMemoryPoints has the following parameter:
int memory[n]: the memory points of each chapter
Returns
int: the largest total memory points
Examples
Example 1
memory = [3, 4, 5]return = 26Read the chapters in the order 5, 4, 3. Reading 5 gives 5 points. Reading 4 next gives 5 + 4 = 9. Reading 3 last gives 5 + 4 + 3 = 12. The total is 5 + 9 + 12 = 26. The first chapter you read is counted 3 times, the second twice and the last once, so the largest value should go first. The answer is 26.
Example 2
memory = [1, 2, 3]return = 14In the given order 1, 2, 3, the running sums are 1, 1 + 2 = 3 and 1 + 2 + 3 = 6, for a total of 10. The order 3, 2, 1 is better. Its running sums are 3, 3 + 2 = 5 and 3 + 2 + 1 = 6, for a total of 3 + 5 + 6 = 14. The first chapter you read is counted 3 times, the second twice and the last once, so the largest value should go first. The answer is 14.
Constraints
1 <= n <= 2000, wherenis the length ofmemory-1000 <= memory[i] <= 1000
More Amazon interview questions
- Add Course Batches All or NothingONSITE INTERVIEW · Seen Dec 2026
- Priority Ad Rotation with Show LimitsONSITE INTERVIEW · Seen Dec 2026
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- Count Connected ComponentsPHONE SCREEN · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge Accounts by Shared AttributePHONE SCREEN · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Minimum Grid InconvenienceOA · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
Similar questions at other companies
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
- K-Capable Model SelectionWalmart · OAWalmart interview questions
- Call Auction Clearing Price and Order FillsHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Hiking Club Fastest PaceInfosys · OAInfosys interview questions
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions