Ad
  • Default User Avatar

    Of course, but only after passing them. The idea is to prevent solving the task in a way that only the test cases pass (without solving it generally).

  • Custom User Avatar

    It should be:

    Test.assert_equals(knapsack(100, [[1, 1], [3, 4]]), [100])

    I assume you mean [100, 0] for the quantities (the last brackets)

    Am I misunderstanding the greedy solution?

    Unless you are reading the items as [value, size] (they are [size, value]), you are misunderstanding the greedy solution. The greedy solution will always add the item with the highest value-to-size ratio that can fit in the knapsack.

    The two ratios here are 1.0 for the [1, 1] item and 1.3 for the [3, 4] item. Therefore, the [3, 4] item will be prioritized. 33 of this item can fit in the 100-size knapsack, taking up 99 of the knapsack. That leaves 1 space for a [1, 1] item to fill.

  • Custom User Avatar

    If you go into "Account Settings" and turn on "CODEWARS LABS" you can post a kumite there...

  • Custom User Avatar

    I had the same problem. If I move numbers generation inside a function it times out(system tests only), but outside a function works in 55ms.

  • Custom User Avatar

    Also, as I've said elsewhere, there are other optimizations to consider aside from just getting your time complexity down.

    That being said, you can get away with slower solutions in javascript because it has a proper JIT...

  • Custom User Avatar

    Yeah, can you post it in a kumite?

  • Custom User Avatar

    Fixed it, thank you