Skip to content

Commit bc2aacb

Browse files
solves #3206: solves Alternating Groups I in java
1 parent 15deee8 commit bc2aacb

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Diff for: README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -922,5 +922,10 @@
922922
| 3184 | [Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i) | [![Java](assets/java.png)](src/CountPairsThatFormACompleteDayI.java) | |
923923
| 3190 | [Find Minimum Operations to Make All Elements Divisible by Three](https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three) | [![Java](assets/java.png)](src/FindMinimumOperationsToMakeAllElementsDivisibleByThree.java) | |
924924
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode.com/problems/minimum-average-of-smallest-and-largest-elements) | [![Java](assets/java.png)](src/MinimumAverageOfSmallestAndLargestElements.java) | |
925-
| 3199 | [Count Triplets With Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i) | | |
925+
| 3199 | 🔒 [Count Triplets With Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i) | | |
926926
| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle) | [![Java](assets/java.png)](src/MaximumHeightOfATriangle.java) | |
927+
| 3206 | [Alternating Groups I](https://leetcode.com/problems/alternating-groups-i) | [![Java](assets/java.png)](src/AlternatingGroupsI.java) | |
928+
| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string) | | |
929+
| 3216 | [Lexicographically Smallest String After a Swap](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap) | | |
930+
| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game) | | |
931+
| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal) | | |

Diff for: src/AlternatingGroupsI.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/alternating-groups-i
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class AlternatingGroupsI {
6+
public int numberOfAlternatingGroups(int[] colors) {
7+
int groups = 0;
8+
for (int i = 0 ; i < colors.length ; i++) {
9+
if (isAlternating(colors, i)) {
10+
groups++;
11+
}
12+
}
13+
return groups;
14+
}
15+
private static boolean isAlternating(int[] colors, int startIndex) {
16+
return colors[startIndex] == colors[(startIndex + 2) % colors.length] &&
17+
colors[startIndex] != colors[(startIndex + 1) % colors.length]
18+
}
19+
}

0 commit comments

Comments
 (0)