Skip to content

Instantly share code, notes, and snippets.

@mwmcode
Last active March 26, 2021 02:24
Show Gist options
  • Save mwmcode/5d726f488fe4ee12c9e13ed766562ba0 to your computer and use it in GitHub Desktop.
Save mwmcode/5d726f488fe4ee12c9e13ed766562ba0 to your computer and use it in GitHub Desktop.
Simple elevator algorithm (return total stops of elevator)
// A [] queue of ppl and their weights
// B [] floors each person is going to
// M maximum floors of building
// X maximum people on elv
// Y maximum wight on elv
function solution(A, B, M, X, Y) {
// if queue had one person and is going to a reachable floor
if (A.length === 1 && B[0] <= M) {
return 2;
// elif one person but heading to a floor that is out of reach
} else if (A.length === 1 && B[0] > M ) {
return 0;
// if more than one person, then we have a queue [array]
} else if (A.length > 1) {
// Filter out (remove) those who are heading to floor > M
for (i=0; i<B.length; i++){
if (B[i] > M) {
B.splice(i,1);
A.splice(i,1);
}
}
}
// if queue is not empty after filteration
if (A.length > 0) {
// onElv obj representing those who are on-elevator at any given time:
// ttlP {p: how many people on board, d: [where each p is heading -- unique]}
// ttlW: total weight of ppl on board
// ttlS: will hold the amount of stops the elevator will make (will be used later on)
var onElv = {
ttlP: {
p:1,
d:[B[0]]
},
ttlW: A[0],
ttlS: 0
};
// extracting first el of array into the obj above
A.shift(); B.shift();
if (X > 1) { // elevator capacity allows > 1 person
for (i = 0; i < A.length; i++ ) {
// if taking on next person doesn't violate the wieght limit(Y) nor the capacity limit (X)
if (((onElv.ttlW + A[i]) <= Y) &&
(onElv.ttlP.p < X) ) {
// check if this new person's destination floor exists in d[]
var exists = false, p = onElv.ttlP.p;
onElv.ttlP.d.forEach(function(el) {
if (el === B[i]) exists = true;
});
if (!exists) { // if destination floor is new (no one on elevator is heading there)
onElv.ttlP.d.push(B[i]); // add it to d[]
}
onElv.ttlP.p ++; // + person, on-boarding person
onElv.ttlW += A[i]; // + weight, increasing total weight
} else { // Y or X doens't allow it, evelvator should go up, then come back down to take new ones
onElv.ttlP.p = 1; // new number of ppl on elv *starting point*
onElv.ttlS += onElv.ttlP.d.length + 1; // adding amount of destations(stops) of last batch/group of ppl
onElv.ttlP.d = [B[i]]; // new dest.
onElv.ttlW = A[i]; // new total weight
}
}// end for
}
// adding last group's dests + 1 for coming back down
onElv.ttlS += onElv.ttlP.d.length + 1;
return onElv.ttlS; // return result
} else { // if filtered queue/array is 0
return 0;
}
}
// example data below shoul return 6
// elev will take first 3 persons (ttlW: 180, d[3,2])
// elevator will go up stops at 2 (#1), 3 (#2) then comes back down (#3)
// takes last two persons goes up drops them at 2 (#4), 3 (#5) comes back down (#6)
var A = [40,40,100,80,20],
B = [3,3,2,2,3],
M = 7, X = 5, Y = 200;
console.log(solution(A,B,M,X,Y));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment