返回介绍

solution / 1800-1899 / 1861.Rotating the Box / README_EN

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

1861. Rotating the Box

中文文档

Description

You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:

  • A stone '#'
  • A stationary obstacle '*'
  • Empty '.'

The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.

It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.

Return _an _n x m_ matrix representing the box after the rotation described above_.

 

Example 1:


Input: box = [["#",".","#"]]

Output: [["."],

     ["#"],

     ["#"]]

Example 2:


Input: box = [["#",".","*","."],

        ["#","#","*","."]]

Output: [["#","."],

     ["#","#"],

     ["*","*"],

     [".","."]]

Example 3:


Input: box = [["#","#","*",".","*","."],

        ["#","#","#","*",".","."],

        ["#","#","#",".","#","."]]

Output: [[".","#","#"],

     [".","#","#"],

     ["#","#","*"],

     ["#","*","."],

     ["#",".","*"],

     ["#",".","."]]

 

Constraints:

  • m == box.length
  • n == box[i].length
  • 1 <= m, n <= 500
  • box[i][j] is either '#', '*', or '.'.

Solutions

Solution 1: Queue Simulation

First, we rotate the matrix 90 degrees clockwise, then simulate the falling process of the stones in each column.

The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix, respectively.

class Solution:
  def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
    m, n = len(box), len(box[0])
    ans = [[None] * m for _ in range(n)]
    for i in range(m):
      for j in range(n):
        ans[j][m - i - 1] = box[i][j]
    for j in range(m):
      q = deque()
      for i in range(n - 1, -1, -1):
        if ans[i][j] == '*':
          q.clear()
        elif ans[i][j] == '.':
          q.append(i)
        elif q:
          ans[q.popleft()][j] = '#'
          ans[i][j] = '.'
          q.append(i)
    return ans
class Solution {
  public char[][] rotateTheBox(char[][] box) {
    int m = box.length, n = box[0].length;
    char[][] ans = new char[n][m];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        ans[j][m - i - 1] = box[i][j];
      }
    }
    for (int j = 0; j < m; ++j) {
      Deque<Integer> q = new ArrayDeque<>();
      for (int i = n - 1; i >= 0; --i) {
        if (ans[i][j] == '*') {
          q.clear();
        } else if (ans[i][j] == '.') {
          q.offer(i);
        } else if (!q.isEmpty()) {
          ans[q.pollFirst()][j] = '#';
          ans[i][j] = '.';
          q.offer(i);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
    int m = box.size(), n = box[0].size();
    vector<vector<char>> ans(n, vector<char>(m));
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        ans[j][m - i - 1] = box[i][j];
      }
    }
    for (int j = 0; j < m; ++j) {
      queue<int> q;
      for (int i = n - 1; ~i; --i) {
        if (ans[i][j] == '*') {
          queue<int> t;
          swap(t, q);
        } else if (ans[i][j] == '.') {
          q.push(i);
        } else if (!q.empty()) {
          ans[q.front()][j] = '#';
          q.pop();
          ans[i][j] = '.';
          q.push(i);
        }
      }
    }
    return ans;
  }
};
func rotateTheBox(box [][]byte) [][]byte {
  m, n := len(box), len(box[0])
  ans := make([][]byte, n)
  for i := range ans {
    ans[i] = make([]byte, m)
  }
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      ans[j][m-i-1] = box[i][j]
    }
  }
  for j := 0; j < m; j++ {
    q := []int{}
    for i := n - 1; i >= 0; i-- {
      if ans[i][j] == '*' {
        q = []int{}
      } else if ans[i][j] == '.' {
        q = append(q, i)
      } else if len(q) > 0 {
        ans[q[0]][j] = '#'
        q = q[1:]
        ans[i][j] = '.'
        q = append(q, i)
      }
    }
  }
  return ans
}

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

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

发布评论

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