FastPrepDistinct Values on Maximum-Sum Frames

Distinct Values on Maximum-Sum Frames

TikTok logoTikTok● MediumINTERNOA
Learn

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) → long

Examples

Example 1

matrix = [[1,2,3],[4,5,6],[7,8,9]]frameSize = 2return = 28

The 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 = 1

All 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 = 4

The two single-cell frames containing 4 tie for the maximum; the value 4 is counted only once.

Constraints

  • 1 <= matrix.length, matrix[i].length <= 200
  • 1 <= frameSize <= min(matrix.length, matrix[i].length)
  • -1000000000 <= matrix[i][j] <= 1000000000
  • The result fits in a signed 64-bit integer.

More TikTok problems

See TikTok hiring insights
public long sumDistinctWinningFrameValues(int[][] matrix, int frameSize) {
    // Write your code here.
}
matrix[[1,2,3],[4,5,6],[7,8,9]]
frameSize2
expected28
Checking account…