Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-06-08 10:52:18
https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/description/
1351. Count Negative Numbers in a Sorted Matrix
給你一個排序好的 Matrix,找出該 Matrix 有幾個非正整數。
Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Example 2:
Input: grid = [[3,2],[1,0]]
Output: 0
思路:
1.從每一列的最右邊往左找,如果小於0 count就遞增,否則跳出處理下一列。
Java Code:
作者: pandix (麵包屌)   2023-06-08 11:22:00
這樣跑到n^2了
作者: Che31128 (justjoke)   2023-06-08 11:47:00
這題遍尋有辦法跑n嗎
作者: NTHUlagka (拉卡)   2023-06-08 12:04:00
這題應該能O(min(m,n)) 因為他rows column都是有序的
作者: Rushia (みけねこ的鼻屎)   2023-06-08 12:30:00
好像可以壓 做的時候沒想太多= =

Links booklink

Contact Us: admin [ a t ] ucptt.com