LeetCode in Kotlin

3410. Maximize Subarray Sum After Removing All Occurrences of One Element

Hard

You are given an integer array nums.

You can do the following operation on the array at most once:

Return the maximum subarray sum across all possible resulting arrays.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [-3,2,-2,-1,3,-2,3]

Output: 7

Explanation:

We can have the following arrays after at most one operation:

The output is max(4, 4, 7, 4, 2) = 7.

Example 2:

Input: nums = [1,2,3,4]

Output: 10

Explanation:

It is optimal to not perform any operations.

Constraints:

Solution

import kotlin.math.max
import kotlin.math.min

class Solution {
    fun maxSubarraySum(nums: IntArray): Long {
        val prefixMap: MutableMap<Long, Long> = HashMap<Long, Long>()
        var result = nums[0].toLong()
        var prefixSum: Long = 0
        var minPrefix: Long = 0
        prefixMap.put(0L, 0L)
        for (num in nums) {
            prefixSum += num.toLong()
            result = max(result, (prefixSum - minPrefix))
            if (num < 0) {
                if (prefixMap.containsKey(num.toLong())) {
                    prefixMap.put(
                        num.toLong(),
                        min(prefixMap[num.toLong()]!!, prefixMap[0L]!!) + num,
                    )
                } else {
                    prefixMap.put(num.toLong(), prefixMap[0L]!! + num)
                }
                minPrefix = min(minPrefix, prefixMap[num.toLong()]!!)
            }
            prefixMap.put(0L, min(prefixMap[0L]!!, prefixSum))
            minPrefix = min(minPrefix, prefixMap[0L]!!)
        }
        return result
    }
}