Distinct Values on Maximum-Sum Frames
Problem statement
Given an integer matrix and frameSize, consider every contiguous frameSize × frameSize submatrix. Its border contains each cell on its top row, bottom row, left column, or right column exactly once.
Find the maximum border sum. Among every frame tied for that maximum, collect all integer values that occur on a winning border. Return the sum of those distinct values.
Function
sumDistinctWinningBorderValues(matrix: int[][], frameSize: int) → longExamples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]frameSize = 2return = 28The bottom-right 2-by-2 frame has the unique maximum border sum, and 5+6+8+9=28.
Example 2
matrix = [[1,1,1],[1,0,1],[1,1,1]]frameSize = 2return = 1All four frames tie; the union of their border values is {0,1}.
Constraints
1 <= rows, columns <= 200.1 <= frameSize <= min(rows, columns).- Matrix values fit in a 32-bit signed integer; the result fits in a 64-bit signed integer.