I am trying to solve Ball Removal problem on topcoder, pasting the problem statement here as login is required to access this link.
Problem Statement
You have N balls, where N is odd. The balls are numbered from 0 to N-1. In that order, they are arranged into a row going from the left to the right.
In addition to the number, each ball has either the word “left” or the word “right” written on it. For simplicity, we will use the character ‘<‘ instead of “left”, and the character ‘>’ instead of “right”. You are given the labels on all balls as the String label. For each i, character i of label represents the word on ball i.
You will now repeat the following procedure:
- Choose a ball that is not at either end of the row of balls.
- If the chosen ball has the label ‘<‘, remove the chosen ball and also the ball immediately to the left of it. Otherwise, remove the chosen ball and also the ball to the right of it.
- Without reordering the remaining balls, push them together to get rid of the gap created in the previous step.
The process ends when only one ball remains in the row. That ball is called the survivor. Note that the numbers on the balls do not change during the process.
Find all possible survivors. Your method must return a String containing exactly N characters. If ball i can be the survivor, character i of the return value must be ‘o’ (lowercase oh). Otherwise, the corresponding character must be ‘.’ (a period).
Constraints
- label will contain between 3 and 49 characters, inclusive.
- label will contain an odd number of characters.
- Each character of label will be either ‘>’ or ‘<‘.
Examples
“<<>”
Returns: “..o”Initially, you have three balls. Since you cannot choose balls at the ends of the row, you have to choose ball 1. As its label is ‘<‘, you remove balls 0 and 1. Hence the only possible survivor is ball 2.
1)
“>>><<”
Returns: “o…o”If you choose ball 2 or ball 3 first, you have to choose ball 1 next, and the survivor will be ball 0. If you choose ball 1 first, you have to choose ball 3 next, and the survivor will be ball 4.
2)
“<<><<”
Returns: “….o”3)
“<><<><>”
Returns: “o…..o”4)
“>>><<<>>>>><<<>”
Returns: “o…..o.o…..o”
I am thinking of a dynamic programming approach to this problem, I am thinking of having an boolean array to mark which of the characters have been deleted and then find which is next left and next right but that makes the approach quite inefficient and I have to write a recursive method. For implementing a dynamic programming approach I need to maintain a state. But I am not able to figure out what I should keep as state, in my thinking a state is combination of both current string and current index, but maintaining a string for state doesn’t seem correct to me.
One more problem I am facing is that in this case I don’t have a particular direction if I change direction result changes also if I move left to right I might need to move right to left also.
Please help me in finding a proper approach to this problem.
The state can be boolean –
DP[left][right][isLeftBoundary][isRightBoundary].This means if the substring starting at
leftand finishing atrightcan be fully eliminated.isLeftBoundaryis just a boolean flag if theleftsymbol is the leftmost of the string.isRightBoundaryis just a boolean flag if therightsymbol is the rightmost of the string.if
DP[0][i - 1][1][0]andDP[i + 1][N][0][1]aretrue, it means the ball at positionican remain.