Skip to content

Commit 814569f

Browse files
Merge pull request #212
add new part of problems
2 parents 22584b1 + c9eb5f2 commit 814569f

File tree

8 files changed

+202
-44
lines changed

8 files changed

+202
-44
lines changed

app/src/main/java/com/leetcode_kotlin/Executing.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.leetcode_kotlin
22

3-
import java.util.concurrent.ConcurrentHashMap
4-
53

64
/**
75
* Executing
@@ -10,10 +8,6 @@ import java.util.concurrent.ConcurrentHashMap
108

119
fun main() {
1210

13-
val concurrentHashMap = ConcurrentHashMap<Int, Int>()
14-
15-
concurrentHashMap.put(1, 5)
16-
1711

1812
}
1913

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

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

33

4+
import com.github.contest.bitManipulation.hammingWeight
45
import com.github.contest.math.numberOfPowerfulInt
56
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
67
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
78
import com.github.contest.strings.fullJustify
8-
import com.github.contest.strings.subStrHash
9+
910
import java.util.TreeMap
1011

1112

@@ -15,10 +16,11 @@ import java.util.TreeMap
1516

1617
fun main() {
1718

18-
val list = mutableListOf<Int>(-1)
19-
println(list)
19+
val str: String? = "ghdirfghdi"
20+
2021
}
2122

23+
2224
infix fun Int.myRange(to: Int): IntRange {
2325
return this..to
2426
}
@@ -96,9 +98,6 @@ fun fullJustifyData() {
9698
}
9799
}
98100

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

103102
fun numberOfPowerfulIntData() {
104103
val start = 141L

contest/src/main/java/com/github/contest/bitManipulation/BitManipulationLeetcode.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,36 @@ fun subsetXORSum(nums: IntArray): Int {
2222

2323
calculateSubsetXOR(0, 0)
2424
return totalXORSum
25+
}
26+
27+
/**
28+
* 191. Number of 1 Bits
29+
*/
30+
31+
fun hammingWeight(n: Int): Int {
32+
var num = n
33+
var count = 0
34+
35+
while (num != 0) {
36+
count += num and 1
37+
num = num ushr 1
38+
}
39+
40+
return count
41+
}
42+
43+
/**
44+
* 461. Hamming Distance
45+
*/
46+
47+
fun hammingDistance(x: Int, y: Int): Int {
48+
var res = x xor y
49+
var count = 0
50+
51+
while (res != 0) {
52+
count += res and 1
53+
res = res ushr 1
54+
}
55+
56+
return count
2557
}

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class ProductOfNumbers() {
4040
class WordDictionary() {
4141

4242
data class TrieNode(
43-
val children: MutableMap<Char, TrieNode> = mutableMapOf(),
44-
var isEndOfWord: Boolean = false
43+
val children: MutableMap<Char, TrieNode> = mutableMapOf(), var isEndOfWord: Boolean = false
4544
)
4645

4746
private val root = TrieNode()
@@ -226,11 +225,7 @@ class CombinationIterator(characters: String, combinationLength: Int) {
226225
fun hasNext(): Boolean = store.isNotEmpty()
227226

228227
private fun combine(
229-
index: Int,
230-
str: String,
231-
subset: StringBuilder,
232-
store: MutableList<String>,
233-
limit: Int
228+
index: Int, str: String, subset: StringBuilder, store: MutableList<String>, limit: Int
234229
) {
235230

236231
if (subset.length == limit) {
@@ -370,4 +365,42 @@ class StockSpanner() {
370365
return span
371366
}
372367

368+
}
369+
370+
/**
371+
* 1865. Finding Pairs With a Certain Sum
372+
*/
373+
374+
class FindSumPairs(private val nums1: IntArray, private val nums2: IntArray) {
375+
376+
private val second = mutableListOf<Long>()
377+
private val freq = mutableMapOf<Long, Int>()
378+
379+
init {
380+
for (elem in nums2) {
381+
val e = elem.toLong()
382+
second.add(e)
383+
freq[e] = freq.getOrDefault(e, 0) + 1
384+
}
385+
}
386+
387+
fun add(index: Int, `val`: Int) {
388+
val old = second[index]
389+
second[index] = second[index] + `val`
390+
val new = second[index]
391+
freq[old] = freq.getOrDefault(old, 0) - 1
392+
if (freq[old] == 0) freq.remove(old)
393+
freq[new] = freq.getOrDefault(new, 0) + 1
394+
}
395+
396+
fun count(tot: Int): Int {
397+
var c = 0
398+
for (elem in nums1) {
399+
val target = tot - elem
400+
if (freq.contains(target.toLong())) c += freq.getOrDefault(target.toLong(), 0)
401+
}
402+
403+
return c
404+
}
405+
373406
}

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: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.contest.strings
22

3-
import java.math.BigInteger
4-
53
/**
64
* 848. Shifting Letters
75
*/
@@ -390,38 +388,87 @@ fun lengthOfLastWord(s: String): Int {
390388

391389

392390
/**
393-
*
391+
* 2138. Divide a String Into Groups of Size k
394392
*/
395393

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

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++
404+
for (char in this) {
405+
if (count != 0) {
406+
temp += char
407+
count--
408+
} else {
409+
res.add(temp)
410+
temp = ""
411+
temp += char
412+
count = delimeterSize - 1
407413
}
408414
}
409415

410-
return ""
416+
res.add(temp)
417+
val arr = res.toTypedArray()
418+
remainingFill(arr, fill, delimeterSize)
419+
420+
return arr
411421
}
412422

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+
private fun remainingFill(strs: Array<String>, pattern: Char, k: Int) {
424+
var last = strs[strs.size - 1]
425+
var count = 0
426+
427+
if (last.length == k) return
428+
else {
429+
count = last.length
430+
while (count != k) {
431+
last += pattern
432+
count++
433+
}
434+
strs[strs.size - 1] = last
423435
}
436+
}
424437

425-
return res % modulo.toBigInteger()
438+
/**
439+
* 3136. Valid Word
440+
*/
441+
442+
fun isValid(word: String): Boolean {
443+
if (word.length < 3) return false
444+
445+
var vowels = 0
446+
var consonants = 0
447+
448+
for (char in word) {
449+
if (isNotDigit(char) && isNotLetter(char)) return false
450+
if (isLetter(char)) {
451+
if (isVowel(char)) vowels++
452+
else consonants++
453+
}
454+
}
455+
456+
return !(vowels == 0 || consonants == 0)
457+
}
458+
459+
private fun isNotDigit(char: Char) = when {
460+
char in '0'..'9' -> false
461+
else -> true
462+
}
463+
464+
private fun isNotLetter(char: Char) = when {
465+
char in 'a'..'z' || char in 'A'..'Z' -> false
466+
else -> true
426467
}
427468

469+
private fun isVowel(char: Char) = when {
470+
char in "aeiou" || char in "AEIOU" -> true
471+
else -> false
472+
}
473+
474+

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+
}

contest/src/main/java/com/github/contest/twoPointer/TwoPointerLeetCode.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,22 @@ fun trap(height: IntArray): Int {
4848
}
4949

5050
return water
51+
}
52+
53+
/**
54+
* 2200. Find All K-Distant Indices in an Array
55+
*/
56+
57+
fun findKDistantIndices(nums: IntArray, key: Int, k: Int): List<Int> {
58+
val result = mutableSetOf<Int>()
59+
for (j in nums.indices) {
60+
if (nums[j] == key) {
61+
val start = maxOf(0, j - k)
62+
val end = minOf(nums.size - 1, j + k)
63+
for (i in start..end) {
64+
result.add(i)
65+
}
66+
}
67+
}
68+
return result.sorted()
5169
}

0 commit comments

Comments
 (0)