LeetCode之Split Array Largest Sum(Kotlin)

問題: git


方法: 貪心算法加二分查找,正確結果必在(0,sum(nums))中,經過計算mid逐漸逼近到正確結果。github

具體實現:算法

class SplitArrayLargestSum {
    fun splitArray(nums: IntArray, m: Int): Int {
        var left = 0
        var right = nums.sum() + 1
        var ans = Int.MAX_VALUE
        while (left < right) {
            val mid = (left + right) / 2
            if (guess(nums, m, mid)) {
                ans = mid
                right = mid
            } else {
                left = mid + 1
            }
        }
        return ans
    }

    private fun guess(nums: IntArray, m: Int, mid: Int): Boolean {
        var sum = 0
        var innerM = m
        for (num in nums) {
            if (sum + num > mid) {
                innerM--
                sum = num
                if (num > mid) {
                    return false
                }
            } else {
                sum += num
            }
        }
        return innerM >= 1
    }
}

fun main(args: Array<String>) {
    val input = intArrayOf(2, 8)
    val m = 1
    val splitArrayLargestSum = SplitArrayLargestSum()
    println(splitArrayLargestSum.splitArray(input, m))
}
複製代碼

有問題隨時溝通bash

具體代碼實現能夠參考Githubui

相關文章
相關標籤/搜索