Skip to content

Commit beae9f0

Browse files
Merge pull request #176
add new problem 16.04
2 parents d0580a0 + 62106d7 commit beae9f0

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

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

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

33

4+
import com.github.contest.hashTable.countGoodAlternativeSolution
45
import com.github.contest.math.numberOfPowerfulInt
56
import com.github.contest.strings.fullJustify
67
import com.github.contest.strings.subStrHash
@@ -13,6 +14,11 @@ import java.util.TreeMap
1314

1415
fun main() {
1516

17+
countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) }
18+
19+
}
20+
21+
fun fullJustifyData() {
1622
fullJustify(
1723
arrayOf(
1824
"Science",
@@ -39,7 +45,6 @@ fun main() {
3945
it
4046
)
4147
}
42-
4348
}
4449

4550
fun subStrHashData() {

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,43 @@ fun countBadPairsAltSolution(nums: IntArray): Long {
4545

4646
class Counter {
4747
var count: Int = 0
48+
}
49+
50+
/**
51+
* 2537. Count the Number of Good Subarrays
52+
* Alternative Solution Optimal
53+
* Hash Map Approach
54+
*/
55+
56+
fun countGoodAlternativeSolution(nums: IntArray, k: Int): Long {
57+
var left = 0
58+
var count = 0L
59+
var totalPairs = 0L
60+
val freq = mutableMapOf<Int, Int>()
61+
62+
for (right in nums.indices) {
63+
64+
val num = nums[right]
65+
val currentFreq = freq.getOrDefault(num, 0)
66+
67+
totalPairs += currentFreq
68+
freq[num] = currentFreq + 1
69+
70+
71+
while (totalPairs >= k) {
72+
val leftNum = nums[left]
73+
74+
totalPairs -= freq[leftNum]!! - 1
75+
freq[leftNum] = freq[leftNum]!! - 1
76+
if (freq[leftNum] == 0) {
77+
freq.remove(leftNum)
78+
}
79+
left++
80+
}
81+
82+
83+
count += left
84+
}
85+
86+
return count
4887
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,38 @@ fun isAnagram(s: String, t: String): Boolean {
331331
}
332332

333333
return true
334+
}
335+
336+
/**
337+
* 2537. Count the Number of Good Subarrays
338+
* TLE Approach
339+
*/
340+
341+
fun countGood(nums: IntArray, k: Int): Long {
342+
if (nums.hasSingle()) return 0L
343+
344+
var count = 0L
345+
var left = 0
346+
347+
while (left < nums.size) {
348+
var right = left + 1
349+
while (right < nums.size) {
350+
var localCounter = 0
351+
for (i in left..right) {
352+
for (j in i + 1..right) {
353+
if (nums[i] == nums[j]) localCounter++
354+
}
355+
}
356+
if (localCounter >= k) count++
357+
right++
358+
}
359+
left++
360+
}
361+
362+
return count
363+
}
364+
365+
private fun IntArray.hasSingle() = when {
366+
this.size == 1 -> true
367+
else -> false
334368
}

0 commit comments

Comments
 (0)