Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.github.contest


import com.github.contest.hashTable.longestPalindromeAlternativeSolution
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.slidingWindow.numberOfAlternatingGroups
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
import java.util.TreeMap
Expand All @@ -16,10 +16,7 @@ import java.util.TreeMap

fun main() {

val arr = arrayOf("em", "pe", "mp", "ee", "pp", "me", "ep", "em", "em", "me")
longestPalindromeAlternativeSolution(
arr
).also { println(it) }
numberOfAlternatingGroups(intArrayOf(0, 1, 0)).also { println(it) }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,87 @@ fun numberOfArithmeticSlices(nums: IntArray): Int {
return count
}

/**
* 3206. Alternating Groups I
*/

fun numberOfAlternatingGroups(colors: IntArray): Int {
var groups = 0
var left = 0
val k = 3
var prevColor = colors[0]


for (right in 1 until colors.size + 2) {
val curr = when {
right >= colors.size -> colors[right - colors.size]
else -> colors[right]
}

if (prevColor == curr) {
left = right
}

prevColor = curr

if (right - left == k - 1) {
groups++
left++
}
}

return groups
}

/**
* 1652. Defuse the Bomb
*/

fun decrypt(code: IntArray, k: Int): IntArray = when {
k == 0 -> IntArray(code.size)
k > 0 -> {
val res = IntArray(code.size)
for (i in code.indices) {
var sum = 0
var right = i + 1
var k = k

while (k != 0) {
sum += when {
right >= code.size -> code[right - code.size]
else -> code[right]
}
right++
k--
}

res[i] = sum
}
res
}

else -> {
val res = IntArray(code.size)
for (i in code.indices) {
var sum = 0
var right = i - 1
var k = k

while (k < 0) {
sum += when {
right < 0 -> code[code.size + right]
else -> code[right]
}
right--
k++
}

res[i] = sum
}
res
}
}




Expand Down