Distinct Values on Maximum-Sum Frames
Problem statement
Given an integer matrix and frameSize, consider every contiguous frameSize x frameSize square submatrix. Its frame contains the cells on its outer border, each counted exactly once. A frame of size 1 contains its single cell.
Find the maximum frame sum. Among all frames tied for that maximum, collect every integer value that appears on at least one winning frame. Return the sum of those distinct values.
Function
sumDistinctWinningFrameValues(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 sum, and its distinct border values sum to 5 + 6 + 8 + 9 = 28.
Example 2
matrix = [[1,1,1],[1,0,1],[1,1,1]]frameSize = 2return = 1All four frames tie. Their border values have global distinct set {0,1}, whose sum is 1.
Example 3
matrix = [[-3,4],[4,-3]]frameSize = 1return = 4The two single-cell frames containing 4 tie for the maximum; the value 4 is counted only once.
Constraints
1 <= matrix.length, matrix[i].length <= 2001 <= frameSize <= min(matrix.length, matrix[i].length)-1000000000 <= matrix[i][j] <= 1000000000- The result fits in a signed 64-bit integer.