Skip to content

Commit c149882

Browse files
Merge pull request #200
add new problems 26.05
2 parents 1671f66 + a48d2a6 commit c149882

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,87 @@ 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+
678+
/**
679+
* 1652. Defuse the Bomb
680+
*/
681+
682+
fun decrypt(code: IntArray, k: Int): IntArray = when {
683+
k == 0 -> IntArray(code.size)
684+
k > 0 -> {
685+
val res = IntArray(code.size)
686+
for (i in code.indices) {
687+
var sum = 0
688+
var right = i + 1
689+
var k = k
690+
691+
while (k != 0) {
692+
sum += when {
693+
right >= code.size -> code[right - code.size]
694+
else -> code[right]
695+
}
696+
right++
697+
k--
698+
}
699+
700+
res[i] = sum
701+
}
702+
res
703+
}
704+
705+
else -> {
706+
val res = IntArray(code.size)
707+
for (i in code.indices) {
708+
var sum = 0
709+
var right = i - 1
710+
var k = k
711+
712+
while (k < 0) {
713+
sum += when {
714+
right < 0 -> code[code.size + right]
715+
else -> code[right]
716+
}
717+
right--
718+
k++
719+
}
720+
721+
res[i] = sum
722+
}
723+
res
724+
}
725+
}
726+
646727

647728

648729

0 commit comments

Comments
 (0)