leftover: DP solution & O(N) time complexity solution