Skip to content

Latest commit

 

History

History
37 lines (29 loc) · 1.04 KB

leetcode_2279.md

File metadata and controls

37 lines (29 loc) · 1.04 KB

LeetCode Problems

2279. Maximum Bags With Full Capacity of Rocks

class Solution {
    fun maximumBags(capacity: IntArray, rocks: IntArray, additionalRocks: Int): Int {
        // check rest spaces of each bags
        val spaces = IntArray(capacity.size, { i -> capacity[i] - rocks[i] })
        // tracking rest rocks and counts of bag with full capacity.
        var restRocks = additionalRocks
        var count = 0
        // to get maximum bags, fill bags from small spaces to upper.
        spaces.sort()
        // iterate
        for (i in 0..spaces.size-1) {
            // if current bag is possible to be full, add count
            // else exit
            if (spaces[i] <= restRocks) {
                restRocks -= spaces[i]
                count ++
            } else break
        }
        return count
    }
}