Organizing Containers of Balls

Sort by

recency

|

616 Discussions

|

  • + 0 comments

    This problem sucks. It has no relevance in the really real world and is so poorly stated it is more of a 'can i parse this idiots question' versus solve some algorithm. Damn I hate this site

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-organizing-containers-of-balls-problem-solution.html

  • + 0 comments

    c# code, O(nlogn)

        public static string organizingContainers(List<List<int>> container)
        {
            int n = container.Count;
            List<long> containerCapacity = new List<long>();
            List<long> ballByTypeCount = new List<long>();
            for (int i = 0; i < n; i++) {
                containerCapacity.Add(0);
                ballByTypeCount.Add(0);
            }
            for (int i = 0; i < n; i++){
                for (int j = 0; j < n; j++){
                    containerCapacity[i] += container[i][j];
                    ballByTypeCount[j] += container[i][j];                
                }
            }
            containerCapacity.Sort();
            ballByTypeCount.Sort();
            for (int i = 0; i < n; i++){
                if (containerCapacity[i] != ballByTypeCount[i])
                    return "Impossible";
            }
            return "Possible";
        }
    
  • + 0 comments
    def organizingContainers(container):
        n = len(container)
        size_of_types = [0] * n
        size_of_containers = [0] * n
    
        for i in range(n):
            size_of_current_container = 0
            for j in range(n):
                size_of_types[j] += container[i][j]
                size_of_current_container += container[i][j]
            
            size_of_containers[i] += size_of_current_container
    
        return "Possible" if sorted(size_of_types) == sorted(size_of_containers) else "Impossible"
    
  • + 1 comment

    Here is my C# solution.

    In short, we calculate sizes of containers and count of balls of each type. And then, for each type of balls we try to find a container of appropriate size. If we can do this we succeed, otherwise we fail.

    static bool OrganizingContainersCore(List<List<int>> containers) {
        // containers: size of container -> count of that size
        Dictionary<int, int> containerInfo = new();
    
        // balls
        int[] balls = new int[containers.Count];
    
        foreach(var container in containers) {
            int containerSz = 0;
    
            for(int j = 0; j < container.Count; j++) {
                int num = container[j];
                balls[j] += num;
                containerSz += num;
            }
            containerInfo.TryGetValue(containerSz, out int count);
            containerInfo[containerSz] = count + 1;
        }
    
        // compare
        foreach(int ballCount in balls) {
            if(!containerInfo.TryGetValue(ballCount, out int b))
                return false;
    
            if(b > 1)
                containerInfo[ballCount] = b - 1;
            else
                containerInfo.Remove(ballCount);
        }
        return containerInfo.Count == 0;
    }