g2701_2800.s2708_maximum_strength_of_a_group.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2701_2800.s2708_maximum_strength_of_a_group
// #Medium #Array #Sorting #Greedy #Backtracking
// #2023_07_31_Time_183_ms_(100.00%)_Space_36.9_MB_(90.91%)
class Solution {
fun maxStrength(nums: IntArray): Long {
val filtered = mutableListOf()
var product = 1L
var hasZero = false
for (num in nums) {
if (num == 0) {
hasZero = true
continue
}
filtered.add(num)
product *= num.toLong()
}
if (filtered.isEmpty()) return 0
if (filtered.size == 1 && filtered[0] <= 0) return if (hasZero) 0 else filtered[0].toLong()
var result = product
for (num in nums) {
if (num == 0) continue
result = result.coerceAtLeast(product / num.toLong())
}
return result
}
}