Haar1D
SRM 275 · 2005-11-30 · by NeverMore
Problem Statement
The above describes a level-1 transform. To perform a level-2 transform, we repeat the above procedure on the first half of the sequence produced by the level-1 transform. The second half of the sequence remains unchanged from the previous level. This pattern continues for higher level transforms (i.e., a level-3 transform operates with the first quarter of the sequence, and so on). Note that this is always possible when the number of elements is a power of 2.
See the examples for clarification.
Create a class Haar1D with a method transform which takes a
Constraints
- data will contain exactly 2, 4, 8, 16 or 32 elements.
- Each element of data will be between 0 and 100 inclusive.
- L will be between 1 and log2(# of elements in data) inclusive.
{1, 2, 3, 5}
1
Returns: {3, 8, -1, -2 }
Start by forming 3=1+2, the sum of the first pair; 8=3+5, the sum of the second pair; -1=1-2, the difference of the first pair; and finally, -2=3-5, the difference of the second pair. To form the output, we create a sequence of the sums in order, and the differences in order, to obtain {3, 8, -1, -2} as the level-1 transform.
{1, 2, 3, 5}
2
Returns: {11, -5, -1, -2 }
From the above example, the level-1 transform is given by {3, 8, -1, -2} So, the level-2 transform of {1, 2, 3, 5} is simply {11, -5, -1, -2} (11=3+8, -5=3-8).
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
3
Returns: {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
{1, 2, 3, 4, 4, 3, 2, 1}
3
Returns: {20, 0, -4, 4, -1, -1, 1, 1 }
{1,2}
1
Returns: {3, -1 }
Submissions are judged against all 57 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Haar1D with a public method vector<int> transform(vector<int> data, int L) · 57 test cases · 2 s / 256 MB per case