FastPrepMost Negative Number in Each Window

Most Negative Number in Each Window

Wells Fargo logoWells Fargo● MediumINTERNOA
Learn

Problem statement

Given an integer array nums and an integer window length k, return one value for every contiguous subarray of length k, in left-to-right window order.

For a window that contains at least one negative value, return its most negative value: the numerically smallest value in that window. If a window contains no negative value, return 0.

Function

mostNegativeInWindows(nums: int[], k: int) → int[]

Examples

Example 1

nums = [12,-1,-7,8,-15,30,16,28]k = 3return = [-7,-7,-15,-15,-15,0]

The first window [12,-1,-7] contributes -7. The last window [30,16,28] has no negative value, so it contributes 0.

Example 2

nums = [5,4,3]k = 2return = [0,0]

Neither length-2 window contains a negative value.

Example 3

nums = [-2,-2,-1]k = 2return = [-2,-2]

Repeated values are retained normally. The smallest negative value in each window is -2.

Constraints

  • 1 <= nums.length <= 200000.
  • 1 <= k <= nums.length.
  • -10^9 <= nums[i] <= 10^9.

More Wells Fargo problems

See Wells Fargo hiring insights
public int[] mostNegativeInWindows(int[] nums, int k) {
  // write your code here
}
nums[12,-1,-7,8,-15,30,16,28]
k3
expected[-7,-7,-15,-15,-15,0]
Checking account…