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

g2801_2900.s2861_maximum_number_of_alloys.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g2801_2900.s2861_maximum_number_of_alloys

// #Medium #Array #Binary_Search #2023_12_21_Time_289_ms_(100.00%)_Space_43.8_MB_(100.00%)

import kotlin.math.max

class Solution {
    fun maxNumberOfAlloys(
        n: Int,
        k: Int,
        budget: Int,
        composition: List>,
        stock: List,
        cost: List
    ): Int {
        var ans = 0
        var max = 0
        for (i in 0 until n) {
            max = max(stock[i].toDouble(), max.toDouble()).toInt()
        }
        for (i in 0 until k) {
            var temp = 0
            var low = 0
            var high = max + budget
            var mid: Int
            while (low <= high) {
                mid = low + (high - low) / 2
                if (isPos(i, mid, n, budget, composition, stock, cost)) {
                    low = mid + 1
                    temp = mid
                } else {
                    high = mid - 1
                }
            }
            ans = max(ans.toDouble(), temp.toDouble()).toInt()
        }
        return ans
    }

    private fun isPos(
        idx: Int,
        mid: Int,
        n: Int,
        budget: Int,
        composition: List>,
        stock: List,
        cost: List
    ): Boolean {
        var paiSa = 0L
        for (i in 0 until n) {
            val require = (composition[idx][i].toLong()) * (mid)
            val have = stock[i].toLong()
            val diff = require - have
            if (diff > 0) {
                paiSa += diff * (cost[i].toLong())
            }
            if (budget < paiSa) {
                return false
            }
        }
        return budget >= paiSa
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy