Skip to content

Commit 4cc86bc

Browse files
Merge pull request #196
add new variants problem 20.05
2 parents 2f81de3 + 2a9b1dd commit 4cc86bc

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package com.github.contest
44
import com.github.contest.math.numberOfPowerfulInt
55
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
66
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
7-
import com.github.contest.strings.camelMatch
7+
import com.github.contest.slidingWindow.getSubArrayBeautyAlternativeSolution
88
import com.github.contest.strings.fullJustify
99
import com.github.contest.strings.subStrHash
1010
import java.util.TreeMap
@@ -16,7 +16,11 @@ import java.util.TreeMap
1616

1717
fun main() {
1818

19-
launchPerformance()
19+
getSubArrayBeautyAlternativeSolution(
20+
intArrayOf(1, -1, -3, -2, 3),
21+
3,
22+
2
23+
).also { it.printArray() }
2024
}
2125

2226

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,64 @@ fun minWindowOptimumSolution(s: String, t: String): String {
4949
}
5050

5151
return if (minLength == Int.MAX_VALUE) "" else s.substring(minStart, minStart + minLength)
52-
}
52+
}
53+
54+
/**
55+
* 2760. Longest Even Odd Subarray With Threshold
56+
* Alternative Solution with O(n) Time Complexity
57+
*/
58+
59+
fun longestAlternatingSubArrayAlternativeSolution(nums: IntArray, threshold: Int): Int {
60+
var maxLength = 0
61+
var currentLength = 0
62+
63+
for (i in nums.indices) {
64+
65+
if (nums[i] <= threshold &&
66+
(currentLength == 0 && isEven(nums[i]) || currentLength > 0 && nums[i] % 2 != nums[i - 1] % 2)
67+
) {
68+
currentLength++
69+
maxLength = maxOf(maxLength, currentLength)
70+
} else currentLength = if (nums[i] % 2 == 0 && nums[i] <= threshold) 1 else 0
71+
72+
}
73+
74+
return maxLength
75+
}
76+
77+
private fun isEven(number: Int) = when {
78+
number % 2 == 0 -> true
79+
else -> false
80+
}
81+
82+
/**
83+
* 2653. Sliding Subarray Beauty
84+
* Alternative Solution (TLE) must be classic sliding window
85+
*/
86+
87+
fun getSubArrayBeautyAlternativeSolution(nums: IntArray, k: Int, x: Int): IntArray {
88+
var numbers = IntArray(101)
89+
val res = mutableListOf<Int>()
90+
91+
nums.toList().windowed(k) {
92+
it.forEach { elem ->
93+
val value = elem + 50
94+
numbers[value]++
95+
}
96+
var cnt = 0
97+
98+
for (i in numbers.indices) {
99+
if (numbers[i] > 0) cnt += numbers[i]
100+
if (cnt >= x) {
101+
if (i >= 50) res.add(0) else res.add(-(50 - i))
102+
break
103+
}
104+
}
105+
106+
numbers = IntArray(101)
107+
}
108+
109+
return res.toIntArray()
110+
}
111+
112+

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ fun getSubArrayBeauty(nums: IntArray, k: Int, x: Int): IntArray {
444444
val map = mutableMapOf<Int, Int>()
445445
val res = IntArray(nums.size - k + 1)
446446
var left = 0
447-
var window = TreeSet<Int>()
447+
val window = TreeSet<Int>()
448448

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

478+
/**
479+
* 2760. Longest Even Odd Subarray With Threshold
480+
*/
481+
482+
fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
483+
var longest = 0
484+
485+
(nums.size downTo 1).forEach { window ->
486+
nums.toList().windowed(window) {
487+
if (isValid(it, threshold)) {
488+
longest = window
489+
return@windowed
490+
}
491+
}
492+
if (longest > 0) return longest
493+
}
494+
495+
return longest
496+
}
497+
498+
private fun isValid(window: List<Int>, threshold: Int): Boolean {
499+
return when {
500+
window.isEmpty() -> false
501+
window.first() % 2 != 0 -> false
502+
else -> {
503+
504+
for (i in 0..window.size - 2) {
505+
if (window[i] % 2 == window[i + 1] % 2) {
506+
return false
507+
}
508+
}
509+
510+
for (element in window) {
511+
if (element > threshold) {
512+
return false
513+
}
514+
}
515+
516+
return true
517+
}
518+
}
519+
}
520+
478521

479522

480523

0 commit comments

Comments
 (0)