Skip to main content

Command Palette

Search for a command to run...

Apple Redistribution into Boxes

Published
2 min read
Apple Redistribution into Boxes
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an array apple of size n and an array capacity of size m.

There are n packs where the i<sup>th</sup> pack contains apple[i] apples. There are m boxes as well, and the i<sup>th</sup> box has a capacity of capacity[i] apples.

Return the minimum number of boxes you need to select to redistribute these n packs of apples into boxes.

Note that, apples from the same pack can be distributed into different boxes.

LeetCode Problem - 3074

class Solution {
    // This method calculates the minimum number of boxes needed to store all the apples given their counts and the capacity of each box.
    public int minimumBoxes(int[] apple, int[] capacity) {
        // Variable to store the total number of apples
        int totalApple = 0;
        // Calculate the total number of apples
        for (int e : apple) {
            totalApple += e;
        }

        // Sort the capacity array in non-decreasing order
        Arrays.sort(capacity);

        // Variables to track the sum of capacities and the number of boxes used
        int sumCapacity = 0, ans = 1;

        // Iterate through the capacities in reverse order (starting from the largest capacity)
        for (int i = capacity.length - 1; i >= 0; i--) {
            // Add the current capacity to the sum
            sumCapacity += capacity[i];
            // If the total capacity accumulated so far is enough to store all the apples, return the current number of boxes used
            if (sumCapacity >= totalApple)
                return ans;
            // Otherwise, increment the number of boxes used
            else
                ans++;
        }

        // If it's impossible to store all the apples, return -1
        return -1;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.