Diagonal Traverse II - Complete Solution Guide
Diagonal Traverse II is LeetCode problem 1424, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a 2D integer array nums , return all elements of nums in diagonal order as shown in the below images . Example 1: Input: nums = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,4,2,7,5,3,8,6,9] Example 2: Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]] Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16] Constraints: 1 <= nums.length <= 10 5 1 <= nums[i].length <= 10 5 1 <= sum(nums[i].length) <= 10 5 1 <= nums[i][j] <= 10 5
Detailed Explanation
The problem asks us to traverse a 2D integer array `nums` in a diagonal order and return a 1D array containing all elements in that order. Diagonal order means grouping elements where the sum of their row and column indices is the same. We read the diagonals in increasing order of their sum (r + c), and within each diagonal, we traverse elements from bottom to top (increasing row and decreasing column index). The input is a 2D array (list of lists) of integers, and the output is a 1D array (list) containing all elements from the input in diagonal order.
Solution Approach
The solution utilizes a hash map (or dictionary) to group elements based on the sum of their row and column indices (r + c). The keys of the hash map are the sums (r + c), and the values are lists of elements belonging to that diagonal. The elements for a given sum r+c are appended to the corresponding list. After grouping, the code iterates through the hash map in increasing order of the keys (r + c), reversing the order of elements in each diagonal's list, and then concatenating the lists into a final result list. This reversal ensures that elements are added to the final result from bottom to top along the diagonal.
Step-by-Step Algorithm
- Step 1: Create a hash map (or dictionary) called `diagonals` to store elements grouped by the sum of their row and column indices.
- Step 2: Iterate through the input 2D array `nums` row by row.
- Step 3: For each element `nums[r][c]`, calculate the sum `r + c`.
- Step 4: Append the element `nums[r][c]` to the list associated with the key `r + c` in the `diagonals` hash map. If the key does not exist, create a new list.
- Step 5: Create an empty list called `result` to store the elements in diagonal order.
- Step 6: Initialize `curr_sum` to 0. This variable represents the current diagonal sum we're processing.
- Step 7: While `curr_sum` exists as a key in the `diagonals` hash map:
- Step 8: Get the list of elements associated with `curr_sum` from the `diagonals` hash map.
- Step 9: Reverse the order of elements in the list to maintain the correct order in each diagonal.
- Step 10: Extend the `result` list by appending the reversed list of elements.
- Step 11: Increment `curr_sum` by 1 to move to the next diagonal.
- Step 12: Return the `result` list.
Key Insights
- Insight 1: Elements on the same diagonal have the same sum of row and column indices (r + c).
- Insight 2: We can group elements based on the sum of their row and column indices.
- Insight 3: Within each diagonal (same r+c value), we need to extract elements in reverse order of their occurrence in the input matrix during the row-wise iteration.
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(N)
Topics
This problem involves: Array, Sorting, Heap (Priority Queue).
Companies
Asked at: BP, Liftoff.