FastPrepSliding Window Maximum

Sliding Window Maximum

NVIDIA logoNVIDIA● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums and a window size k, return the maximum value in each contiguous window of exactly k elements. Move the window one position at a time and return the maxima from left to right.

Function

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

Examples

Example 1

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

The three windows have maxima 4, 3 and 5.

Example 2

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

Both windows contain -1; negative values remain valid maxima.

Example 3

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

A window spanning the entire array produces one result.

Constraints

  • 1 <= nums.length <= 10^5.
  • -10^4 <= nums[i] <= 10^4.
  • 1 <= k <= nums.length.

More NVIDIA problems

See NVIDIA hiring insights
public int[] maxSlidingWindow(int[] nums, int k) {
    // Write your code here.
}
nums[4,1,3,2,5]
k3
expected[4,3,5]
Checking account…