Optimal Solution (found using internet)

Couldn't think of a faster way, so I did some googling... should prob.
discount this commit.