PIXELBANKv9.1.0
Menu

Seam Carving - Find Minimum Energy Path

Problem Statement

Seam Carving is a content-aware image resizing technique that removes pixels along low-energy "seams" (paths) to reduce image width while preserving important features.

Given a 2D energy matrix where each cell represents the "energy" (importance) of a pixel, find the minimum total energy of a vertical seam from top to bottom.

A vertical seam is a path of pixels from top row to bottom row, where each pixel in the seam is connected to the pixel directly below it or diagonally below-left or below-right (i.e., you can move to (row+1, col-1), (row+1, col), or (row+1, col+1)).

Constraints

  • 1≤rows,cols≤1001 \leq rows, cols \leq 100
  • 0≤energy[i][j]≤10000 \leq energy[i][j] \leq 1000

Example

For energy matrix:

[[1, 4, 3],
 [5, 2, 6],
 [9, 8, 7]]

The minimum seam goes: 1 → 2 → 7 = 10

Example:

Input:
energy = [[1, 4, 3], [5, 2, 6], [9, 8, 7]]
Output:
10
Reasoning:

Path: (0,0)→(1,1)→(2,2) gives 1+2+7=10

solution.py

Test Results

0/0
Run code to see test results.
Seam Carving - Find Minimum Energy Path - Medium | PixelBank