Approach
Consensus
- Give and , where is the number of rows and is the number of columns. Both and are even Integer (整数)
- We need to return a matrix that is , each cells contains a value of or . We need to make sure each cell has exactly 2 adjacent cells that have an opposite value. So if the current cell has a value of , it needs to have 2 adjacent cells that have a value of
Idea
- Lets first break down the problem into its smallest form
- What is the smallest form of the problem? Basically it is the smallest value for both and . Since and are both even integers, and the smallest even integers are . Thus, the smallest form of the problem is for both and
- Below is the only 2 possible solution to this smallest form of the problem. Feel free to double check it. One thing to note here is that, the answer here is self-contained
- Self-contained means that the answer to the smallest problem has a stable state, if we want to build on top the it, we need to avoid disrupting this stable state
- How to build on top and avoid disrupting the stable state? Actually it is very simple, there is this pattern as shown below. We extend 1st answer with 2nd answer, and vice verse
- Since and are event integers, we can have , , where and are integer
- , and the smallest matrix has a value of . So we can conclude that we can use smallest matrix to form the final matrix
- We just need to make sure each smallest matrix is adjacent to a different type of the smallest matrix, 1st type matrix is adjacent to 2nd type matrix, vice versa
Implementation Details
- We can make use XOR Bitmasking to populate each row of the matrix with the smallest matrix
- We also need to make use of XOR Bitmasking when we need to populate the next row
Conclusion
- Break down the problem into its smallest form with the number theory concept (definition of even number), and find what are the possible solutions for the smallest form
- Then see how can we build on top of the smallest form, and eventually obtain overall answer - Dynamic Programming
- For implementation, we make use of bitwise operator XOR to populate the matrix efficiently
Space & Time Analysis
The analysis method we are using is Algorithm Complexity Analysis
Space - O(nm)
- Ignore input size & language dependent space
- We need to create an output buffer that contains the result matrix, to avoid time timeout due to too many IO operations
Time - O(nm)
- We need to populate each cell one by one
Codes
1st Attempt (Java)
Personal Reflection
- Why it takes so long to solve: Overwhelmed by the given problem, unable to break down the problem into its smallest form and build on top of it
- What you could have done better: Try to break down the problem into is smallest form, and see how to build on top of it
- What you missed: Simplification of the problem with the help of number theory concepts, and the dynamic programming component
- Ideas you’ve seen before: Dynamic programming and XOR Bitwising
- Ideas you found here that could help you later: Problem simplification
- Ideas that didn’t work and why: Bitwise, bitwise is only used to help with the implementation, not a core component that is tested here