The first problem was posted on Monday. The statement says that you're give a string of letters. The task is to find the index of a letter in the string such that after removing that letter is a palindrome. It's guaranteed that such an index exists. One test file consists of at most 20 test cases.
There are two cases. The string can be already a palindrome or not.
It's easy to check, that if is already a palindrome, then after removing the -th letter, remains a palindrome.
If is not a palindrome, then there exists an index such that . Let be the smallest such index.
Again, it's easy to see that we have to remove either or , because after removing any for , will be compared with the same letter that it was compared in the original and the result of that comparison is still negative. In addition, it makes no sense to remove any for or because with that action we won't achieve anything positive.
From the problem statement, we know that at least one of and is a palindrome. If is a palindrome, then the result is . In the other case, the result is .
The time complexity of that solution is .
In Tuesday problem, you're given an array , where . Initially for each .
Next, there are queries. Each query consists of 3 integers , and it adds to every where . Your task is to return the maximal value in the array after performing these operations. The full problem statement is here.
At the first sight it seems like a segment tree problem, but it would be an overkill here. The crucial thing there is that we only have to compute a maximal value once, so we can first "perform" all queries and then calculate the result. The next simple observation is that we can only consider elements for which there is at least one query where or equals . That observation led us to a solution based on sorting.
Let's thing of each query as of two event. Each event has it's own index and value. For the query the first event is to add a value to the index and the second event is to subtract a value from the index . After that, we can sort these events. We say that event1 < event2 iff. the index of event1 is less than the index of event2 or these indices are equal and event1 is the opening event i.e. it adds a value. Next we can process all the events from left to right keeping track of the current value and the maximum value. We update the maximum value if the current value is greater than the maximum value collected so far.
The total time complexity of that method is and it's worth to mention, that it doesn't depend on at all.
Wednesday problem is more difficult, but not so much While the original problem statement has some story behind it, the real task is the following:
You're given distinct integers. The task is to count how many distinct Binary Search Trees (BST) can be created by picking any non-empty subset of these integers. Because the result can be very large, you have to compute it modulo , but in order to provide a clear solution I'll omit that. There are at most testcases in one test file.
There are two crucial observations. If you pick two different subsets, the trees created from these subsets are different. On the other hand, if you pick two subsets with the same number of elements, the number of different trees created from each subset is the same, because only ranks of elements matter, not their relative values.
Let be the number of different BST that can be created from integers and assume that we can compute that value.
Then using two above facts, the result can be computed as:
because we can compute the number of different BST for each subset separately and that number is the same for subsets of the same size.
But how to compute for any ?
If we have elements, we can put any of them in the root of a tree. If we put the -th smallest of them, smallest elements have to be in the left subtree of the root and largest elements have to be in the right subtree. The number of possible different BST as a left subtree is and it's for the right subtree. This led us to the recursive formula:
The base case is:
because there is only one empty BST.
In the full solution, we precompute table and binomial coefficients . These two precomputations take time. After that, we can compute every testcase in time which gives time, where is the number of testcases.