Organizing Containers of Balls

  • + 0 comments

    Solution 1. Determine the size of the boxes 2. Determine the quantity of each type 3. If the quantity of each type corresponds to the size of the box -> return 'Possible' otherwise 'Impossible' Code:

    const boxs_hash: { [key: number]: number } = {};
      const ball_hash: { [key: number]: number } = {};
    
      for (let i = 0; i < container.length; i++) {
        const box = container[i];
        boxs_hash[i] = 0;
        for (let j = 0; j < box.length; j++) {
          const ball = box[j];
          boxs_hash[i] = (boxs_hash[i] || 0) + ball;
          ball_hash[j] = (ball_hash[j] || 0) + ball;
        }
      }
    
      const boxs_arr = Object.values(boxs_hash).sort();
      const ball_arr = Object.values(ball_hash).sort();
    
      return boxs_arr.every((value, i) => value === ball_arr[i])
        ? 'Possible'
        : 'Impossible';