### 技术控

今日:341| 主题:58035

# [其他] Elevator Stops Problem + Solution

131 2
 Recently I went to a technical interview in a big tech company for the front-end engineering role. Regardless of how I feel about their process and how unrelated the test was to the job, I found one of the questions interesting and thought it would be nice if I post my solution here.   Here’s the problem:       There is an elevator in a building with      Mfloors. This elevator can take a max of      Xpeople at a time or max of total weight      Y. Given that a set of people and their weight and the floor they need to stop, how many stops has the elevator taken to serve all the people? Consider the elevator serves in “first come first serve” basis and the order for the queue can not be changed.        Example:       Let Array        Abe the weight of each person        A = [60, 80, 40].             Let Array        Bbe the floors where each person needs to be dropped off        B = [2, 3, 5].               The building has 5 floors, maximum of 2 persons are allowed in the elevator at a time with max weight capacity being 200. For this example, the elevator would take total of 5 stops in floors:      2,      3,      G,      5and finally      G.        And here’s my solution: /** * Remove duplicate items from an array * @param {Array} arr * @returns {Array} */functionuniq(arr){returnarr.reduce((prev, curr) =>{if(prev.indexOf(curr) ===-1) { prev = prev.concat(curr); }returnprev; }, []);}/** * Solution to our problem * * @param {Array.} A Array of passengers weights * @param {Array.} B Array of passenger destination floors * @param {Number} M Number of floors in the building * @param {Number} X Elevator max passenger capacity * @param {Number} Y Elevator max weight capacity * @returns {Number} Number of total stops */functionsolution(A, B, M, X, Y){lettrip =0, tripWeight = 0, rounds = [];for(leti =0, len = A.length; i < len; i +=1) {// If there's an unclosed trip, let’s see if we can get more people inif(typeofrounds[trip] !=='undefined') {// Check if we have filled the capacity for the current trip,// if so, then close the existing trip and create a new oneif(rounds[trip].length === X || tripWeight + A[i] > Y) {// Increase trip count trip++;// Reset current weight tripWeight = 0; } }// Create an empty array for the current trip rounds[trip] = rounds[trip] || [];// Push passengers destination to current trip rounds[trip].push(B[i]); // Increase current load tripWeight += A[i]; }// Remove duplicate floors from each trip, since// the elevator will make 1 stop for pessengers that// go to the same floor. Then add 1 (return to ground floor) rounds = rounds.map(round=>uniq(round).length +1);// To get number of total stops, we sum up// destination count in each trip.returnrounds.reduce((prev, curr) =>prev + curr,0);}console.log( solution( [60,80,40],// Weights [2,3,5],// Destinations5,// Floors2,// Max passengers200// Max weight )); // -> 5复制代码 After I successfully submitted the solution, I searched Google to see if this question has been asked before, turns out it’s a common problem that is asked in programming tests and many people have solved it (    for example this one).     I hope this solution comes in handy for anybody who’s willing to cheat a programming test. Hope you don’t though :-)

 坐沙发喽，楼主给赏钱不？

lxp326 投递于 2016-10-6 13:12:43
 帮顶，帮顶，快速顶贴中・・・・・・

• ## çťä˝ äťŹä¸ä¸ćŹĄć

ĺžŽäżĄĺˇ [...]

• ## 女明星们夏天都在做什么美甲？

微信号 meijialove_ [...]

• ## 这个季节牛仔裤应该配什么衣服！

微信号 dp9978 阅读 [...]

• ## 被顶级化妆师摸过的女人，都变成了女神

微信号 diany233 关 [...]

• ## 夏天这么做美甲，包你美翻天！

微信号 NailBeauty [...]

• ## Oculus Rift每周在Oculus Home平台的更新内

端午假期的到来，小伙伴有何计划，有没有吃粽子 [...]

• ## ä¸äťśTć¤ďźćĺŽĺ

ĺžŽäżĄĺˇ [...]

• ## 传谷歌联合创始人欲打造世界最大飞艇，来细

【猎云网（微信号：）】5月28日报道（文/grace33 [...]

• ## 天使投资人孙江涛：创业15年，我的成功与反

题图：天使投资人，钱袋宝与神州付创始人 孙 [...]