Skip to content

Commit 0f21207

Browse files
Merge pull request #191
add new problems 11.05
2 parents 2c3e3f1 + 6c9171e commit 0f21207

File tree

7 files changed

+227
-24
lines changed

7 files changed

+227
-24
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.hashTable.countGoodAlternativeSolution
5-
import com.github.contest.hashTable.numEquivDominoPairsProdVariant
6-
import com.github.contest.hashTable.testing
4+
import com.github.contest.array.minSum
75
import com.github.contest.math.numberOfPowerfulInt
86
import com.github.contest.strings.fullJustify
97
import com.github.contest.strings.subStrHash
@@ -16,15 +14,15 @@ import java.util.TreeMap
1614

1715
fun main() {
1816

19-
numEquivDominoPairsProdVariant(
20-
arrayOf(
21-
intArrayOf(1, 2), intArrayOf(2, 1), intArrayOf(3, 4), intArrayOf(5, 6)
22-
)
17+
minSum(
18+
intArrayOf(0, 0, 10, 10, 12, 0, 13, 6, 0, 2, 10),
19+
intArrayOf(24, 5, 12, 22)
2320
).also { println(it) }
2421
}
2522

2623
fun countGoodData() {
27-
countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) }
24+
25+
2826
}
2927

3028
fun fullJustifyData() {

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,55 @@ fun merge(intervals: Array<IntArray>): Array<IntArray> {
295295
return res.toTypedArray()
296296
}
297297

298+
/**
299+
* 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros
300+
*/
301+
302+
fun minSum(nums1: IntArray, nums2: IntArray): Long {
303+
val zerosOne = nums1.countLong { it == 0 }
304+
val zerosTwo = nums2.countLong { it == 0 }
305+
val sumOne = nums1.sumLong() + zerosOne
306+
val sumTwo = nums2.sumLong() + zerosTwo
307+
308+
return when {
309+
sumOne == sumTwo -> sumOne
310+
sumOne < sumTwo -> if (zerosOne > 0) sumTwo else -1L
311+
else -> if (zerosTwo > 0) sumOne else -1L
312+
}
313+
}
314+
315+
private fun IntArray.countLong(predicate: (Int) -> Boolean): Long {
316+
var count = 0L
317+
for (element in this) {
318+
if (predicate(element)) count++
319+
}
320+
return count
321+
}
322+
323+
private fun IntArray.sumLong(): Long {
324+
var sum = 0L
325+
for (element in this) sum += element
326+
return sum
327+
}
328+
329+
/**
330+
* 1550. Three Consecutive Odds
331+
*/
332+
333+
fun threeConsecutiveOdds(arr: IntArray): Boolean {
334+
var consecutive = 3
335+
336+
for (num in arr) {
337+
if (isOdd(num)) consecutive--
338+
else consecutive = 3
339+
340+
if (consecutive == 0) return true
341+
}
342+
343+
return false
344+
}
345+
346+
fun isOdd(number: Int) = when {
347+
number % 2 != 0 -> true
348+
else -> false
349+
}

contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,32 @@ fun mergeProdVariantII(intervals: Array<IntArray>): Array<IntArray> =
120120
it[1] = maxOf(it[1], interval[1])
121121
} ?: merged.add(interval)
122122
merged
123-
}.toTypedArray()
123+
}.toTypedArray()
124+
125+
/**
126+
* 1550. Three Consecutive Odds
127+
* Prod Variants
128+
*/
129+
130+
fun threeConsecutiveOddsProdVariantI(arr: IntArray): Boolean =
131+
arr.toList()
132+
.windowed(3)
133+
.filter {
134+
val one = it[0]
135+
val two = it[1]
136+
val three = it[2]
137+
isOdd(one) && isOdd(two) && isOdd(three)
138+
}.isNotEmpty()
139+
140+
fun threeConsecutiveOddsProdVariantII(arr: IntArray): Boolean =
141+
arr.toList()
142+
.windowed(3)
143+
.any { isOdd(it.first()) && isOdd(it.middle()) && isOdd(it.last()) }
144+
145+
private fun List<Int>.middle(): Int = this[this.size / 2]
146+
147+
fun threeConsecutiveOddsProdVariantIII(arr: IntArray): Boolean =
148+
arr.toList()
149+
.windowed(3)
150+
.any { window -> window.all { it % 2 != 0 } }
151+

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,24 @@ fun numEquivDominoPairs(dominoes: Array<IntArray>): Int {
508508
return count
509509
}
510510

511+
/**
512+
* 2062. Count Vowel Substrings of a String
513+
*/
514+
515+
fun countVowelSubstrings(word: String): Int {
516+
var count = 0
517+
val vowels = setOf('a', 'e', 'i', 'o', 'u')
518+
519+
for (i in word.indices) {
520+
val seen = mutableSetOf<Char>()
521+
for (j in i until word.length) {
522+
val char = word[j]
523+
if (char !in vowels) break
524+
seen.add(char)
525+
if (seen.size == 5) count++
526+
}
527+
}
528+
529+
return count
530+
}
511531

contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ package com.github.contest.hashTable
77
*/
88

99

