FunctionRacerProblemsRace

Stretches With Median K

HardHash mapsPython, JavaScript, TypeScript, Java, C++

All items of nums are different, and k is one of them. The median of a stretch (one or more items next to each other) is its middle item once the stretch is sorted. A stretch of even length has two middle items, and its median is the smaller one.

Return how many stretches of nums have median k.

Example 1

nums [4,1,3,2,5]
k 3
returns 4

The stretches are [3], [4, 1, 3], [3, 2, 5] and the whole list.

Example 2

nums [1,2,3]
k 2
returns 3

The stretches are [2], [2, 3] and the whole list. In [1, 2] the smaller middle item is 1.

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 Hash maps problems