Created
October 7, 2021 21:50
-
-
Save misterpoloy/9a96b9439c36fb5d2656b74dc4e7b17b to your computer and use it in GitHub Desktop.
Class photos Greedy Method
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
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; |
Author
misterpoloy
commented
Oct 7, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment