diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index e5e65b3b..3990e68e 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,9 +1,7 @@ package com.github.contest -import com.github.contest.hashTable.countGoodAlternativeSolution -import com.github.contest.hashTable.numEquivDominoPairsProdVariant -import com.github.contest.hashTable.testing +import com.github.contest.array.minSum import com.github.contest.math.numberOfPowerfulInt import com.github.contest.strings.fullJustify import com.github.contest.strings.subStrHash @@ -16,15 +14,15 @@ import java.util.TreeMap fun main() { - numEquivDominoPairsProdVariant( - arrayOf( - intArrayOf(1, 2), intArrayOf(2, 1), intArrayOf(3, 4), intArrayOf(5, 6) - ) + minSum( + intArrayOf(0, 0, 10, 10, 12, 0, 13, 6, 0, 2, 10), + intArrayOf(24, 5, 12, 22) ).also { println(it) } } fun countGoodData() { - countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) } + + } fun fullJustifyData() { diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt index 7a62b585..24c100b4 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt @@ -295,3 +295,55 @@ fun merge(intervals: Array): Array { return res.toTypedArray() } +/** + * 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros + */ + +fun minSum(nums1: IntArray, nums2: IntArray): Long { + val zerosOne = nums1.countLong { it == 0 } + val zerosTwo = nums2.countLong { it == 0 } + val sumOne = nums1.sumLong() + zerosOne + val sumTwo = nums2.sumLong() + zerosTwo + + return when { + sumOne == sumTwo -> sumOne + sumOne < sumTwo -> if (zerosOne > 0) sumTwo else -1L + else -> if (zerosTwo > 0) sumOne else -1L + } +} + +private fun IntArray.countLong(predicate: (Int) -> Boolean): Long { + var count = 0L + for (element in this) { + if (predicate(element)) count++ + } + return count +} + +private fun IntArray.sumLong(): Long { + var sum = 0L + for (element in this) sum += element + return sum +} + +/** + * 1550. Three Consecutive Odds + */ + +fun threeConsecutiveOdds(arr: IntArray): Boolean { + var consecutive = 3 + + for (num in arr) { + if (isOdd(num)) consecutive-- + else consecutive = 3 + + if (consecutive == 0) return true + } + + return false +} + +fun isOdd(number: Int) = when { + number % 2 != 0 -> true + else -> false +} diff --git a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt index 7ee7363e..bba539c4 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayProdVariant.kt @@ -120,4 +120,32 @@ fun mergeProdVariantII(intervals: Array): Array = it[1] = maxOf(it[1], interval[1]) } ?: merged.add(interval) merged - }.toTypedArray() \ No newline at end of file + }.toTypedArray() + +/** + * 1550. Three Consecutive Odds + * Prod Variants + */ + +fun threeConsecutiveOddsProdVariantI(arr: IntArray): Boolean = + arr.toList() + .windowed(3) + .filter { + val one = it[0] + val two = it[1] + val three = it[2] + isOdd(one) && isOdd(two) && isOdd(three) + }.isNotEmpty() + +fun threeConsecutiveOddsProdVariantII(arr: IntArray): Boolean = + arr.toList() + .windowed(3) + .any { isOdd(it.first()) && isOdd(it.middle()) && isOdd(it.last()) } + +private fun List.middle(): Int = this[this.size / 2] + +fun threeConsecutiveOddsProdVariantIII(arr: IntArray): Boolean = + arr.toList() + .windowed(3) + .any { window -> window.all { it % 2 != 0 } } + diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt index d587d756..98e76ed3 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -508,4 +508,24 @@ fun numEquivDominoPairs(dominoes: Array): Int { return count } +/** + * 2062. Count Vowel Substrings of a String + */ + +fun countVowelSubstrings(word: String): Int { + var count = 0 + val vowels = setOf('a', 'e', 'i', 'o', 'u') + + for (i in word.indices) { + val seen = mutableSetOf() + for (j in i until word.length) { + val char = word[j] + if (char !in vowels) break + seen.add(char) + if (seen.size == 5) count++ + } + } + + return count +} diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt index 15fe5742..a80d0961 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt @@ -7,15 +7,14 @@ package com.github.contest.hashTable */ -fun areAlmostEqualProdVariant(s1: String, s2: String): Boolean = - when { - s1 == s2 -> true - s1.length != s2.length -> false - else -> s1.zip(s2).withIndex().filter { it.value.first != it.value.second }.map { it.index } - .let { diff -> - diff.size == 2 && s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]] - } - } +fun areAlmostEqualProdVariant(s1: String, s2: String): Boolean = when { + s1 == s2 -> true + s1.length != s2.length -> false + else -> s1.zip(s2).withIndex().filter { it.value.first != it.value.second }.map { it.index } + .let { diff -> + diff.size == 2 && s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]] + } +} /** * 2965. Find Missing and Repeated Values @@ -114,12 +113,44 @@ fun intToRomanProdVariant(num: Int): String { * 1128. Number of Equivalent Domino Pairs */ -fun numEquivDominoPairsProdVariant(dominoes: Array): Int = dominoes - .map { (a, b) -> if (a <= b) a to b else b to a } - .groupingBy { it } - .eachCount() - .values - .sumOf { count -> count * (count - 1) / 2 } +fun numEquivDominoPairsProdVariant(dominoes: Array): Int = + dominoes.map { (a, b) -> if (a <= b) a to b else b to a } + .groupingBy { it } + .eachCount() + .values + .sumOf { count -> count * (count - 1) / 2 } + +/** + * 2062. Count Vowel Substrings of a String + * Prod Variant + */ + +fun countVowelSubstringProdVariant(word: String): Int = word.indices.sumOf { i -> + val seen = mutableSetOf() + word.substring(i).takeWhile { it in "aeiou" }.count { + seen.add(it) + seen.size == 5 + } +} + + +/** + * 2062. Count Vowel Substrings of a String + * Prod Variant III + */ + +fun countVowelSubstringsProdVariantIII(word: String) = sequence { + word.forEachIndexed { i, _ -> + val seen = mutableSetOf() + word.asSequence().drop(i) + .takeWhile { it in "aeiou" } + .forEach { c -> + seen += c + if (seen.size == 5) yield(1) + } + } +}.sum() + 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 cc970657..eeb68294 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -1,5 +1,6 @@ package com.github.contest.slidingWindow + /** * 76. Minimum Window Substring */ @@ -237,3 +238,66 @@ private fun atMostK(nums: IntArray, k: Int): Int { return count } + +/** + * 2062. Count Vowel Substrings of a String + */ + +fun countVowelSubstrings(word: String): Int { + if (word.length < 5) return 0 + + var count = 0 + val k = 5 + val vowels = "aeiou" + + for (window in k..word.length) { + var left = 0 + val freq = mutableMapOf() + + for (right in 0 until word.length) { + freq[word[right]] = freq.getOrDefault(word[right], 0) + 1 + + if (right - left == window - 1) { + var isValid = true + + for ((key, _) in freq) { + if (!isVowel(key)) { + isValid = false + break + } + } + + for (vowel in vowels) { + if (!freq.contains(vowel)) { + isValid = false + break + } + } + + if (isValid) count++ + + freq[word[left]] = freq.getOrDefault(word[left], 0) - 1 + if (freq[word[left]] == 0) freq.remove(word[left]) + left++ + } + } + } + + return count +} + +private fun isVowel(char: Char) = when { + char in "aeiou" -> true + else -> false +} + +/** + * 3364. Minimum Positive Sum Subarray + */ + +fun minimumSumSubarray(nums: List, l: Int, r: Int): Int { + val minSums = mutableListOf() + return TODO("Make this method") +} + + diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt index 5202631f..b4685c04 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt @@ -86,3 +86,13 @@ fun countSubArraysProdVariant(nums: IntArray): Int = (it.first() + it.last()).toDouble() == it[1].toDouble() / 2.0 } +/** + * 3364. Minimum Positive Sum Subarray + * Prod Variant + */ + +fun minimumSumSubarrayProdVariant(nums: List, l: Int, r: Int): Int = buildList { + (l..r).forEach { window -> + nums.windowed(window).map { it.sum() }.filter { it > 0 }.forEach { add(it) } + } +}.minOrNull() ?: -1