Convert an Array Into a 2D Array With Conditions - Leetcode 2610 - Python