02. Remove Colored Pieces if Both Neighbors are the Same Color
The problem can be found at the following link: Question Link
My Approach
Initialize scores for Alice and Bob (
aliceScore
andbobScore
) tozero
.Iterate through the colors (excluding edge pieces) using a loop. For each piece in the iteration:
Check if it's A and its neighboring pieces are also A. If yes, increment
aliceScore
as Alice can remove the current piece.Check if it's B and its neighboring pieces are also B. If yes, increment
bobScore
as Bob can remove the current piece.
Return
true
if Alice's score isgreater
than Bob's score; otherwise, returnfalse
.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
Auxiliary Space Complexity:
O(1)
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated