Skip to content

Commit cbbd2e1

Browse files
Merge pull request #209
add new problem sliding window 22.06
2 parents 6f974de + 4086e06 commit cbbd2e1

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.github.contest.math.numberOfPowerfulInt
55
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
66
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
77
import com.github.contest.strings.fullJustify
8-
import com.github.contest.strings.subStrHash
8+
99
import java.util.TreeMap
1010

1111

@@ -15,10 +15,13 @@ import java.util.TreeMap
1515

1616
fun main() {
1717

18-
val list = mutableListOf<Int>(-1)
19-
println(list)
18+
val str = "abcdefrt"
19+
20+
str.chunked(3).also { println(it) }
21+
2022
}
2123

24+
2225
infix fun Int.myRange(to: Int): IntRange {
2326
return this..to
2427
}
@@ -96,9 +99,6 @@ fun fullJustifyData() {
9699
}
97100
}
98101

99-
fun subStrHashData() {
100-
subStrHash("xxterzixjqrghqyeketqeynekvqhc", 15, 94, 4, 16).also { println(it) }
101-
}
102102

103103
fun numberOfPowerfulIntData() {
104104
val start = 141L

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,30 @@ private fun String.eachCount(): MutableMap<Char, Int> {
768768
return count
769769
}
770770

771+
/**
772+
* 713. Subarray Product Less Than K
773+
*/
774+
775+
fun numSubarrayProductLessThanK(nums: IntArray, k: Int): Int = atMostKProduct(nums, k - 1)
776+
777+
private fun atMostKProduct(nums: IntArray, k: Int): Int {
778+
var left = 0
779+
var product = 1
780+
var count = 0
781+
782+
for (right in nums.indices) {
783+
product *= nums[right]
771784

785+
while (left <= right && product > k) {
786+
product /= nums[left]
787+
left++
788+
}
789+
790+
count += (right - left + 1)
791+
}
792+
793+
return count
794+
}
772795

773796

774797

contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -389,39 +389,53 @@ fun lengthOfLastWord(s: String): Int {
389389
}
390390

391391

392+
392393
/**
393-
*
394+
* 2138. Divide a String Into Groups of Size k
394395
*/
395396

396-
fun subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String {
397-
var left = 0
397+
fun divideString(s: String, k: Int, fill: Char): Array<String> {
398+
val res = s.split(k, fill)
399+
return res
400+
}
401+
402+
private fun String.split(delimeterSize: Int, fill: Char): Array<String> {
403+
val res = mutableListOf<String>()
398404
var temp = ""
405+
var count = delimeterSize
399406

400-
for (right in s.indices) {
401-
temp += s[right]
402-
if (right - left == k - 1) {
403-
val hash = hash(temp, power, modulo)
404-
if (hash.intValueExact() == hashValue.toBigInteger().intValueExact()) return temp
405-
temp = temp.substring(1, temp.length)
406-
left++
407+
for (char in this) {
408+
if (count != 0) {
409+
temp += char
410+
count--
411+
} else {
412+
res.add(temp)
413+
temp = ""
414+
temp += char
415+
count = delimeterSize - 1
407416
}
408417
}
409418

410-
return ""
419+
res.add(temp)
420+
val arr = res.toTypedArray()
421+
remainingFill(arr, fill, delimeterSize)
422+
423+
return arr
411424
}
412425

413-
private fun hash(str: String, power: Int, modulo: Int): BigInteger {
414-
var res = 0.toBigInteger()
415-
var pow = 0
416-
val power = power.toBigInteger()
417-
for (element in str) {
418-
val index = (element - 'a' + 1).toBigInteger()
419-
val base = (power.pow(pow))
420-
val calc = index * base
421-
res += calc
422-
pow++
423-
}
426+
private fun remainingFill(strs: Array<String>, pattern: Char, k: Int) {
427+
var last = strs[strs.size - 1]
428+
var count = 0
424429

425-
return res % modulo.toBigInteger()
430+
if (last.length == k) return
431+
else {
432+
count = last.length
433+
while (count != k) {
434+
last += pattern
435+
count++
436+
}
437+
strs[strs.size - 1] = last
438+
}
426439
}
427440

441+

contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ fun smallestStringProdVariant(s: String): String {
6565
* Prod Variant
6666
*/
6767

68-
fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0
68+
fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0
69+
70+
/**
71+
* 2138. Divide a String Into Groups of Size k
72+
* Prod Variant
73+
*/
74+
75+
fun divideStringProdVariant(s: String, k: Int, fill: Char): Array<String> = when {
76+
s.length % k == 0 -> s.chunked(k).toTypedArray()
77+
else -> s.chunked(k).toMutableList().apply {
78+
this[this.lastIndex] = this.last().padEnd(k, fill)
79+
}.toTypedArray()
80+
}

0 commit comments

Comments
 (0)