Max Pooling 1D

MediumMachine LearningArraySliding Window

Description

Given a 1D array and a pool size k, apply non-overlapping 1D max pooling: split the array into consecutive windows of size k and return the maximum of each window. The final window may be smaller when the length is not divisible by k.

Examples

Input:[1,3,2,5,4,6], 2
Output:[3,5,6]
Explanation:

The array is broken into consecutive windows of the pool size and each window collapses to its largest value.

Input:[1,2,3,4], 2
Output:[2,4]
Explanation:

The array is broken into consecutive windows of the pool size and each window collapses to its largest value.

Input:[5,1,3], 1
Output:[5,1,3]
Explanation:

The array is broken into consecutive windows of the pool size and each window collapses to its largest value.

Constraints

  • 1 ≤ length ≤ 10⁴
  • 1 ≤ k ≤ length

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.