[LeetCode]#1380. Lucky Numbers in a Matrix

Fatboy Slim
1 min readMay 10, 2020

--

Environment: Python 3.7

Key technique: zip

Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

Example 1:

Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column

Analysis:

  1. Search minimum element in its row
  2. Search maximum in its column.
  3. Use logic “and” to find lucky number.

Solution:

class Solution:
def luckyNumbers (self, matrix):
min_n = {min(rows) for rows in matrix}
max_n = {max(columns) for columns in zip(*matrix)}

return list(min_n & max_n)

Submissions:

Reference:

http://puremonkey2010.blogspot.com/2015/10/python-python-zip.html

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet