|
| 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: Greedy |
| 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 | +> Visually, this looks like this: |
| 51 | +> |
| 52 | +>  |
| 53 | +
|
| 54 | +This problem can be solved using a greedy algorithm that analyzes the matrix row by row and column by column. |
| 55 | + |
| 56 | +We start by iterating over the rows to find the minimum values. Out of those minimum values, we choose the largest |
| 57 | +minimum value and store it in r_largest_min. Similarly, we calculate the maximum values in columns and after finding |
| 58 | +all the largest values, we choose the smallest of them and store them in c_smallest_max. Once we have found the values |
| 59 | +in both rows and columns, we match them to see if they are the same. If they are, we return either of the values; |
| 60 | +r_largest_min or c_smallest_max. Otherwise if now matching value is found, we return an empty matrix. |
| 61 | + |
| 62 | +Following are the detailed steps of the algorithm that we have just discussed: |
| 63 | + |
| 64 | +1. We define two variables, r_largest_min and c_smallest_max: |
| 65 | + |
| 66 | + - r_largest_min is set to negative infinity (float('-inf')) to ensure any row’s minimum value can be updated. |
| 67 | + - c_smallest_max is set to positive infinity (float('inf')) to ensure any column’s maximum value can be updated. |
| 68 | + |
| 69 | +2. For each row in the matrix: |
| 70 | + |
| 71 | + - We calculate the minimum value of the row (r_min). |
| 72 | + - Then, we update r_largest_min to the maximum of r_largest_min and r_min. |
| 73 | + - The steps above ensure we consider only minimum values in their rows, narrowing the candidate set for a lucky number. |
| 74 | + |
| 75 | +3. For each column in the matrix: |
| 76 | + |
| 77 | + - We calculate the maximum value of the column (c_max) by iterating over all rows. |
| 78 | + - Next, we update c_max_min to the minimum of c_smallest_max and c_max. |
| 79 | + - The above steps ensure we consider only maximum values in their columns, further narrowing the candidate set for a |
| 80 | + lucky number. |
| 81 | + |
| 82 | +4. Finally, we compare whether r_largest_min equals c_smallest_max. If TRUE, we return the value stored in [r_largest_min]. |
| 83 | + Otherwise, we return an empty array []. The comparison ensures that the identified value satisfies both conditions of |
| 84 | + being the minimum in its row and the maximum in its column, making it a valid lucky number. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +### Time Complexity |
| 104 | + |
| 105 | +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 |
| 106 | +rows in the matrix. |
| 107 | + |
| 108 | +### Space Complexity |
| 109 | + |
| 110 | +The solution’s space complexity is O(1) as no extra space is required apart from the few variables. |
| 111 | + |
| 112 | +## Solution: Simulation |
| 113 | + |
| 114 | +We are given a matrix of size MXN with distinct integers. We need to return the list of lucky numbers in the matrix. |
| 115 | +An integer in the matrix is lucky if it is the maximum integer in its column and it is the minimum value in its row. |
| 116 | + |
| 117 | +In this approach, we will simulate the process by iterating over each integer in the matrix, checking if it is the |
| 118 | +maximum in its row and the minimum in its column. If it meets both criteria, we will add it to the list of lucky numbers, |
| 119 | +luckyNumbers. |
| 120 | + |
| 121 | +The naive approach to check the criteria for each integer involves iterating over each integer in the current row and |
| 122 | +column to verify the minimum and maximum criteria, requiring M+N operations per integer. A more efficient method is to |
| 123 | +precompute the minimum of each row and the maximum of each column before processing the matrix. This allows us to check |
| 124 | +the criteria for each integer in constant time. We iterate over each row to store the minimum in rowMin and each column |
| 125 | +to store the maximum in colMax. |
| 126 | + |
| 127 | +### Algorithm |
| 128 | + |
| 129 | +1. Iterate over each row and store the minimum of the ith row at the ith position in the list rowMin. |
| 130 | +2. Iterate over each column and store the maximum of the ith column at the ith position in the list colMax. |
| 131 | +3. Iterate over each integer in the matrix and for each integer at (i, j), check if the integer is equal to rowMin[i] |
| 132 | + and colMax[j]. If yes, add it to the list luckyNumbers. |
| 133 | +4. Return luckyNumbers. |
| 134 | + |
| 135 | +### Complexity Analysis |
| 136 | + |
| 137 | +Here, N is the number of rows in the matrix and M is the number of columns in the matrix. |
| 138 | + |
| 139 | +#### Time complexity: O(N*M). |
| 140 | + |
| 141 | +To store the maximum of each row, we require N*M operations and the same for strong the maximum of each column. In the |
| 142 | +end, to find the lucky numbers we again iterate over each integer. Hence, the total time complexity is equal to O(N*M). |
| 143 | + |
| 144 | +#### Space complexity: O(N+M). |
| 145 | + |
| 146 | +We require two lists, rowMin and colMax of size N and M respectively. Hence the total space complexity is equal to O(N+M). |
0 commit comments