All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0101_0200.s0152_maximum_product_subarray.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0101_0200.s0152_maximum_product_subarray

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
// #2022_09_06_Time_253_ms_(88.42%)_Space_42.1_MB_(44.74%)

class Solution {
    fun maxProduct(nums: IntArray): Int {
        var pos = nums[0]
        var neg = nums[0]
        var max = nums[0]
        for (i in 1 until nums.size) {
            val temp = pos
            pos = Math.max(nums[i], nums[i] * if (nums[i] >= 0) pos else neg)
            neg = Math.min(nums[i], nums[i] * if (nums[i] >= 0) neg else temp)
            max = Math.max(max, pos)
        }
        return max
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy