Skip to content

Commit 79c65f7

Browse files
Merge pull request #177
add new problem and repeat old 17.04
2 parents beae9f0 + 2fae4f8 commit 79c65f7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,50 @@ fun countGood(nums: IntArray, k: Int): Long {
365365
private fun IntArray.hasSingle() = when {
366366
this.size == 1 -> true
367367
else -> false
368+
}
369+
370+
371+
/**
372+
* 169. Majority Element
373+
*/
374+
375+
fun majorityElement(nums: IntArray): Int {
376+
var majority = 0
377+
var res = 0
378+
val target = nums.size / 2
379+
380+
nums.forEach {
381+
if (majority == 0) res = it
382+
majority += when {
383+
it == res -> 1
384+
else -> -1
385+
}
386+
}
387+
388+
return res
389+
}
390+
391+
/**
392+
* 2176. Count Equal and Divisible Pairs in an Array
393+
*/
394+
395+
fun countPairs(nums: IntArray, k: Int): Int {
396+
if (nums.size < 2) return 0
397+
398+
val store = mutableMapOf<Int, MutableList<Int>>()
399+
var count = 0
400+
401+
for (i in nums.indices) {
402+
val num = nums[i]
403+
if (store.contains(num)) {
404+
val indexed = store.getOrDefault(num, mutableListOf())
405+
for (index in indexed) {
406+
if ((index * i) % k == 0) count++
407+
}
408+
indexed.add(i)
409+
store[num] = indexed
410+
} else store[num] = mutableListOf(i)
411+
}
412+
413+
return count
368414
}

0 commit comments

Comments
 (0)