Contest problem: 1423. Maximum Points You Can Obtain from Cards
Solution1
Today I joined my first LeetCode contest, pass only 1 easy case, which means I have a large room for improvement.
The problem I failed stated as followed:
Given a list of numbers (with M cards)and the amount of picking (K), we should return the maximum possible summary of the picked numbers. Each time we should only pick the rightmost or the leftmost.
Minus minimum summary method:
The solution to the problem is to find continuous M-K numbers in the list that has the minimum summary, so the maximum possible summary is the total summary of all numbers minus the minimum summary.
Maximum summary method:
This solution is equivalent to finding k (integer) continuous numbers from the leftmost and K-k continuous numbers from the rightmost that has the maximum summary.
So the algorithm is as following:
1. If K>0.5*M, use the Minus minimum summary method to reduce the amount of each summary. Else, use the Maximum summary method.
2. Store the summary of first picking list: pick the 1st to the Kth number
3. Try every other possible case in either of the methods, replace the stored summary with the new one when the calculated summary exceeds the stored summery.
However, this method exceeds the time requirement.
https://leetcode.com/contest/weekly-contest-186/problems/maximum-points-you-can-obtain-from-cards/