Updated to solution to use single scan of input heights