From 76872872720885201f9b553e47009b1c7e16afe7 Mon Sep 17 00:00:00 2001 From: gameloader Date: Sat, 23 Nov 2024 16:58:05 +0800 Subject: [PATCH] leetcode update --- content/posts/leetcode.md | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index 620aeb7..697f5cf 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -18182,3 +18182,53 @@ public: }; ``` + +## day260 2024-11-23 + +### 1861. Rotating the Box +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. + +![1123pEbOZz2P5uHK](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/1123pEbOZz2P5uHK.png) + +### 题解 +本题图里的石头挺有意思的,像哪里来的魔法石。题目直观的想象是将一个容器翻转90°,翻转后石头会自由落体直到落到一块比较坚实的“地面”上。考虑原来的行和翻转后的列的关系,原来的第一行翻转后会变为第一列,如果共有m行n列,那么原来位于(p,q)位置的会变为位于(q,m-p-1)。先构建一个n x m的空数组,遍历原始的m x n数组,当按行遍历时统计该行中遇到过的石头的个数直到遇到一个障碍物,此时将该障碍物按照转换后的位置填入空数组中,并根据该障碍物前面的石头个数在空数组中该障碍物的位置向上填充对应数量的石头。如此反复,即得最终转换后的数组。 + +### 代码 +```cpp +class Solution { +public: + vector> rotateTheBox(vector>& box) { + int m = box.size(); + int n = box[0].size(); + vector> rotate(n,(vector(m,'.'))); + int stones = 0; + for(int i=0;i 0){ + rotate[j-stones][m-i-1] = '#'; + stones--; + } + } + } + while(stones > 0){ + rotate[n-stones][m-i-1] = '#'; + stones--; + } + } + return rotate; + } +}; +```