kt: add «2275. Largest Combination With Bitwise AND Greater Than Zero»

URL:	https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-07 22:11:06 +01:00
parent f4b32c3129
commit f29fb46aa0
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,22 @@
class Solution {
private val maxBits = 24
private data class State(val counters: IntArray) {
fun update(num: Int): State {
counters.indices.fold(num) { num, i ->
if (num % 2 != 0) {
counters[i] += 1
}
num.shr(1)
}
return this
}
}
fun largestCombination(candidates: IntArray): Int =
candidates.fold(State(IntArray(maxBits))) { acc, it ->
acc.update(it)
}.counters.max()
}