Skip to content

chore: add optimize note for lc-1380 #3331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions solution/1300-1399/1380.Lucky Numbers in a Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

#### Python3
Expand Down