Блог пользователя mzaferbooshi96

Автор mzaferbooshi96, история, 4 часа назад, По-английски

They love to play chess together. They play n From chess matches. None of them end in a draw. You should compare the number of times the letter "A" is repeated with the letter "D". If the letter "A" is more, type "Anton",If the letter "D" is repeated more than once. type "Danik",If they are tied, type "Friendship".

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

»
4 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by mzaferbooshi96 (previous revision, new revision, compare).

»
3 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Another interesting problem from you, although much harder than the previous one. I will assume that the input is a string.

We can solve this with Divide and Conquer in a similar way that we would construct a segment tree. Let's create an array $$$a$$$, where $$$a_i$$$ is $$$1$$$ if $$$s_i$$$ is 'A', otherwise $$$a_i$$$ is $$$-1$$$. We can recursively calculate the difference between sum of the array by calculating the sum of the left part and the right part. Merging the result is trivial: $$$sum_{L, R} = sum_{L, M} + sum_{M + 1, R}$$$, where $$$M = \lfloor \frac{L + R}{2} \rfloor$$$.

If the sum of the array is positive, the answer is Anton, if it is negative, the answer is Danik, and if it is $$$0$$$ the answer is Friendship.

Time complexity: $$$O(n)$$$

Proof of time complexity: At avery level of the recursion where $$$L \neq R$$$, we split into two more recursions. We can model the recurrence as $$$T(n) = 2 \cdot T(\frac{n}{2}) + O(1)$$$. By the [Master Theorem](https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)) the time complexity is $$$O(n)$$$.

My implementation