Skip to content

Commit c2326d1

Browse files
add 1865
1 parent aa0de6c commit c2326d1

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

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
}

0 commit comments

Comments
 (0)