diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 8c72ba83..c47179f7 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -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 @@ -16,7 +16,11 @@ import java.util.TreeMap fun main() { - launchPerformance() + getSubArrayBeautyAlternativeSolution( + intArrayOf(1, -1, -3, -2, 3), + 3, + 2 + ).also { it.printArray() } } diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowAlternativeSolution.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowAlternativeSolution.kt index 7c2747ab..fea6fb11 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowAlternativeSolution.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowAlternativeSolution.kt @@ -49,4 +49,64 @@ fun minWindowOptimumSolution(s: String, t: String): String { } return if (minLength == Int.MAX_VALUE) "" else s.substring(minStart, minStart + minLength) -} \ No newline at end of file +} + +/** + * 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() + + 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() +} + + diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt index 17bbbb41..652185ea 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -444,7 +444,7 @@ fun getSubArrayBeauty(nums: IntArray, k: Int, x: Int): IntArray { val map = mutableMapOf() val res = IntArray(nums.size - k + 1) var left = 0 - var window = TreeSet() + val window = TreeSet() for (right in nums.indices) { map[nums[right]] = map.getOrDefault(nums[right], 0) + 1 @@ -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, 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 + } + } +} +