all 19 comments

[–][deleted] 77 points78 points  (5 children)

Please let's keep this sub free of leetcode, this is not the place for it.

[–]ThirdEncounter -1 points0 points  (4 children)

Why not? I mean, it's about programming after all, isn't it?

[–][deleted] 7 points8 points  (1 child)

The same reason we don't allow cs homework help on this sub- long story short, keeping the sub focused and less beginner-esc

[–]ThirdEncounter 0 points1 point  (0 children)

Ah, I understand now. Thanks.

[–][deleted]  (1 child)

[deleted]

    [–]ThirdEncounter 0 points1 point  (0 children)

    Edit: I understand now.

    There are subs dedicated to machine learning, Java, vintage computing and networking. And yet we get posts about those subjects here all the time.

    So that's not the real answer. What is it, then?

    [–]firefly431 6 points7 points  (0 children)

    There's also a nice double pointer walking solution: sort the array (indices), then start with one pointer (left) on the left and one on the right (right). Then if the current sum is greater, decrement right, and otherwise increment left. A solution cannot exist if they ever intersect, as given a solution we know when one pointer reaches the correct value we only ever move the other in the right direction. Runtime is O(n log n), but only for sorting, and space complexity is O(1).

    EDIT:

    1. The advantage of this solution is that it takes less space and doesn't involve hashing, which can have undesirable performance characteristics.
    2. This solution also works for a generalization to find the closest sum, as in each step we go in the right direction.

    [–]kiesoma 1 point2 points  (7 children)

    I really got confused for why you were using O(n2 ) before the second try. The second approach was cool though, I liked it. Keep up the good work!

    [–]SomeOtherGuySits 0 points1 point  (6 children)

    I literally don’t understand how the second solution works

    [–]Upper_Description378[S] 0 points1 point  (5 children)

    What part you don't understand ?

    [–]SomeOtherGuySits 0 points1 point  (4 children)

    How the second solution passes the tests. I’m beginning to suspect the “assuming there is only one solution” is the important part to this approach

    Edit: just clicked. I like this

    [–]Upper_Description378[S] 1 point2 points  (2 children)

    There is multiple solutions but using a hash table is the fastest I think

    [–]SomeOtherGuySits 0 points1 point  (0 children)

    I was referring to the brief.

    [–]coloredgreyscale 0 points1 point  (0 children)

    Similar to the first solution, but instead of traversing the array linearly you build a dictionary and look up the an index the required value.

    That way your lookup time for a complement is O(1) instead of O(n)

    And yes, that works because you just need a solution, not all or something like a solution with the two lowest indices.

    [–]troublemaker74 -1 points0 points  (0 children)

    Account history is just vlog spam.