2023年8月4日 星期五

8/4 每日一題(找出矩陣中數字最大的數量)

 You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i] = [ai, bi] means M[x][y] should be incremented by one for all 0 <= x < ai and 0 <= y < bi.

Count and return the number of maximum integers in the matrix after performing all the operations.

 

Example 1:

Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.

Example 2:

Input: m = 3, n = 3, ops = [[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3],[2,2],[3,3],[3,3],[3,3]]
Output: 4

Example 3:

Input: m = 3, n = 3, ops = []
Output: 9

 

Constraints:

  • 1 <= m, n <= 4 * 104
  • 0 <= ops.length <= 104
  • ops[i].length == 2
  • 1 <= ai <= m
  • 1 <= bi <= n

class Solution:
    def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
      if not len(ops):
        return m*n
      #找出最短的高和最短的寬
      min_x=10000000
      min_y=10000000
      for i in ops:
        min_x=min(min_x,i[0])
        min_y=min(min_y,i[1])
      return min_x * min_y
     

----------------------------
class Solution:
    def maxCount(self, m: int, n: int, ops: List[List[int]]) -> int:
        minRow = m
        minCol = n
----初始化最小的矩陣大小為m和n
        for x, y in ops:
            minRow = min(x, minRow)
            minCol = min(y, minCol)
---遍歷迴圈找出最小的矩陣
        return minRow*minCol











標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