In this post, we are going to discuss the leetcode problem no. 1423— Maximum Points You Can Obtain from Cards, which is recently asked in Google interviews.
Problem Analysis
This question is to find the maximum sum we can get by picking k
numbers either from the head or the tail.
My Thinking Process
For this type of question, usually the first thing I do is, try to find out all the possibilities of numbers we can pick. So the possibilities are below:
- Pick all the first
k
numbers from the head - Pick all the first
k-1
numbers from the head, and1
number from the tail - Pick all the first
k-2
numbers from the head, and2
numbers from the tail - …
- Pick
0
numbers from the head, andk
numbers from the tail - Find the maximum sum from the above possibilities
Give the above analysis, the approach now becomes clearer:
- we can start with the first possibility, find the sum
current_sum
of the firstk
numbers from the head - Remove the
k^th
number from the head (we can easily do this by using a stack), and add the last number from the tail, and update thecurrent_sum