Beta

Layout ordered sections in columns

18 of 21mweiss
Description
Loading description...
Algorithms
  • Please sign in or sign up to leave a comment.
  • G_kuldeep Avatar

    random tests?

  • OverZealous Avatar

    Nice, this is a fairly challenging kata. I enjoyed it.

    My only concern is that the returned array is a little odd. I think it would make sense to include the initial 0, or return an array of arrays of section lengths. Either seems more natural, but it's not enough to ruin the kata.

    Also, I don't know if my solution is truly a valid one, but it passed all the tests. :-)

    • mweiss Avatar

      I like your solution! It's way more efficient than the brute force one, especially when there's a lot of columns. I'm not sure if there's a test case that breaks it.

      I agree it's more straight forward to include the initial 0. I've modified the kata so that the output is an array of starting indexes including the initial 0.

    • OverZealous Avatar

      Cool, I'll update my solutions... :-D

      An example that breaks my solution would be [5,5,3,3,1,1,2,3,4] at 3 columns. It should render to [[5,5],[3,3,1,1],[2,3,4]] for the optimal case (col heights of [10,8,9]), but ends up becoming [[5,5],[3,3],[1,1,2,3,4]] (heights of [10,6,11]).

      I'm not sure the solution without a major change to how it works. I might be able to add in a loop that looks for any 2 columns more than 2 units apart, and attempt to even them out.

    • mweiss Avatar

      Ah, that's true! I've added that test case. I'm not 100% sure how you'd even them out to be guaranteed the optimal solution, but I look forward to a cool solution.

    • OverZealous Avatar

      Oof, I thought I had a solution, but then ran into a real pickle with this input: [5,5,1,1,1,5,3,2,1,1,2,3,4] at 4 columns, which should result in: [0,2,6,10] for column heights of [10,8,7,9].

      However, because of where the 3rd 5 is, I keep getting stuck at [0,1,5,10], which has heights of [5,8,12,9].

      I'm not sure, at this point, of an easy, non-brute force method for solving it. I'll keep thinking about it.

    • OverZealous Avatar

      I just realized that the correct solution to that would actually be [0,2,6,11], if you assume left-weighted priority. I have a much better solution I'll be posting in a minute. Scratch that, no such luck on the better solution.