Last active
March 9, 2017 00:22
-
-
Save nitely/1daf65bb82c9207ce8a6d5c38cda6bec to your computer and use it in GitHub Desktop.
Lame JS puzzle #1
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
/* | |
We have an array of objects A and an array of indexes B. Reorder objects in array A with given indexes in array B. Do not change array A's length. | |
example: | |
var A = [C, D, E, F, G]; | |
var B = [3, 0, 4, 1, 2]; | |
sort(A, B); | |
// A is now [D, F, G, C, E]; | |
*/ | |
// TC O(n) | |
// SC O(n) | |
function sort(a, b) { | |
for (let [i, v] of [...a].entries()) | |
a[b[i]] = v; | |
} | |
let A = ['C', 'D', 'E', 'F', 'G']; | |
let B = [3,0,4,1,2]; | |
sort(A, B); | |
console.log(A); | |
// A is now [D, F, G, C, E]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment