Smallest Number Following Pattern Leetcode Problems Sqlite Python