Created
July 14, 2020 22:26
-
-
Save Shaddyjr/299680250c3b332cf06937ea48ee36dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: https://www.hackerrank.com/challenges/organizing-containers-of-balls | |
function organizingContainers(containers) { | |
const n = containers.length; | |
// keep # of balls per container | |
const containerCounts = new Array(n).fill(0); | |
// keep # of types of balls | |
const ballTypes = new Array(n).fill(0); | |
for(let i = 0; i < n; i++){ | |
const container = containers[i]; | |
for(let j = 0; j < n; j++){ | |
const ballCount = container[j]; | |
containerCounts[i] += ballCount; | |
ballTypes[j] += ballCount; | |
} | |
} | |
// sort to make comparable | |
containerCounts.sort(); | |
ballTypes.sort(); | |
for(let i = 0; i<n;i++){ | |
if(containerCounts[i] !== ballTypes[i]) return "Impossible"; | |
} | |
return "Possible"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment