Coding problem
The best total with no two neighbours
The task
A radio station sells ad slots in a row of time slots with different prices, and a sponsor never wants two of its ads in neighbouring slots. The station wants the most money it can make from that sponsor.
Complete MaxNonAdjacentSum(int[] values). Pick some of the values so that no two picked values are next to each other, and return the largest possible total, as a long. Picking nothing is allowed, so the answer is never below 0; values may be negative. The array is never null.
Signature
public static long MaxNonAdjacentSum(int[] values)
Examples
[4, 8, 3, 1]→9. 8 + 1; the neighbours of 8 cannot be picked with it.[3, 1, 1, 3]→6. Both ends, which are not neighbours.
Limits: Up to 5,000 values; a value can be any int.
The tests check: an empty array, one value, negative values, both ends picked, a gap of two, int.MaxValue twice, and 5,000 values in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// The largest total of values picked so that no two picked values are next to each other.
public static long MaxNonAdjacentSum(int[] values)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.