Skip to content

Commit b9137d7

Browse files
add 2537
1 parent 89ab3d1 commit b9137d7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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.countGood
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+
countGood(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/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)