Given a list nums and a positive integer k, split the list into consecutive groups of k elements and return them as a list of lists. The final group may contain fewer than k elements if the length is not a multiple of k.
Examples
Input: nums = [1, 2, 3, 4, 5], k = 2
Output: [[1, 2], [3, 4], [5]]
Input: nums = [], k = 3
Output: []
Constraints
0 <= len(nums) <= 10**51 <= k <= 10**5- Return new lists; do not modify
nums. One test callsnums_after(nums, k), which runs yourchunk, appends an item to the first group you returned and then returnsnumsitself, so it must come back unchanged. - Target: O(n) time.
Goals
- Step through a list in strides of k
- Produce a shorter final group instead of padding
- Return an empty list of groups for empty input