|
| 1 | +# Lucky Numbers in a Matrix |
| 2 | + |
| 3 | +Given an m × n matrix of distinct numbers, return the lucky number in the matrix. |
| 4 | + |
| 5 | +> A lucky number is an element of the matrix such that it is the smallest element in its row and largest in its column. |
| 6 | +
|
| 7 | +Constraints |
| 8 | + |
| 9 | +- m = `matrix.length` |
| 10 | +- n = `matrix[i].length` |
| 11 | +- 1 <= m, n <= 50 |
| 12 | +- 1 <= `matrix[i][j]` <= 10^5 |
| 13 | +- All elements in the matrix are distinct. |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +The core idea behind the solution is to recognize that there can be, at most, one lucky number in the matrix. This is |
| 24 | +proven by contradiction, as having two such numbers would violate the unique conditions for being a lucky number. |
| 25 | + |
| 26 | +> **Proof by contradiction:** |
| 27 | +> |
| 28 | +> Suppose we have an integer x located at row r1 and column c1 in a matrix. The integer x is the smallest value in its |
| 29 | +> row and the largest value in its column, making it a lucky number. |
| 30 | +> Now, assume another integer y exists in row r2 and column c2. For the sake of argument, let’s assume y is also a |
| 31 | +> lucky number, meaning it is the smallest value in its row and the largest in its column. |
| 32 | +> We assess these assumptions using the following steps: |
| 33 | +> |
| 34 | +> 1. As `y` is a lucky number, the smallest value is in row r2 and the largest value is in column c2. Let’s denote the |
| 35 | +> integer at position (r2,c2) as `a` |
| 36 | +> - Then, `y` < `a` because `y` is the minimum in its row |
| 37 | +> - Then, `x` > `a` because `x` is the maximum in it column. |
| 38 | +> |
| 39 | +> Therefore `y` < `x` |
| 40 | +> |
| 41 | +> 2. Next, let's consider the integer at position (r1, c2), which we'll call `b` |
| 42 | +> - Then, `y` > `b` because `y` is the maximum in its column |
| 43 | +> - Then, `x` < `b` because `x` is the minimum in its row |
| 44 | +> |
| 45 | +> Therefore `y` > `x` |
| 46 | +> |
| 47 | +> This leads to a contradiction, as we deduced `y<x` and `y>x`. This inconsistency implies that our initial assumption— |
| 48 | +> that y is a lucky number—is incorrect. Therefore, only x can be the lucky number in this configuration. |
| 49 | +
|
| 50 | +This problem can be solved using a greedy algorithm that analyzes the matrix row by row and column by column. |
| 51 | + |
| 52 | +We start by iterating over the rows to find the minimum values. Out of those minimum values, we choose the largest |
| 53 | +minimum value and store it in r_largest_min. Similarly, we calculate the maximum values in columns and after finding |
| 54 | +all the largest values, we choose the smallest of them and store them in c_smallest_max. Once we have found the values |
| 55 | +in both rows and columns, we match them to see if they are the same. If they are, we return either of the values; |
| 56 | +r_largest_min or c_smallest_max. Otherwise if now matching value is found, we return an empty matrix. |
| 57 | + |
| 58 | +Following are the detailed steps of the algorithm that we have just discussed: |
| 59 | + |
| 60 | +1. We define two variables, r_largest_min and c_smallest_max: |
| 61 | + |
| 62 | + - r_largest_min is set to negative infinity (float('-inf')) to ensure any row’s minimum value can be updated. |
| 63 | + - c_smallest_max is set to positive infinity (float('inf')) to ensure any column’s maximum value can be updated. |
| 64 | + |
| 65 | +2. For each row in the matrix: |
| 66 | + |
| 67 | + - We calculate the minimum value of the row (r_min). |
| 68 | + - Then, we update r_largest_min to the maximum of r_largest_min and r_min. |
| 69 | + - The steps above ensure we consider only minimum values in their rows, narrowing the candidate set for a lucky number. |
| 70 | + |
| 71 | +3. For each column in the matrix: |
| 72 | + |
| 73 | + - We calculate the maximum value of the column (c_max) by iterating over all rows. |
| 74 | + - Next, we update c_max_min to the minimum of c_smallest_max and c_max. |
| 75 | + - The above steps ensure we consider only maximum values in their columns, further narrowing the candidate set for a lucky number. |
| 76 | + |
| 77 | +4. Finally, we compare whether r_largest_min equals c_smallest_max. If TRUE, we return the value stored in [r_largest_min]. |
| 78 | + Otherwise, we return an empty array []. The comparison ensures that the identified value satisfies both conditions of |
| 79 | + being the minimum in its row and the maximum in its column, making it a valid lucky number. |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +### Time Complexity |
| 99 | + |
| 100 | +The time complexity of the solution is O(m×n), where m is the number of columns in the matrix and n is the number of |
| 101 | +rows in the matrix. |
| 102 | + |
| 103 | +### Space Complexity |
| 104 | + |
| 105 | +The solution’s space complexity is O(1) as no extra space is required apart from the few variables. |
0 commit comments