While coding, it pays to code efficiently in one go. It might sound difficult, but not impossible. How? Think through your algorithm multiple times, refine it, dry run it. Question yourself- can it be better, can it be done in a lesser time, using lesser space? Did you pick the best data structure for it, or could you exploit another one better? You have seen some exceptional questions and algorithms before, can you employ the insights from that one here?
"Think Twice, Code once"
Thinking through your algorithm ensures a clear way for you to code wherein you will only code and not second guess your ideas after every other statement. The results are tremendously time saving and the rewards only multiply as you move forward keeping this practice with yourself.