返回介绍

solution / 1300-1399 / 1337.The K Weakest Rows in a Matrix / README_EN

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

1337. The K Weakest Rows in a Matrix

中文文档

Description

You are given an m x n binary matrix mat of 1's (representing soldiers) and 0's (representing civilians). The soldiers are positioned in front of the civilians. That is, all the 1's will appear to the left of all the 0's in each row.

A row i is weaker than a row j if one of the following is true:

  • The number of soldiers in row i is less than the number of soldiers in row j.
  • Both rows have the same number of soldiers and i < j.

Return _the indices of the _k_ weakest rows in the matrix ordered from weakest to strongest_.

 

Example 1:

Input: mat = 
[[1,1,0,0,0],
 [1,1,1,1,0],
 [1,0,0,0,0],
 [1,1,0,0,0],
 [1,1,1,1,1]], 
k = 3
Output: [2,0,3]
Explanation: 
The number of soldiers in each row is: 
- Row 0: 2 
- Row 1: 4 
- Row 2: 1 
- Row 3: 2 
- Row 4: 5 
The rows ordered from weakest to strongest are [2,0,3,1,4].

Example 2:

Input: mat = 
[[1,0,0,0],
 [1,1,1,1],
 [1,0,0,0],
 [1,0,0,0]], 
k = 2
Output: [0,2]
Explanation: 
The number of soldiers in each row is: 
- Row 0: 1 
- Row 1: 4 
- Row 2: 1 
- Row 3: 1 
The rows ordered from weakest to strongest are [0,2,3,1].

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 2 <= n, m <= 100
  • 1 <= k <= m
  • matrix[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
    m, n = len(mat), len(mat[0])
    ans = [n - bisect_right(row[::-1], 0) for row in mat]
    idx = list(range(m))
    idx.sort(key=lambda i: ans[i])
    return idx[:k]
class Solution {
  public int[] kWeakestRows(int[][] mat, int k) {
    int m = mat.length, n = mat[0].length;
    int[] res = new int[m];
    List<Integer> idx = new ArrayList<>();
    for (int i = 0; i < m; ++i) {
      idx.add(i);
      int[] row = mat[i];
      int left = 0, right = n;
      while (left < right) {
        int mid = (left + right) >> 1;
        if (row[mid] == 0) {
          right = mid;
        } else {
          left = mid + 1;
        }
      }
      res[i] = left;
    }
    idx.sort(Comparator.comparingInt(a -> res[a]));
    int[] ans = new int[k];
    for (int i = 0; i < k; ++i) {
      ans[i] = idx.get(i);
    }
    return ans;
  }
}
class Solution {
public:
  int search(vector<int>& m) {
    int l = 0;
    int h = m.size() - 1;
    while (l <= h) {
      int mid = l + (h - l) / 2;
      if (m[mid] == 0)
        h = mid - 1;
      else
        l = mid + 1;
    }
    return l;
  }

  vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
    vector<pair<int, int>> p;
    vector<int> res;
    for (int i = 0; i < mat.size(); i++) {
      int count = search(mat[i]);
      p.push_back({count, i});
    }
    sort(p.begin(), p.end());
    for (int i = 0; i < k; i++) {
      res.push_back(p[i].second);
    }
    return res;
  }
};
func kWeakestRows(mat [][]int, k int) []int {
  m, n := len(mat), len(mat[0])
  res := make([]int, m)
  var idx []int
  for i, row := range mat {
    idx = append(idx, i)
    left, right := 0, n
    for left < right {
      mid := (left + right) >> 1
      if row[mid] == 0 {
        right = mid
      } else {
        left = mid + 1
      }
    }
    res[i] = left
  }
  sort.Slice(idx, func(i, j int) bool {
    return res[idx[i]] < res[idx[j]] || (res[idx[i]] == res[idx[j]] && idx[i] < idx[j])
  })
  return idx[:k]
}
function kWeakestRows(mat: number[][], k: number): number[] {
  let n = mat.length;
  let sumMap = mat.map((d, i) => [d.reduce((a, c) => a + c, 0), i]);
  let ans = [];
  // 冒泡排序
  for (let i = 0; i < k; i++) {
    for (let j = i; j < n; j++) {
      if (
        sumMap[j][0] < sumMap[i][0] ||
        (sumMap[j][0] == sumMap[i][0] && sumMap[i][1] > sumMap[j][1])
      ) {
        [sumMap[i], sumMap[j]] = [sumMap[j], sumMap[i]];
      }
    }
    ans.push(sumMap[i][1]);
  }
  return ans;
}

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

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

发布评论

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