Organizing Containers of Balls

Sort by

recency

|

622 Discussions

|

  • + 0 comments

    here is my solution

    $total = count($container);
    	
    	$capacityArray = array();
    	$indexArray = array();
    
    	for ($i=0; $i < $total; $i++) { 
    		$sum = array_sum($container[$i]);
    		$capacityArray[] = $sum;
    		$indexSum = 0;
    		for ($j=0; $j < $total ; $j++) { 
    			$indexSum+= $container[$j][$i];
    		}
    		$indexArray[] = $indexSum;
    	}
    
    sort($capacityArray);
    sort($indexArray);
    
    $result = '';
    if ($capacityArray == $indexArray) {
    	$result = 'Possible';
    }
    else
    {
    	$result = 'Impossible';
    }
    echo $result;
    
  • + 0 comments

    for the love of god can you get into the habit of clarifying which indexes mean what in the input? every single problem on this site involving a matrix i have to comb through the example input to figure out which axes mean what because nobody bothered to clarify whether container[i][j] is the amount of i-color balls in container j or the amount of j-color balls in container i

  • + 0 comments
    def organizingContainers(container) -> str:
        '''returns Impossible | Possible for whether the balls can be split'''
        # this problem is very easy with two containers
        # the count of balls in one container must be equal to the counts of one type
        
        # this logic can be extended to the fact that for every count of a ball type there needs to be a container that fits it
        # in other words the sum of the columns must be equal to the sum of some other row (and that row cannot already be filled with a different ball type)
        
        # if you are wondering why zip(*container) gives a list of a ball type's count in each container here is the breakdown
        # container = [
        #      [1,4],
        #      [2,3],
        # ]
        # the * operator unpacks the container matrix 
        # zip(*container) = zip([1,4], [2,3])
        #
        # zip then joins equal indices together
        # list(zip(*container)) = [(1,2), (4,3)]
        sums_of_balls = [sum(b) for b in zip(*container)]
        sums_of_containers = [sum(c) for c in container]
        sums_of_balls.sort()
        sums_of_containers.sort()
        return "Possible" if sums_of_balls == sums_of_containers else "Impossible" 
    
  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*; import static java.util.stream.Collectors.toList;

    class Result {

    /*
     * Complete the 'organizingContainers' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts 2D_INTEGER_ARRAY container as parameter.
     */
    
    public static String organizingContainers(List<List<Integer>> container) {
        int n = container.size();
        int[] containerCapacity = new int[n];
        int[] typeCount = new int[n];
    
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                containerCapacity[i] += container.get(i).get(j); // total balls in container i
                typeCount[j] += container.get(i).get(j);         // total balls of type j
            }
        }
    
        Arrays.sort(containerCapacity);
        Arrays.sort(typeCount);
    
        return Arrays.equals(containerCapacity, typeCount) ? "Possible" : "Impossible";
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int q = Integer.parseInt(bufferedReader.readLine().trim());
    
        IntStream.range(0, q).forEach(qItr -> {
            try {
                int n = Integer.parseInt(bufferedReader.readLine().trim());
    
                List<List<Integer>> container = new ArrayList<>();
    
                IntStream.range(0, n).forEach(i -> {
                    try {
                        container.add(
                            Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                                .map(Integer::parseInt)
                                .collect(toList())
                        );
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                });
    
                String result = Result.organizingContainers(container);
    
                try {
                    bufferedWriter.write(result);
                    bufferedWriter.newLine();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    I dislike this problem.

    it should give an example that container[0] doesn't have to contain only the balls labeled by 0 .