10-
fun areAlmostEqualProdVariant(s1: String, s2: String): Boolean =
11-
when {
12-
s1 == s2 -> true
13-
s1.length != s2.length -> false
14-
else -> s1.zip(s2).withIndex().filter { it.value.first != it.value.second }.map { it.index }
15-
.let { diff ->
16-
diff.size == 2 && s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]]
17-
}
18-
}
10+
fun areAlmostEqualProdVariant(s1: String, s2: String): Boolean = when {
11+
s1 == s2 -> true
12+
s1.length != s2.length -> false
13+
else -> s1.zip(s2).withIndex().filter { it.value.first != it.value.second }.map { it.index }
14+
.let { diff ->
15+
diff.size == 2 && s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]]
16+
}
17+
}
1918

2019
/**
2120
* 2965. Find Missing and Repeated Values
@@ -114,12 +113,44 @@ fun intToRomanProdVariant(num: Int): String {
114113
* 1128. Number of Equivalent Domino Pairs
115114
*/
116115

117-
fun numEquivDominoPairsProdVariant(dominoes: Array<IntArray>): Int = dominoes
118-
.map { (a, b) -> if (a <= b) a to b else b to a }
119-
.groupingBy { it }
120-
.eachCount()
121-
.values
122-
.sumOf { count -> count * (count - 1) / 2 }
116+
fun numEquivDominoPairsProdVariant(dominoes: Array<IntArray>): Int =
117+
dominoes.map { (a, b) -> if (a <= b) a to b else b to a }
118+
.groupingBy { it }
119+
.eachCount()
120+
.values
121+
.sumOf { count -> count * (count - 1) / 2 }
122+
123+
/**
124+
* 2062. Count Vowel Substrings of a String
125+
* Prod Variant
126+
*/
127+
128+
fun countVowelSubstringProdVariant(word: String): Int = word.indices.sumOf { i ->
129+
val seen = mutableSetOf<Char>()
130+
word.substring(i).takeWhile { it in "aeiou" }.count {
131+
seen.add(it)
132+
seen.size == 5
133+
}
134+
}
135+
136+
137+
/**
138+
* 2062. Count Vowel Substrings of a String
139+
* Prod Variant III
140+
*/
141+
142+
fun countVowelSubstringsProdVariantIII(word: String) = sequence {
143+
word.forEachIndexed { i, _ ->
144+
val seen = mutableSetOf<Char>()
145+
word.asSequence().drop(i)
146+
.takeWhile { it in "aeiou" }
147+
.forEach { c ->
148+
seen += c
149+
if (seen.size == 5) yield(1)
150+
}
151+
}
152+
}.sum()
153+
123154

124155

125156

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.contest.slidingWindow
22

3+
34
/**
45
* 76. Minimum Window Substring
56
*/
@@ -237,3 +238,66 @@ private fun atMostK(nums: IntArray, k: Int): Int {
237238

238239
return count
239240
}
241+
242+
/**
243+
* 2062. Count Vowel Substrings of a String
244+
*/
245+
246+
fun countVowelSubstrings(word: String): Int {
247+
if (word.length < 5) return 0
248+
249+
var count = 0
250+
val k = 5
251+
val vowels = "aeiou"
252+
253+
for (window in k..word.length) {
254+
var left = 0
255+
val freq = mutableMapOf<Char, Int>()
256+
257+
for (right in 0 until word.length) {
258+
freq[word[right]] = freq.getOrDefault(word[right], 0) + 1
259+
260+
if (right - left == window - 1) {
261+
var isValid = true
262+
263+
for ((key, _) in freq) {
264+
if (!isVowel(key)) {
265+
isValid = false
266+
break
267+
}
268+
}
269+
270+
for (vowel in vowels) {
271+
if (!freq.contains(vowel)) {
272+
isValid = false
273+
break
274+
}
275+
}
276+
277+
if (isValid) count++
278+
279+
freq[word[left]] = freq.getOrDefault(word[left], 0) - 1
280+
if (freq[word[left]] == 0) freq.remove(word[left])
281+
left++
282+
}
283+
}
284+
}
285+
286+
return count
287+
}
288+
289+
private fun isVowel(char: Char) = when {
290+
char in "aeiou" -> true
291+
else -> false
292+
}
293+
294+
/**
295+
* 3364. Minimum Positive Sum Subarray
296+
*/
297+
298+
fun minimumSumSubarray(nums: List<Int>, l: Int, r: Int): Int {
299+
val minSums = mutableListOf<Int>()
300+
return TODO("Make this method")
301+
}
302+
303+

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,13 @@ fun countSubArraysProdVariant(nums: IntArray): Int =
8686
(it.first() + it.last()).toDouble() == it[1].toDouble() / 2.0
8787
}
8888

89+
/**
90+
* 3364. Minimum Positive Sum Subarray
91+
* Prod Variant
92+
*/
93+
94+
fun minimumSumSubarrayProdVariant(nums: List<Int>, l: Int, r: Int): Int = buildList {
95+
(l..r).forEach { window ->
96+
nums.windowed(window).map { it.sum() }.filter { it > 0 }.forEach { add(it) }
97+
}
98+
}.minOrNull() ?: -1

0 commit comments

Comments
 (0)