Top Coding Interview Question – Solved

10 Live
Winner Winner Alice and Bob are playing a game to decide who is better. They both play alternatively. Alice starts first. The subjects and the books are read sequentially. The rules of the game are as follows: - Both of them have N subjects. - Let array S denote the number of books in each subject. - Let array P denote the points for completing a book in a particular subject. - All books on one subject have the same points. In one move, a player has to complete the following steps: - They can skip all books of a subject if they want. They can keep skipping books till they want. Skip here means that the books of the subject become unavailable to read for both of them. As a result, the whole subject becomes unavailable. The player has the choice to skip subjects as long as he wants without losing his turn. - They read one book on a subject if it is available. On reading the book, the points of that person increase by P[i] where P[i] is the points for completing the book. So the number of books on that particular subject decreases by one. Note that he can read only one book in this step. The chance immediately goes to the other person just after he reads. If no book is available, the game ends. The other person continues with the remaining subjects and the remaining books. The game goes on alternatively until no book is available to read.

Asked in: No companies listed

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image Passcode Image

Solution


from functools import lru_cache
from sys import setrecursionlimit
setrecursionlimit(10**6)
def solve (n, s, p):
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem conceptually, we must understand both the game mechanics and the strategic implications of each move. The game involves two players, Alice and Bob, who take turns reading or skipping subjects. The twist lies in the power of skipping: once a subject is skipped, it's gone for good—neither player can access its books anymore. This opens up a deeply strategic component to the game, much like a tactical board game.

The first step in approaching this problem is understanding the structure of the data. We are given two arrays: one representing the number of books in each subject, and the other the points per book in each subject. These arrays are aligned, meaning the i-th index in both arrays refers to the same subject. The first immediate thought should be: some subjects are more valuable than others due to a high combination of book count and points per book. This total point value per subject is a good indicator of how important that subject is to the game.

Now, given that players take turns and can only read one book at a time per turn, we must realize that the game unfolds slowly. Subjects are gradually depleted as books are read, and turns pass back and forth. The strategy for each player, therefore, becomes one of maximizing their own gain while minimizing the potential gain for the opponent. Skipping plays a vital role here: it can be used not only to avoid low-value subjects but also to deny the opponent access to high-value subjects that the player themselves cannot fully exploit.

One crucial insight is to simulate the game while modeling the players’ turns accurately. Since Alice starts first, she always has the advantage of making the first move. This can be vital if the number of total books is odd, as she will get one more turn than Bob. The optimal decisions at each step must consider what subject to choose next or whether to skip a subject entirely.

Another aspect to reflect on is the dynamic availability of subjects. Since skipping is allowed repeatedly until the player reads, the player has a lot of control over how the board is shaped for the opponent. For instance, Alice may choose to skip several low-value subjects just to reach a high-value subject first. However, she needs to weigh this against the risk that the game will eventually reach an end where Bob may get the last valuable books.

When thinking about how to model this behavior, you want to consider a structure that allows you to dynamically update the available subjects and the remaining books in them. A list or queue can be used to represent the subjects, and each subject can be modeled as an object with its current count of books and the value per book.

The core gameplay should be approached with turn-based logic. On each player’s turn, simulate all possible skips followed by a single read, and then switch to the opponent’s turn. To analyze who will win, track the total points accumulated by each player. The moment there are no books left, the game ends, and you can compare scores.

The real challenge in solving this problem lies in deciding what subjects to read from or skip, and in what order. The brute-force way would involve simulating all possible paths of skips and reads, but that can be computationally expensive. So, the player needs a heuristic or strategy to choose the most optimal action during their turn. For example, a greedy strategy might favor the highest-point-per-book subject, but that could backfire if the opponent then gains access to multiple other valuable subjects.

A deeper strategic layer would involve predicting how many books each player can potentially read from a subject before it runs out. Since they alternate turns and can only read one book per turn, you can compute, for a given subject, how many turns it would take to deplete it and which player would get how many of those reads based on whose turn it currently is. This allows for better prediction of point gain per subject for each player.

Finally, this game can be seen as a combination of turn-based strategy and resource control. The players control access to resources (books) and can deny them to the opponent through skips. The optimal approach would be to simulate the game, considering all these factors, and evaluate the final scores of each player based on their moves. It’s essential to simulate each decision point carefully and think in terms of both immediate gain and long-term denial of opportunity to the opponent. You’ll want to implement logic that reflects these priorities in the sequence of moves made by each player.
```


Related Questions