Skip to content

Commit bea4b18

Browse files
committed
chore: add optimize note for lc-1380
1 parent 8aa6828 commit bea4b18

File tree

1 file changed

+14
-0
lines changed
  • solution/1300-1399/1380.Lucky Numbers in a Matrix

1 file changed

+14
-0
lines changed

solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ After the traversal is finished, we return the answer array.
7373

7474
The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$. Where $m$ and $n$ are the number of rows and columns in the matrix, respectively.
7575

76+
#### Optimize
77+
78+
We can return the lucky number immediately when we found it, since there's only one lucky nunber.
79+
We can prove this as follows:
80+
81+
Given the input matrix contains only distinct numbers, now let suppose $[i,j]$ is the lucky number, we assume that there's another lucky number $[u,v]$, then we have:
82+
83+
- $m[u][v]$ is the min of the row $u => m[u][v] < m[u][j] < m[i][j]$
84+
- $m[u][v]$ is the max of the col $v => m[u][v] > m[i][v] > m[i][j]$
85+
86+
Thus $m[u][v] < m[i][j]$ and $m[u][v] > m[i][j]$, which is a contradiction.
87+
Therefore $[u,v]$ either does not exist or $[u,v]$ is the same as $[i,j]$. Since the matrix contains only distinct numbers, there is only one lucky number.
88+
89+
7690
<!-- tabs:start -->
7791

7892
#### Python3

0 commit comments

Comments
 (0)