技术控

    今日:3| 主题:49249
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Elevator Stops Problem + Solution

[复制链接]
雾里雾气 发表于 2016-10-3 07:43:04
67 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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:
  1. /**
  2. * Remove duplicate items from an array
  3. * @param {Array} arr
  4. * @returns {Array}
  5. */
  6. functionuniq(arr){
  7. returnarr.reduce((prev, curr) =>{
  8. if(prev.indexOf(curr) ===-1) {
  9. prev = prev.concat(curr);
  10. }
  11. returnprev;
  12. }, []);
  13. }
  14. /**
  15. * Solution to our problem
  16. *
  17. * @param {Array.<Number>} A Array of passengers weights
  18. * @param {Array.<Number>} B Array of passenger destination floors
  19. * @param {Number} M Number of floors in the building
  20. * @param {Number} X Elevator max passenger capacity
  21. * @param {Number} Y Elevator max weight capacity
  22. * @returns {Number} Number of total stops
  23. */
  24. functionsolution(A, B, M, X, Y){
  25. lettrip =0,
  26. tripWeight = 0,
  27. rounds = [];
  28. for(leti =0, len = A.length; i < len; i +=1) {
  29. // If there's an unclosed trip, let’s see if we can get more people in
  30. if(typeofrounds[trip] !=='undefined') {
  31. // Check if we have filled the capacity for the current trip,
  32. // if so, then close the existing trip and create a new one
  33. if(rounds[trip].length === X || tripWeight + A[i] > Y) {
  34. // Increase trip count
  35. trip++;
  36. // Reset current weight
  37. tripWeight = 0;
  38. }
  39. }
  40. // Create an empty array for the current trip
  41. rounds[trip] = rounds[trip] || [];
  42. // Push passengers destination to current trip
  43. rounds[trip].push(B[i]);
  44. // Increase current load
  45. tripWeight += A[i];
  46. }
  47. // Remove duplicate floors from each trip, since
  48. // the elevator will make 1 stop for pessengers that
  49. // go to the same floor. Then add 1 (return to ground floor)
  50. rounds = rounds.map(round=>uniq(round).length +1);
  51. // To get number of total stops, we sum up
  52. // destination count in each trip.
  53. returnrounds.reduce((prev, curr) =>prev + curr,0);
  54. }
  55. console.log(
  56. solution(
  57. [60,80,40],// Weights
  58. [2,3,5],// Destinations
  59. 5,// Floors
  60. 2,// Max passengers
  61. 200// Max weight
  62. )
  63. ); // -> 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 :-)
友荐云推荐




上一篇:A Dramatic Tour through Python’s Data Visualization Landscape (including ggplot
下一篇:Qiscus SDK
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

张晓鹏 发表于 2016-10-5 02:26:33
坐沙发喽,楼主给赏钱不?
回复 支持 反对

使用道具 举报

lxp326 发表于 2016-10-6 13:12:43
帮顶,帮顶,快速顶贴中・・・・・・
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表