返回介绍

solution / 0500-0599 / 0533.Lonely Pixel II / README_EN

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

533. Lonely Pixel II

中文文档

Description

Given an m x n picture consisting of black 'B' and white 'W' pixels and an integer target, return _the number of black lonely pixels_.

A black lonely pixel is a character 'B' that located at a specific position (r, c) where:

  • Row r and column c both contain exactly target black pixels.
  • For all rows that have a black pixel at column c, they should be exactly the same as row r.

 

Example 1:

Input: picture = [["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","W","B","W","B","W"]], target = 3
Output: 6
Explanation: All the green 'B' are the black pixels we need (all 'B's at column 1 and 3).
Take 'B' at row r = 0 and column c = 1 as an example:
 - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels. 
 - Rule 2, the rows have black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.

Example 2:

Input: picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1
Output: 0

 

Constraints:

  • m == picture.length
  • n == picture[i].length
  • 1 <= m, n <= 200
  • picture[i][j] is 'W' or 'B'.
  • 1 <= target <= min(m, n)

Solutions

Solution 1

class Solution:
  def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
    m, n = len(picture), len(picture[0])
    rows = [0] * m
    cols = defaultdict(list)
    for i in range(m):
      for j in range(n):
        if picture[i][j] == 'B':
          rows[i] += 1
          cols[j].append(i)
    t = [[False] * m for _ in range(m)]
    for i in range(m):
      for k in range(i, m):
        if i == k:
          t[i][k] = True
        else:
          t[i][k] = all([picture[i][j] == picture[k][j] for j in range(n)])
        t[k][i] = t[i][k]
    res = 0
    for i in range(m):
      if rows[i] == target:
        for j in range(n):
          if len(cols[j]) == target and all([t[i][k] for k in cols[j]]):
            res += 1
    return res
class Solution {
  public int findBlackPixel(char[][] picture, int target) {
    int m = picture.length, n = picture[0].length;
    int[] rows = new int[m];
    Map<Integer, List<Integer>> cols = new HashMap<>();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (picture[i][j] == 'B') {
          ++rows[i];
          cols.computeIfAbsent(j, k -> new ArrayList<>()).add(i);
        }
      }
    }
    boolean[][] t = new boolean[m][m];
    for (int i = 0; i < m; ++i) {
      for (int k = i; k < m; ++k) {
        t[i][k] = i == k || all(picture[i], picture[k]);
        t[k][i] = t[i][k];
      }
    }
    int res = 0;
    for (int i = 0; i < m; ++i) {
      if (rows[i] == target) {
        for (int j = 0; j < n; ++j) {
          List<Integer> col = cols.get(j);
          if (col != null && col.size() == target) {
            boolean check = true;
            for (int k : col) {
              check = check && t[i][k];
            }
            if (check) {
              ++res;
            }
          }
        }
      }
    }
    return res;
  }

  private boolean all(char[] row1, char[] row2) {
    int n = row1.length;
    for (int j = 0; j < n; ++j) {
      if (row1[j] != row2[j]) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  int findBlackPixel(vector<vector<char>>& picture, int target) {
    int m = picture.size(), n = picture[0].size();
    vector<int> rows(m);
    unordered_map<int, vector<int>> cols;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (picture[i][j] == 'B') {
          ++rows[i];
          cols[j].push_back(i);
        }
      }
    }
    vector<vector<bool>> t(m, vector<bool>(m, false));
    for (int i = 0; i < m; ++i) {
      for (int k = i; k < m; ++k) {
        t[i][k] = i == k || all(picture[i], picture[k]);
        t[k][i] = t[i][k];
      }
    }
    int res = 0;
    for (int i = 0; i < m; ++i) {
      if (rows[i] == target) {
        for (int j = 0; j < n; ++j) {
          if (cols[j].size() == target) {
            bool check = true;
            for (int k : cols[j]) check = check && t[i][k];
            if (check) ++res;
          }
        }
      }
    }
    return res;
  }

  bool all(vector<char>& row1, vector<char>& row2) {
    int n = row1.size();
    for (int j = 0; j < n; ++j)
      if (row1[j] != row2[j]) return false;
    return true;
  }
};
func findBlackPixel(picture [][]byte, target int) int {
  m, n := len(picture), len(picture[0])
  rows := make([]int, m)
  cols := make(map[int][]int)
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      if picture[i][j] == 'B' {
        rows[i]++
        cols[j] = append(cols[j], i)
      }
    }
  }
  t := make([][]bool, m)
  for i := 0; i < m; i++ {
    t[i] = make([]bool, m)
  }
  for i := 0; i < m; i++ {
    for k := i; k < m; k++ {
      if i == k {
        t[i][k] = true
      } else {
        t[i][k] = all(picture[i], picture[k])
      }
      t[k][i] = t[i][k]
    }
  }
  res := 0
  for i := 0; i < m; i++ {
    if rows[i] == target {
      for j := 0; j < n; j++ {
        col, ok := cols[j]
        if ok && len(col) == target {
          check := true
          for _, k := range col {
            check = check && t[i][k]
          }
          if check {
            res++
          }
        }
      }
    }
  }
  return res
}

func all(row1, row2 []byte) bool {
  n := len(row1)
  for i := 0; i < n; i++ {
    if row1[i] != row2[i] {
      return false
    }
  }
  return true
}

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

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

发布评论

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