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
Definition
- Class:
- SumRectangle
- Method:
- getCorner
- Parameters:
- int[], int[]
- Returns:
- int
- Method signature:
- int getCorner(int[] leftColumn, int[] topRow)
- (be sure your method is public)
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.
Examples
{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
{47}
{47,42}
Returns: 42
{100,0,100,0,100,0,100,0,100,0}
{100,0,100,0,100,0,100,0,100,0}
Returns: 59841700
{0,100,0,100,0,100,0,100,0,100}
{0,100,0,100,0,100,0,100,0,100}
Returns: -86414600
{12,21,21,14,51,25,26,76,0,24}
{12,62,52,35,75,2,53,35,73,21}
Returns: -17584335
{12}
{12,62,52,35,75,2,53,35,73,21}
Returns: 21
{12,62,52,35,75,2,53,35,73,21}
{12}
Returns: 21
{0,0,0,0,0,0}
{0,0,47,0,0}
Returns: -2350
{3,1,4,1,5,9,2,6,5}
{3,5,8,9,7,9,3,2}
Returns: 706
{0,100,0,100,0,100,0,100,0,100}
{0,100,0,100,0,100,0,100,0,100}
Returns: -86414600
{27,9,3,1}
{27,9,3,1}
Returns: 61
{27,9,3,1}
{27,9,3,1,3}
Returns: -119
{100,0,100,0,97,4,100,1,100,0}
{100,10,90,1,98,4,100,1,100,3}
Returns: 54439753
{0}
{0}
Returns: 0
{100}
{100}
Returns: 100
{1,2}
{1}
Returns: 2
{1}
{1,100}
Returns: 100
{0,100}
{0,100}
Returns: -200
{100,2}
{100,2,3}
Returns: -97
{6, 1 }
{6, 2 }
Returns: 3
{1 }
{1 }
Returns: 1
{77 }
{77 }
Returns: 77
{2 }
{2 }
Returns: 2
{0, 1 }
{0, 1 }
Returns: -2
{5 }
{5 }
Returns: 5
{99 }
{99 }
Returns: 99
{50 }
{50 }
Returns: 50
{88, 18, 1 }
{88, 57, 33, 10, 5 }
Returns: 18
{1, 1 }
{1, 1 }
Returns: -1
{77, 88 }
{77 }
Returns: 88
{10 }
{10 }
Returns: 10
{1 }
{1, 2 }
Returns: 2
{7 }
{7 }
Returns: 7
{6, 5, 2, 3 }
{6 }
Returns: 3
{88 }
{88, 2 }
Returns: 2
{1 }
{1, 1, 1, 1 }
Returns: 1
{3 }
{3 }
Returns: 3
{66 }
{66 }
Returns: 66
{1, 2, 3 }
{1 }
Returns: 3
{3, 2 }
{3, 2 }
Returns: -1
{9, 0, 3, 5, 4, 3 }
{9, 3, 9, 7, 3 }
Returns: -1167
{1 }
{1, 6 }
Returns: 6
{3, 1 }
{3 }
Returns: 1
{9 }
{9 }
Returns: 9
{1, 2, 3, 4, 5, 6, 7, 8 }
{1 }
Returns: 8
{88 }
{88, 57 }
Returns: 57