diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index ac3e9cd8..999b6dfb 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,6 +1,7 @@ package com.github.contest +import com.github.contest.hashTable.countGoodAlternativeSolution import com.github.contest.math.numberOfPowerfulInt import com.github.contest.strings.fullJustify import com.github.contest.strings.subStrHash @@ -13,6 +14,11 @@ import java.util.TreeMap fun main() { + countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) } + +} + +fun fullJustifyData() { fullJustify( arrayOf( "Science", @@ -39,7 +45,6 @@ fun main() { it ) } - } fun subStrHashData() { diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt index e720e671..ea6995ae 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt @@ -45,4 +45,43 @@ fun countBadPairsAltSolution(nums: IntArray): Long { class Counter { var count: Int = 0 +} + +/** + * 2537. Count the Number of Good Subarrays + * Alternative Solution Optimal + * Hash Map Approach + */ + +fun countGoodAlternativeSolution(nums: IntArray, k: Int): Long { + var left = 0 + var count = 0L + var totalPairs = 0L + val freq = mutableMapOf() + + for (right in nums.indices) { + + val num = nums[right] + val currentFreq = freq.getOrDefault(num, 0) + + totalPairs += currentFreq + freq[num] = currentFreq + 1 + + + while (totalPairs >= k) { + val leftNum = nums[left] + + totalPairs -= freq[leftNum]!! - 1 + freq[leftNum] = freq[leftNum]!! - 1 + if (freq[leftNum] == 0) { + freq.remove(leftNum) + } + left++ + } + + + count += left + } + + return count } \ No newline at end of file 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 8647bbc5..dcea64a0 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -331,4 +331,38 @@ fun isAnagram(s: String, t: String): Boolean { } return true +} + +/** + * 2537. Count the Number of Good Subarrays + * TLE Approach + */ + +fun countGood(nums: IntArray, k: Int): Long { + if (nums.hasSingle()) return 0L + + var count = 0L + var left = 0 + + while (left < nums.size) { + var right = left + 1 + while (right < nums.size) { + var localCounter = 0 + for (i in left..right) { + for (j in i + 1..right) { + if (nums[i] == nums[j]) localCounter++ + } + } + if (localCounter >= k) count++ + right++ + } + left++ + } + + return count +} + +private fun IntArray.hasSingle() = when { + this.size == 1 -> true + else -> false } \ No newline at end of file