Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4bee36c

Browse files
committed
Add solution #2038
1 parent f71ce91 commit 4bee36c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,700 LeetCode solutions in JavaScript
1+
# 1,701 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1562,6 +1562,7 @@
15621562
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
15631563
2035|[Partition Array Into Two Arrays to Minimize Sum Difference](./solutions/2035-partition-array-into-two-arrays-to-minimize-sum-difference.js)|Hard|
15641564
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
1565+
2038|[Remove Colored Pieces if Both Neighbors are the Same Color](./solutions/2038-remove-colored-pieces-if-both-neighbors-are-the-same-color.js)|Medium|
15651566
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15661567
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15671568
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 2038. Remove Colored Pieces if Both Neighbors are the Same Color
3+
* https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/
4+
* Difficulty: Medium
5+
*
6+
* There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'.
7+
* You are given a string colors of length n where colors[i] is the color of the ith piece.
8+
*
9+
* Alice and Bob are playing a game where they take alternating turns removing pieces from the
10+
* line. In this game, Alice moves first.
11+
* - Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored
12+
* 'A'. She is not allowed to remove pieces that are colored 'B'.
13+
* - Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored
14+
* 'B'. He is not allowed to remove pieces that are colored 'A'.
15+
* - Alice and Bob cannot remove pieces from the edge of the line.
16+
* - If a player cannot make a move on their turn, that player loses and the other player wins.
17+
*
18+
* Assuming Alice and Bob play optimally, return true if Alice wins, or return false if Bob wins.
19+
*/
20+
21+
/**
22+
* @param {string} colors
23+
* @return {boolean}
24+
*/
25+
var winnerOfGame = function(colors) {
26+
let aliceMoves = 0;
27+
let bobMoves = 0;
28+
29+
for (let i = 1; i < colors.length - 1; i++) {
30+
if (colors[i] === 'A' && colors[i - 1] === 'A' && colors[i + 1] === 'A') {
31+
aliceMoves++;
32+
} else if (colors[i] === 'B' && colors[i - 1] === 'B' && colors[i + 1] === 'B') {
33+
bobMoves++;
34+
}
35+
}
36+
37+
return aliceMoves > bobMoves;
38+
};

0 commit comments

Comments
 (0)