Skip to content

Commit 3fd5e06

Browse files
add 3206
1 parent 2fdee3e commit 3fd5e06

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.contest
22

33

4-
import com.github.contest.hashTable.longestPalindromeAlternativeSolution
54
import com.github.contest.math.numberOfPowerfulInt
65
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
76
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
7+
import com.github.contest.slidingWindow.numberOfAlternatingGroups
88
import com.github.contest.strings.fullJustify
99
import com.github.contest.strings.subStrHash
1010
import java.util.TreeMap
@@ -16,10 +16,7 @@ import java.util.TreeMap
1616

1717
fun main() {
1818

19-
val arr = arrayOf("em", "pe", "mp", "ee", "pp", "me", "ep", "em", "em", "me")
20-
longestPalindromeAlternativeSolution(
21-
arr
22-
).also { println(it) }
19+
numberOfAlternatingGroups(intArrayOf(0, 1, 0)).also { println(it) }
2320
}
2421

2522

contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,38 @@ fun numberOfArithmeticSlices(nums: IntArray): Int {
643643
return count
644644
}
645645

646+
/**
647+
* 3206. Alternating Groups I
648+
*/
649+
650+
fun numberOfAlternatingGroups(colors: IntArray): Int {
651+
var groups = 0
652+
var left = 0
653+
val k = 3
654+
var prevColor = colors[0]
655+
656+
657+
for (right in 1 until colors.size + 2) {
658+
val curr = when {
659+
right >= colors.size -> colors[right - colors.size]
660+
else -> colors[right]
661+
}
662+
663+
if (prevColor == curr) {
664+
left = right
665+
}
666+
667+
prevColor = curr
668+
669+
if (right - left == k - 1) {
670+
groups++
671+
left++
672+
}
673+
}
674+
675+
return groups
676+
}
677+
646678

647679

648680

0 commit comments

Comments
 (0)