Organizing Containers of Balls

Sort by

recency

|

607 Discussions

|

  • + 0 comments

    For the solution I focused in the amount of spaces that each container has and the amount of balls per type that there needs to be. Because at the end of the day what is asked is that each type of ball can be in one container.

    the total capacity of a container is the sum of all the elements in the row

    the total amount of balls per type is the sum of each i'th element in each row.

    Then we would get, two lists, one that represents the amount of spaces that each container has, and the other one the amount of balls per type that we have. If we sort those lists and then check if they are equal, meaning that they have the same values on each position, is possible to swap the items between the containers and meet the requirements.

  • + 1 comment

    I hate how people just paste the code instead of hinting what should be done in these challenges, so lazy...

    The clue here is not to focus on how to move these balls, rather than that we need to determine if such combination of balls and containers is even possible. If you read the intro, there is no mention of how many balls can be stored in the container, but if the only move we have is to switch two balls between containers, that means that the total capacity of the containers do not change. What if we have a lot of balls in single color, but no container can fit them all? Or if we have just 1 ball, but all the containers have capacity of at least two? These are the cases you should focus on.

  • + 0 comments
    public static String organizingContainers(List<List<Integer>> container) {
    // Write your code here
    		List<Integer> colorSum = new ArrayList<>();
    		List<Integer> containerSum = new ArrayList<>(); 
    
    		Collections.sort(container, Comparator.comparing(list -> list.stream().reduce(0, (a, b) -> a + b)));
    
    		for (int i = 0; i < container.size(); i++) { 
    				int sum = 0;  
    				for (int j = 0; j < container.get(i).size(); j++) { 
    						sum += container.get(i).get(j); 
    						if (colorSum.size() == j) {
    								colorSum.add(container.get(i).get(j)); 
    						}
    						else { 
    								colorSum.set(j, colorSum.get(j) + container.get(i).get(j));
    						} 
    				} 
    				containerSum.add(sum);  
    		}  
    
    		Collections.sort(containerSum); 
    		Collections.sort(colorSum);  
    
    		if (!containerSum.equals(colorSum)) {
    				return "Impossible"; 
    		}
    
    		return "Possible"; 
    } 
    
  • + 1 comment

    These sorting are used in CMS especially WordPress . I design a website in WordPress( https://goldrateinfo.ae/سوق-الذهب-الشارقة ) and used it internally in func.php

    PHP Solution

    function sortBalls( $containers ) { 
        $ballTypes = [];
        $swaps = 0;
    
        // Group the balls by type
        foreach ($containers as $container) {
            $ballType = $container[0];
            if (!isset($ballTypes[$ballType])) {
                $ballTypes[$ballType] = [];
            }
            $ballTypes[$ballType] = array_merge($ballTypes[$ballType], $container);
        }
    
        // Assign the balls back to the containers
        $index = 0;
        foreach ($ballTypes as $type => $balls) {
            foreach ($balls as $ball) {
                if ($containers[$index][0] != $ball) {
                    // Swap the ball to its correct container
                    $swaps++;
                    $temp = $containers[$index][0];
                    $containers[$index][0] = $ball;
                    $containers[$index + 1][0] = $temp;
                }
                $index++;
            }
        }
    
        return $swaps;
    }
    
    // Example usage
    $containers = [
        [1, 1, 2, 2, 3, 3],
        [1, 1, 2, 2, 3, 3],
        [1, 2, 3, 1, 2, 3]
    ];
    
    $swaps = sortBalls($containers);
    echo "Minimum number of swaps required: " . $swaps;
    
  • + 1 comment

    My idea consists of verifying if the number of balls of each type is equal to the number of spaces in each box.

    Python Code

    def organizingContainers(container):
        
        total_spaces = sorted(list(map(sum, container)))
        total_balls = sorted(list(map(sum, zip(*container))))
        
        if total_spaces == total_balls:
            return 'Possible'
        else:
            return 'Impossible'