Advertisement
rajeshinternshala

Untitled

Nov 18th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. def playSegments(segments):
  2.     n = len(segments)
  3.     player1_score = 0
  4.     total_score = sum(1 if s == 1 else -1 for s in segments)
  5.  
  6.     # Player 2 plays all segments initially
  7.     player2_score = total_score
  8.  
  9.     # Check if Player 1 should not play at all
  10.     if player1_score > player2_score:
  11.         return 0
  12.  
  13.     for i in range(n):
  14.         # Calculate the score for Player 1 for each segment
  15.         if segments[i] == 1:
  16.             player1_score += 1
  17.         else:
  18.             player1_score -= 1
  19.  
  20.         # Update Player 2's score as if they start from the next segment
  21.         player2_score -= 1 if segments[i] == 1 else -1
  22.  
  23.         # If Player 1's score is greater than Player 2's score from the next segment, return the segments Player 1 should play
  24.         if player1_score > player2_score:
  25.             return i + 1
  26.  
  27.     # If Player 1's score is never greater, they should not play any segments
  28.     return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement