Problem Statement
Given the integers n, k, construct an n by n grid of integers where each row and column has at most k distinct integers. The returned grid must have the maximum number of distinct integers.
Return your answer as a int[] with exactly n * n elements. Element i*n+j of your return value denotes the value of the integer in the i-th row and j-th column of the grid.
If there are multiple optimal solutions, you may return any of them. The integers in your solution can be arbitrary, as long as they fit into a signed 32-bit integer variable.
Definition
- Class:
- DistinctGrid
- Method:
- findGrid
- Parameters:
- int, int
- Returns:
- int[]
- Method signature:
- int[] findGrid(int n, int k)
- (be sure your method is public)
Constraints
- n will be between 3 and 50, inclusive.
- k will be between 1 and n/2, inclusive.
Examples
3
1
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0 }The returned value shown above represents the following grid: { 0,0,0, 0,0,0, 0,0,0 }6
3
Returns: {1, 0, 0, 0, 0, 2, 0, 3, 0, 0, 4, 0, 0, 0, 5, 6, 0, 0, 0, 0, 7, 8, 0, 0, 0, 9, 0, 0, 10, 0, 11, 0, 0, 0, 0, 12 }The returned value shown above represents the following grid: { 1,0,0,0,0,2, 0,3,0,0,4,0, 0,0,5,6,0,0, 0,0,7,8,0,0, 0,9,0,0,10,0, 11,0,0,0,0,12 }4
2
Returns: {123, 999, 999, 999, 999, 999, 999, 456, 999, 789, 999, 999, 999, 999, 240, 999 }The returned value shown above represents the following grid: { 123,999,999,999, 999,999,999,456, 999,789,999,999, 999,999,240,999 };