FastPrepDistinct Values on Maximum-Sum Frames

Distinct Values on Maximum-Sum Frames

Capital One logoCapital One● MediumFULLTIMEOA
Learn

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) → 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 border sum, and 5+6+8+9=28.

Example 2

matrix = [[1,1,1],[1,0,1],[1,1,1]]frameSize = 2return = 1

All 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.

More Capital One problems

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