diff --git a/solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md b/solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md index 541f37076eb83..d9f6c5c8378c8 100644 --- a/solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md +++ b/solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md @@ -73,6 +73,19 @@ After the traversal is finished, we return the answer array. 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. +#### Optimize + +We can return the lucky number immediately when we found it, since there's only one lucky nunber. +We can prove this as follows: + +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: + +- $m[u][v]$ is the min of the row $u => m[u][v] < m[u][j] < m[i][j]$ +- $m[u][v]$ is the max of the col $v => m[u][v] > m[i][v] > m[i][j]$ + +Thus $m[u][v] < m[i][j]$ and $m[u][v] > m[i][j]$, which is a contradiction. +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. + #### Python3