Advertisement
niedzwiedzw

Untitled

Dec 13th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Hey Haroen,
  2.  
  3. thank you for the interview again, it's been a real pleasure.
  4.  
  5. One thing I would like to add is there is a very simple optimization to the "package in a box" problem that I just thought of. It's still not a complete solution, but it should yield pretty good solution for uniformly distributed data:
  6.  
  7. you could instead of collecting into a Vec<Package>, use a sorted VecDeque<Package>, and then alternate between popping elements from back and front. This should result in a much smaller amount of reduntant Boxes.
  8.  
  9. even better solution:
  10.  
  11. each insert into the box should iterate over sorted Vec<Package>, this time biggest to smallest and pick the biggest one that can fit. In order to not iterate over all elements each time, you could store the index of the last "popped/removed" entry and only go down starting from that index (as the remaining Box capacity will only go down, so there is no point in checking Packages that's already been checked).
  12.  
  13. Then, another optimization would be to not use Vec<Package>, but rather a LinkedList, so that the "remove" operation doesn't reallocate the entire container.
  14.  
  15. I swear I didn't google this, I'm just not very good at thinking fast under pressure, it just occurred to me when I was making tea :D.
  16.  
  17. cheers,
  18.  
  19. Wojciech
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement