bottom up approach - O(n)

ported leetcode.com soln to c