Question: python solution for There are N players standing in a row, one player on a field. They are numbered from 0 to N 1 from

python solution for There are N players standing in a row, one player on a field. They are numbered from 0 to N1 from left to right. Players perform moves one by one from left to right, that is, in ascending order of numbers. Each player presses an arrow key in one of the four cardinal left ('), right ('>'), up ('^') or down (''). A key press in the given direction means that the player attempts to move onto the closest field in the direction specified. A move can be performed only if there is no other player already standing on the target field. Moves are represented as a string S of length N, where S[K](for K within the range 0.N1) is the direction of the K-th player's move. How many players will actually perform a move successfully? Write a function: class Solution \{ public int solution(String S); } which, given a string S of length N representing arrow keys pressed by each of the players, returns the number of players that will perform a move successfully. Examples: leftwards, because player 0 is standing on the target field. Players 2 and 3 can both perform their moves because there are other play stang their target fields. In the pictures below, players that will perform their moves successfully are marked green and players whose moves will fail are marked red. 2. Given S="^>", your function should return 6. Players 0,1,2,3,4 can all perform their moves because there are no other players standing on their target fields. Player 5 pressed the right-arrow key, but the move cannot be performed as player 6 is already standing on the target field. The move of player 6 can be performed, though, as there is no other player standing on the target field. 3. Given S=">>", your function should return 0. No player can perform a move.
python solution for There are N players standing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!