Coding problem
The shortest stretch that reaches a target
The task
A van must leave with at least target kilograms on board, and it loads parcels from the belt as one block of neighbours.
Complete ShortestRunAtLeast(int[] values, int target). Every value is at least 1 and the target is at least 1. Return the length of the shortest run of neighbouring values whose sum is at least the target, or 0 if no run reaches it.
Signature
public static int ShortestRunAtLeast(int[] values, int target)
Examples
[2, 1, 5, 2, 3], target 7 →2. 5 + 2 = 7.[1, 1, 1, 1], target 3 →3. Three ones are the fewest that reach 3.
Limits: Up to 500,000 values, each up to 1,000,000,000, so a sum can pass int.MaxValue.
The tests check: an empty array, one value that reaches the target or not, a single value that is enough on its own, the whole array, a best run at the start or the end, sums beyond int, and 500,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
{
// Every value is at least 1; returns the shortest length of a run whose sum is at least target, or 0.
public static int ShortestRunAtLeast(int[] values, int target)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.