[Solved] Which Solution is better in leetCode in terms of Runtime and Memory Usage? [closed]


“Which is better” is always a matter of opinion. In your particular application, is execution time or memory use more of a problem? Then optimize for that aspect.

In terms of leetcode, I understand it’s a sort of quiz, but for numbers that small, my response is “who cares?”. I don’t care about an execution-time difference of 5 mS for an entire program (even though the faster saves ~30% of the runtime of the latter). I don’t care about a memory difference of half a megabyte for an entire program (and here the savings are of the order of 1%).

Now, if that code happens to be part of a large program where we’re scaling up a million-fold, or something like that, then I’d care, possibly a lot. But absent that information, it doesn’t seem to matter. I’d favour optimizing for the time spent writing it — programmers being more expensive than machine time.

If this particular leetcode test has time/space objectives, then it might matter there also. But you have not mentioned any such.

solved Which Solution is better in leetCode in terms of Runtime and Memory Usage? [closed]