Skip to content

Commit 2a9b1dd

Browse files
add 2653
1 parent cc2a0c0 commit 2a9b1dd

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,36 @@ fun longestAlternatingSubArrayAlternativeSolution(nums: IntArray, threshold: Int
7777
private fun isEven(number: Int) = when {
7878
number % 2 == 0 -> true
7979
else -> false
80-
}
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: 1 addition & 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

0 commit comments

Comments
 (0)