Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created October 7, 2021 21:50
Show Gist options
  • Save misterpoloy/9a96b9439c36fb5d2656b74dc4e7b17b to your computer and use it in GitHub Desktop.
Save misterpoloy/9a96b9439c36fb5d2656b74dc4e7b17b to your computer and use it in GitHub Desktop.
Class photos Greedy Method
function classPhotos(redShirtHeights, blueShirtHeights) {
// Sort by height the students Log(n)
redShirtHeights.sort((a, b) => a - b)
blueShirtHeights.sort((a, b) => a - b)
// Pick up who's going to be the backrow
const backColor = (redShirtHeights[0] > blueShirtHeights[0]) ? "RED" : "BLUE"
// compare every classmate and validate it's possible
for (let i = 0; i < redShirtHeights.length; i++) {
// validate for red in the back
if (backColor == "RED") {
if (blueShirtHeights[i] >= redShirtHeights[i]) {
return false
}
} else {
// validate in blue in the back
if (redShirtHeights[i] >= blueShirtHeights[i]) {
return false
}
}
}
return true;
}
// Do not edit the line below.
exports.classPhotos = classPhotos;
@misterpoloy
Copy link
Author

screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment