Advertisement
ErolKZ

Untitled

Sep 29th, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. Trimming Cables
  2. Young Seryozha is the junior IT at the trendiest coworking space (Campus V, V stands for Vladivostok) and he's been assigned with an extremely boring task. He has to trim all the cables hanging from the ceiling at the server room and he just can't find the inner strength to begin.
  3.  
  4. So, to brighten up his day (and waste a little time), he decides to trim those cables by following specific rules: he identifies the shortest cable and then he trims all the cables exactly as much as the shortest's length. This process is repeated until all cables are leveled with the ceiling. Meanwhile, he counts the number of trims performed and at the end of the day, he will log this number into JIRA.
  5.  
  6. ----- ----- ----- -----
  7. | | | | | |
  8. | | |
  9. |
  10. 3 trims 2 trims 1 trims 0 trims (total 6 trims performed)
  11. Input
  12. Exactly one line:
  13.  
  14. the cables (numbers, separated by a space)
  15. Output
  16. Exactly one line:
  17.  
  18. the number of of trims performed
  19. Constraints
  20. 0 < total cables < 2000
  21. 0 < cable length < 500
  22. Sample tests
  23. Input
  24. 3 1 2
  25. Output
  26. 6
  27. Input
  28. 2 6 2
  29. Output
  30. 4
  31. JavaScript
  32.  
  33.  
  34.  
  35.  
  36.  
  37. let input = [
  38.  
  39. '3 1 2'
  40.  
  41. // '2 6 2'
  42.  
  43. ]
  44.  
  45.  
  46.  
  47.  
  48. let print = this.print || console.log;
  49. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  50.  
  51.  
  52. let cables = gets().split(' ').map(Number);
  53.  
  54.  
  55. cables.sort(((a, b) => b - a));
  56.  
  57. cables = [... new Set(cables)];
  58.  
  59.  
  60. let counter = 0;
  61.  
  62. let sum = 0;
  63.  
  64. cables.map(par => sum += par);
  65.  
  66.  
  67.  
  68. print(cables, sum);
  69.  
  70.  
  71.  
  72.  
  73.  
  74. for (let i = 0; i < cables.length; i++) {
  75.  
  76. for (let j = 0; j < cables[i]; j++) {
  77.  
  78. sum = sum - cables[cables.length - 1];
  79.  
  80. if (sum < 0) {
  81.  
  82. break;
  83. }
  84.  
  85. counter++;
  86.  
  87.  
  88.  
  89.  
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. print(counter);
  100.  
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement