TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Max Pooling 2D

Computer Vision
Medium

Max pooling is a downsampling operation commonly used in convolutional neural networks. It reduces the spatial dimensions of a feature map by selecting the maximum value within non-overlapping rectangular regions (pools). This helps reduce computation, extract dominant features, and provide a degree of spatial invariance.

Given a 2D matrix and a pool size, apply max pooling with non-overlapping windows (stride equal to pool size).

Algorithm

  1. Compute the output dimensions by dividing the input dimensions by the pool size (integer division):
Hout=⌊Hp⌋H_{out} = \left\lfloor \frac{H}{p} \right\rfloorHout​=⌊pH​⌋ Wout=⌊Wp⌋W_{out} = \left\lfloor \frac{W}{p} \right\rfloorWout​=⌊pW​⌋
  1. For each output position (i, j), examine the corresponding p × p window in the input starting at (i·p, j·p), and select the maximum value:
out[i][j]=max⁡0≤a,b<pX[i⋅p+a][j⋅p+b]\text{out}[i][j] = \max_{0 \le a,b < p} X[i \cdot p + a][j \cdot p + b]out[i][j]=0≤a,b<pmax​X[i⋅p+a][j⋅p+b]

Return the pooled two-dimensional list.

Loading visualization...

Examples

Input: X = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], pool_size = 2

Output: [[6, 8], [14, 16]]

Explanation: Each non-overlapping 2 by 2 block contributes its maximum.

Input: X = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]], pool_size = 3

Output: [[15, 18], [33, 36]]

Hint 1

Use integer division to count complete pooling windows along each dimension.

Hint 2

Initialize each maximum from the window’s top-left value, then scan that window.

Requirements

  • Apply non-overlapping max pooling with stride equal to pool_size
  • Select the maximum value from each pooling window
  • Handle rectangular inputs where dimensions may not be square
  • Discard any remaining rows or columns that don't form a complete pool
  • Return the pooled 2D matrix as a list of lists

Constraints

  • X is a non-empty 2D matrix of numbers
  • pool_size >= 1
  • Input dimensions are at least pool_size in both directions
  • Return a 2D list of numbers
  • Time limit: 300 ms
Try Similar Problems
Average Pooling 2dGlobal Avg PoolingConv2d Image FilteringSimple Cnn LayerRoi Pooling

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Max Pooling 2D

Computer Vision
Medium

Max pooling is a downsampling operation commonly used in convolutional neural networks. It reduces the spatial dimensions of a feature map by selecting the maximum value within non-overlapping rectangular regions (pools). This helps reduce computation, extract dominant features, and provide a degree of spatial invariance.

Given a 2D matrix and a pool size, apply max pooling with non-overlapping windows (stride equal to pool size).

Algorithm

  1. Compute the output dimensions by dividing the input dimensions by the pool size (integer division):
Hout=⌊Hp⌋H_{out} = \left\lfloor \frac{H}{p} \right\rfloorHout​=⌊pH​⌋ Wout=⌊Wp⌋W_{out} = \left\lfloor \frac{W}{p} \right\rfloorWout​=⌊pW​⌋
  1. For each output position (i, j), examine the corresponding p × p window in the input starting at (i·p, j·p), and select the maximum value:
out[i][j]=max⁡0≤a,b<pX[i⋅p+a][j⋅p+b]\text{out}[i][j] = \max_{0 \le a,b < p} X[i \cdot p + a][j \cdot p + b]out[i][j]=0≤a,b<pmax​X[i⋅p+a][j⋅p+b]

Return the pooled two-dimensional list.

Loading visualization...

Examples

Input: X = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], pool_size = 2

Output: [[6, 8], [14, 16]]

Explanation: Each non-overlapping 2 by 2 block contributes its maximum.

Input: X = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]], pool_size = 3

Output: [[15, 18], [33, 36]]

Hint 1

Use integer division to count complete pooling windows along each dimension.

Hint 2

Initialize each maximum from the window’s top-left value, then scan that window.

Requirements

  • Apply non-overlapping max pooling with stride equal to pool_size
  • Select the maximum value from each pooling window
  • Handle rectangular inputs where dimensions may not be square
  • Discard any remaining rows or columns that don't form a complete pool
  • Return the pooled 2D matrix as a list of lists

Constraints

  • X is a non-empty 2D matrix of numbers
  • pool_size >= 1
  • Input dimensions are at least pool_size in both directions
  • Return a 2D list of numbers
  • Time limit: 300 ms
Try Similar Problems
Average Pooling 2dGlobal Avg PoolingConv2d Image FilteringSimple Cnn LayerRoi Pooling

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.