Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def playSegments(segments):
- n = len(segments)
- player1_score = 0
- total_score = sum(1 if s == 1 else -1 for s in segments)
- # Player 2 plays all segments initially
- player2_score = total_score
- # Check if Player 1 should not play at all
- if player1_score > player2_score:
- return 0
- for i in range(n):
- # Calculate the score for Player 1 for each segment
- if segments[i] == 1:
- player1_score += 1
- else:
- player1_score -= 1
- # Update Player 2's score as if they start from the next segment
- player2_score -= 1 if segments[i] == 1 else -1
- # If Player 1's score is greater than Player 2's score from the next segment, return the segments Player 1 should play
- if player1_score > player2_score:
- return i + 1
- # If Player 1's score is never greater, they should not play any segments
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement