SumRectangle
TCO10 Qual 3 · 2010-04-11 · by misof
Problem Statement
A sum rectangle is a rectangle divided into a grid of unit squares. Each square contains a number, and the numbers in neighboring squares always satisfy the following property:
The number in any square S that is neither in the bottom row nor in the right column can be computed as the sum of the following three numbers:
- The number in the square directly below S.
- The number in the square directly to the right of S.
- The number in the other square adjacent to the previous two squares (the one diagonally down and to the right of S).
An example of a correctly filled sum rectangle:
+----+----+----+----+----+ | 88 | 57 | 33 | 10 | 5 | +----+----+----+----+----+ | 18 | 13 | 11 | 12 | -7 | +----+----+----+----+----+ | 1 | 4 | -2 | 1 | 18 | +----+----+----+----+----+
For example, in the top left corner we have 88 = 18 + 57 + 13.
We have a secret sum rectangle.
You will be given a
Notes
- You may assume that the return value will always fit into an int (i.e., a 32-bit signed integer data type).
Constraints
- leftColumn will contain between 1 and 10 elements, inclusive.
- Each element of leftColumn will be between 0 and 100, inclusive.
- topRow will contain between 1 and 10 elements, inclusive.
- Each element of topRow will be between 0 and 100, inclusive.
- Element 0 of leftColumn will be equal to element 0 of topRow.
{88,18,1}
{88,57,33,10,5}
Returns: 18
This is the rectangle from the problem statement. The lower right corner is uniquely determined by the left column and the top row.
{0,0,0,0}
{0,0,0,0}
Returns: 0
The only correct way to fill this rectangle is to place a zero into each square.
{6,1}
{6,2}
Returns: 3
This is the smallest non-trivial case: +----+----+ | 6 | 2 | +----+----+ | 1 | ?? | +----+----+
{47}
{47}
Returns: 47
{47,42}
{47}
Returns: 42
Submissions are judged against all 50 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SumRectangle with a public method int getCorner(vector<int> leftColumn, vector<int> topRow) · 50 test cases · 2 s / 256 MB per case