파비의 매일매일 공부기록

2023.11.23 Today's Challenge 본문

Problem Solving/LeetCode

2023.11.23 Today's Challenge

fabichoi 2023. 11. 23. 23:45

https://leetcode.com/problems/diagonal-traverse-ii/

 

Diagonal Traverse II - LeetCode

Can you solve this real interview question? Diagonal Traverse II - Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.   Example 1: [https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png] I

leetcode.com

class Solution:
    def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]:
        groups = defaultdict(list)
        for row in range(len(nums)-1, -1, -1):
            for col in range(len (nums[row])):
                diagonal = row + col
                groups[diagonal].append(nums[row][col])
        ans, curr = [], 0

        while curr in groups:
            ans.extend(groups[curr])
            curr += 1
        
        return ans
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.11.25 Today's Challenge  (2) 2023.11.25
2023.11.24 Today's Challenge  (1) 2023.11.24
2023.11.22 Today's Challenge  (0) 2023.11.22
2023.11.21 Today's Challenge  (0) 2023.11.21
2023.11.20 Today's Challenge  (1) 2023.11.20
Comments