Kick Start 2019 Round H
Because Europe was on winter time, I was in UTC +1.00, so for this contest I deliberately got up at 6 a.m. to participate. After finishing, I rested for an hour, then went to Ghent with friends for the whole day. When I came back at night, I suddenly remembered that Machine Learning assignment 2 was due that day, and then started rushing the deadline like crazy. Facts proved that without studying seriously, I still could not handle the assignment. I submitted something rough; better than not submitting at all. For the blank questions, I generously told the TA that I simply did not know how to do them.
This was the last round of Kick Start 2019, and I still really wanted to participate. This year I participated in six Kick Start rounds in total. Although I already got an interview invitation in Round A, I still could not afford to be careless. The rankings for each round are shown below:
| Round | A | D | E | F | G | H |
|---|---|---|---|---|---|---|
| Rank | 600 | 765 | 1566 | 1341 | 462 | 330 |
| Score | 35 | 27 | 22 | 11 | 42 | 41 |
Except for Round F, when I was traveling in Paris and just messed around with the problems at night in the hostel, I can say I gave my full effort in the other rounds.
Overall, my rank first went down and then went up. It does not reflect a change in ability so much as a change in mindset. Round D was relatively important, and at that time a classmate was competing too, so I did not have the heart to communicate with him during the contest. During Round E, I was at ByteDance’s summer camp and solved it together with a girl. I did want to do well, but because my Union-Find did not implement path compression in find, it caused TLE. This was also a blind spot I had not noticed before. The other three rounds with relatively good results were instead the ones where I had no burden and was more relaxed.
Recently I also unexpectedly received an invitation to Google’s Beijing A Day with Google. Unfortunately I was not in Beijing anymore, so I had to decline. In addition, I emailed Google to report that all Google recruiting-related forms did not include Beihang University in the “My University” options. I guessed this might be because Beihang was on the U.S. Department of Commerce blacklist. Google resolved it quickly. From now on, my Beihang is no longer treated like a diploma mill.
In this contest, I solved the warm-up problem and the small test set 1 of problem 3. I guess that counts as a normal performance.
A. H-index
The first time I tried it, I misunderstood the statement and thought it only asked for the final H-index. I simply wrote a binary search solution that used a decision problem to find the maximum value.
Later I realized that the result after each paper was needed, and then several bugs I wrote affected the time cost.
Intuition:
As papers are published, the H-index is monotonically non-decreasing. Using this property, after each paper is published, try to increase the H-index.
Here I used the ordered property of C++ STL set and the iterator stability of node-based containers after inserting new nodes, and efficiently implemented the operation of trying to increase the H-index.
Time complexity: O(N log N),
space complexity: O(N).
1 |
|
B. Diagonal Puzzle
During the contest I tried to enumerate all flips through backtracking to find the minimum solution, with time complexity O(2 ^ (4 * N)). I did not expect even the smallest dataset to time out. So the solutions below were learned from the official Analysis after the contest.
Small Test Set
There is no need to enumerate all flips. We can enumerate only the flips in one direction, then check whether every line in the other direction has the same color.
There are 2 * N - 1 flips parallel to the main diagonal, and also 2 * N - 1 flips perpendicular to the main diagonal.
The complexity of checking same color is O(N ^ 2).
So the total time complexity is O(2 ^ (2 * N - 1) * N ^ 2).
Large Test Set
This solution is based on an interesting observation: if we determine the flips of the main diagonal and anti-diagonal, then in order to make everything white in the end, all other flips can be determined from that. When N is odd, it is the main diagonal and the secondary anti-diagonal. The cases for N == 6, 7 are shown below.
1 | \..../ \...... |
So we can enumerate the flip states of the main diagonals, four cases in total, and then determine the other flips according to the colors of the other elements on the diagonals. Finally, check whether everything is white.
Time complexity: O(4 * N ^ 2).
This solution is actually very similar to the light-flipping problems I often did before: flipping one light also flips its four surrounding lights, and the goal is to find the number of steps needed to turn all lights off.
You only need to enumerate the flips in the first row. To change the state of the lights in the current row, we can only flip lights in the next row, so all remaining rows are determined from that.
The problem can be found on LeetCode: LeetCode 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix.
My solution was learned from acwing. You can also watch Da Xue Cai’s video to learn it.
Another Solution
Convert this problem into a variant of 2-coloring. Each cell is shared by two lines. If the cell is white, exactly one of the two lines needs to be flipped. If the cell is black, either neither is flipped, or both are flipped.
We treat each diagonal as a node in the graph, and each cell as an edge.
If the cell is black, the two nodes connected by the corresponding edge must have the same color. Otherwise, they must have different colors.
This 2-coloring graph problem can be solved with DFS.
Enumerate the color of the root, use DFS to determine the colors of its neighbors, and if a neighbor’s color has already been determined, just check it. The smaller color class represents the number of flips.
The number of edges is O(N^2), and the DFS solution has complexity O(|Edges|).
The total time complexity is O(N ^ 2).
1 |
|
Points to pay attention to during implementation:
- How to store and construct the Graph.
- When using DFS, you need to traverse all nodes, because the whole graph can be divided into multiple connected components. Starting DFS from only one root gives incomplete coloring.
C. Elevanagram
For the small test set, 0 <= A_i <= 20, we can use backtracking and try every positive/negative sign assignment.
Time complexity: O(A_i ^ 9). With pruning, the complexity will be lower.
Space complexity: O(9).
1 |
|