Grid Layer Perimeter Sums
Problem Description
Given an `m x n` integer matrix `grid`, return a list of the sum of elements on each **concentric shell** (layer), starting from the outermost shell and moving inward.
A shell at depth `d` consists of all cells at row-distance `d` from the nearest edge.
**Example 1:**
```
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
```
Output: `[40, 5]`
Explanation: Outermost shell: 1+2+3+6+9+8+7+4 = 40. Inner shell: 5.
**Example 2:**
```
grid = [
[10, 20],
[30, 40]
]
```
Output: `[100]`
Explanation: Only one shell containing all four elements: 10+20+40+30 = 100.
Constraints
- 1 <= m, n <= 200
- -10^4 <= grid[i][j] <= 10^4
Follow-up
Can you generalise this to return the index coordinates of each shell rather than just the sum, in O(m*n) time and O(1) extra space?
Hints
Try the problem first. If you get stuck, you can reveal hints one at a time.
Solution
Leaderboard
No entries yet for python.
Be the first — submit an accepted solution.