返回介绍

solution / 2300-2399 / 2387.Median of a Row Wise Sorted Matrix / README_EN

发布于 2024-06-17 01:03:06 字数 4230 浏览 0 评论 0 收藏 0

2387. Median of a Row Wise Sorted Matrix

中文文档

Description

Given an m x n matrix grid containing an odd number of integers where each row is sorted in non-decreasing order, return _the median of the matrix_.

You must solve the problem in less than O(m * n) time complexity.

 

Example 1:

Input: grid = [[1,1,2],[2,3,3],[1,3,4]]
Output: 2
Explanation: The elements of the matrix in sorted order are 1,1,1,2,2,3,3,3,4. The median is 2.

Example 2:

Input: grid = [[1,1,3,3,4]]
Output: 3
Explanation: The elements of the matrix in sorted order are 1,1,3,3,4. The median is 3.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 500
  • m and n are both odd.
  • 1 <= grid[i][j] <= 106
  • grid[i] is sorted in non-decreasing order.

Solutions

Solution 1

class Solution:
  def matrixMedian(self, grid: List[List[int]]) -> int:
    def count(x):
      return sum(bisect_right(row, x) for row in grid)

    m, n = len(grid), len(grid[0])
    target = (m * n + 1) >> 1
    return bisect_left(range(10**6 + 1), target, key=count)
class Solution {
  private int[][] grid;

  public int matrixMedian(int[][] grid) {
    this.grid = grid;
    int m = grid.length, n = grid[0].length;
    int target = (m * n + 1) >> 1;
    int left = 0, right = 1000010;
    while (left < right) {
      int mid = (left + right) >> 1;
      if (count(mid) >= target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }

  private int count(int x) {
    int cnt = 0;
    for (var row : grid) {
      int left = 0, right = row.length;
      while (left < right) {
        int mid = (left + right) >> 1;
        if (row[mid] > x) {
          right = mid;
        } else {
          left = mid + 1;
        }
      }
      cnt += left;
    }
    return cnt;
  }
}
class Solution {
public:
  int matrixMedian(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    int left = 0, right = 1e6 + 1;
    int target = (m * n + 1) >> 1;
    auto count = [&](int x) {
      int cnt = 0;
      for (auto& row : grid) {
        cnt += (upper_bound(row.begin(), row.end(), x) - row.begin());
      }
      return cnt;
    };
    while (left < right) {
      int mid = (left + right) >> 1;
      if (count(mid) >= target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
};
func matrixMedian(grid [][]int) int {
  m, n := len(grid), len(grid[0])

  count := func(x int) int {
    cnt := 0
    for _, row := range grid {
      left, right := 0, n
      for left < right {
        mid := (left + right) >> 1
        if row[mid] > x {
          right = mid
        } else {
          left = mid + 1
        }
      }
      cnt += left
    }
    return cnt
  }
  left, right := 0, 1000010
  target := (m*n + 1) >> 1
  for left < right {
    mid := (left + right) >> 1
    if count(mid) >= target {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return left
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文