Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間2年前 (2023/06/08 10:52), 編輯推噓3(301)
留言4則, 4人參與, 2年前最新討論串340/719 (看更多)
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: ------------------------------------------------------------ class Solution { public int countNegatives(int[][] grid) { int count = 0; for (int i = 0; i < grid.length; i++) { for (int j = grid[0].length - 1; j >= 0; j--) { if (grid[i][j] >= 0) { break; } count++; } } return count; } } ------------------------------------------------------------ -- https://i.imgur.com/3e5CZfj.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.75.86 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1686192740.A.C4A.html

06/08 11:22, 2年前 , 1F
這樣跑到n^2了
06/08 11:22, 1F

06/08 11:47, 2年前 , 2F
這題遍尋有辦法跑n嗎
06/08 11:47, 2F

06/08 12:04, 2年前 , 3F
這題應該能O(min(m,n)) 因為他rows column都是有序的
06/08 12:04, 3F

06/08 12:30, 2年前 , 4F
好像可以壓 做的時候沒想太多= =
06/08 12:30, 4F
文章代碼(AID): #1aWK9anA (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1aWK9anA (Marginalman)