FunctionRacerProblemsRace

Shortest Stretch Reaching a Total

HardTwo pointersPython, JavaScript, TypeScript, Java, C++

nums may hold negative numbers. Return the length of the shortest stretch of nums (one or more items next to each other) whose sum is at least target, or -1 if no stretch reaches it.

Example 1

nums [4,-2,3,1]
target 5
returns 3

4 - 2 + 3 = 5 takes three items. No single item and no two neighbors reach 5.

Example 2

nums [1,2]
target 4
returns -1

Everything together is only 3.

Example 3

nums [6,-9,6]
target 6
returns 1

A single 6 is enough.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems