Go on LC and try various approaches compare time-complexities and it will be obvious why you have to use a certain algorithm.
Go on LC and try various approaches compare time-complexities and it will be obvious why you have to use a certain algorithm.