PE2 (AY23/24)
Last updated
Last updated
Questions:
Official Answers:
A give-away question.
Another give-away question using .
This is an interesting problem which tests yourself about your ability to break a problem into small parts. The rule is easy to understannd, but the genius point in the question is the way how it is implememnted.
Firstly, since we have 8 directions, the method we use to count the flips in each direction should be the same except that how we transit to the next cell may be different. To use this pattern, we can define a fixed length 2-dimensional array. (Genius point 1)
Now, we need to implement our count_flips_in_one_dir()
. This is done by flipping all the zeros in that direction within range. And at last, we check whether the cell we stopped at is one or not. If it is, we return count-1 (-1 is for the last cell which is one, this doesn't need to be flipped). If not, means we cannot flip any zero in this direction, thus we return 0. (Genius point 2)
The same idea can be used in the actual flipping of the cell.
Then, in our main()
function, we just need to memorize the position with maximum count flips. And then use that position to actually flip the cells accordingly.
Or similarly speaking, output the number of "#" in a string
Basically, it utilizes the fact that our input is a string, so if we reach the edge case when all the characters in the string is "#", now histogram[r][mid+1]
will be \0
. And when we exit the loop, it will output the last position.
The modification just adds the row index constraint.
The comments has provided good enough explanation. And one thing to note in the generate()
function below is that in Line 14, we will only try to pair the people behind curr
with curr
because since we start from 0, so means we have already found the possibility for the pair before curr
.
But the print()
part is still a bit tricky since we need to print all the pairs first, then print the individual.
Include the variation of Binary Search in PE2 Review, and study it further. This is quite important. Maybe need to include Tut 08 - Searching and Sorting into PE2 Review also.
Include the common functions from C Standard Libaray into PE2 Review, like isupper()
and islower()
from <ctype.h>
.
Know how to define and initialize a fixed length 2-dimensional array in C from 3. Reversi*. Include in the PE2 Review.
This is another question (the previous one is ) that is the variant of Binary Search, which means that the variation of Binary Search is very important).
The idea is pretty similar and the implementation you can just use the one in . The only thing I want to discuss is how to reach the time complexity . Use Binary Search on each rows takes and on each column takes . So, the idea is that we do binary search , then use the binary seach to to compare the score.
This is a variation of , it tests the backtracking idea.