Leetcode 289: Game of Life

Chase / Eng
ghosted!

Go to company page Chase Eng

ghosted!
Jan 3, 2019 6 Comments

Top voted solution uses bit manipulation: changes cell to 2 (binary: 10) or 3 (binary:11) to represent 0 or 1 in the next state. Then recover the true value by doing bitwise right shift by 1 (>>1).

But why is that necessary?

Why not recover it by %2? (2%2 = 0(dead), and 3%2=1(alive)).

And even simpler, change cell to 'AliveToDead' or 'DeadToAlive', and recover it by using if statements on those 2 strings!

None of them is slower than bitwise manipluation.

What am I missing? Is the top voted solution unnecessarily complex without benefits?

comments

Want to comment? LOG IN or SIGN UP
TOP 6 Comments
  • Google
    Lame.

    Go to company page Google

    Lame.
    Why are you tagging Amazon/Facebook/Google? You think we are your TA from school?
    Jan 3, 2019 4
  • It’s using the next bit as a flag... just some trick. Think of that bit is a simple hashmap with true/false.

    The thing about leetcode is that some best rated solution won’t give you best performance, even they have a better big O.

    Best rated solutions are often concise, short, use less variables, simpler data structures etc. that’s a great way you show your interviewer that you are ACM qualified - where only coding speed matters.

    If you show these kind of tricks, I am pretty sure you can come up “normal” solutions very fast.
    Jan 3, 2019 0