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
8 changes: 6 additions & 2 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.github.contest
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.strings.camelMatch
import com.github.contest.slidingWindow.getSubArrayBeautyAlternativeSolution
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
import java.util.TreeMap
Expand All @@ -16,7 +16,11 @@ import java.util.TreeMap

fun main() {

launchPerformance()
getSubArrayBeautyAlternativeSolution(
intArrayOf(1, -1, -3, -2, 3),
3,
2
).also { it.printArray() }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,64 @@ fun minWindowOptimumSolution(s: String, t: String): String {
}

return if (minLength == Int.MAX_VALUE) "" else s.substring(minStart, minStart + minLength)
}
}

/**
* 2760. Longest Even Odd Subarray With Threshold
* Alternative Solution with O(n) Time Complexity
*/

fun longestAlternatingSubArrayAlternativeSolution(nums: IntArray, threshold: Int): Int {
var maxLength = 0
var currentLength = 0

for (i in nums.indices) {

if (nums[i] <= threshold &&
(currentLength == 0 && isEven(nums[i]) || currentLength > 0 && nums[i] % 2 != nums[i - 1] % 2)
) {
currentLength++
maxLength = maxOf(maxLength, currentLength)
} else currentLength = if (nums[i] % 2 == 0 && nums[i] <= threshold) 1 else 0

}

return maxLength
}

private fun isEven(number: Int) = when {
number % 2 == 0 -> true
else -> false
}

/**
* 2653. Sliding Subarray Beauty
* Alternative Solution (TLE) must be classic sliding window
*/

fun getSubArrayBeautyAlternativeSolution(nums: IntArray, k: Int, x: Int): IntArray {
var numbers = IntArray(101)
val res = mutableListOf<Int>()

nums.toList().windowed(k) {
it.forEach { elem ->
val value = elem + 50
numbers[value]++
}
var cnt = 0

for (i in numbers.indices) {
if (numbers[i] > 0) cnt += numbers[i]
if (cnt >= x) {
if (i >= 50) res.add(0) else res.add(-(50 - i))
break
}
}

numbers = IntArray(101)
}

return res.toIntArray()
}


Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ fun getSubArrayBeauty(nums: IntArray, k: Int, x: Int): IntArray {
val map = mutableMapOf<Int, Int>()
val res = IntArray(nums.size - k + 1)
var left = 0
var window = TreeSet<Int>()
val window = TreeSet<Int>()

for (right in nums.indices) {
map[nums[right]] = map.getOrDefault(nums[right], 0) + 1
Expand Down Expand Up @@ -475,6 +475,49 @@ fun getSubArrayBeauty(nums: IntArray, k: Int, x: Int): IntArray {
return res
}

/**
* 2760. Longest Even Odd Subarray With Threshold
*/

fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
var longest = 0

(nums.size downTo 1).forEach { window ->
nums.toList().windowed(window) {
if (isValid(it, threshold)) {
longest = window
return@windowed
}
}
if (longest > 0) return longest
}

return longest
}

private fun isValid(window: List<Int>, threshold: Int): Boolean {
return when {
window.isEmpty() -> false
window.first() % 2 != 0 -> false
else -> {

for (i in 0..window.size - 2) {
if (window[i] % 2 == window[i + 1] % 2) {
return false
}
}

for (element in window) {
if (element > threshold) {
return false
}
}

return true
}
}
}




Expand Down