diff --git a/README.md b/README.md index 82748858..2831bf96 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | +|1861|[Rotating the Box](https://leetcode.com/problems/rotating-the-box/) | [C++](./algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp)|Medium| |1860|[Incremental Memory Leak](https://leetcode.com/problems/incremental-memory-leak/) | [C++](./algorithms/cpp/incrementalMemoryLeak/IncrementalMemoryLeak.cpp)|Medium| |1859|[Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence/) | [C++](./algorithms/cpp/sortingTheSentence/SortingTheSentence.cpp)|Easy| |1857|[Largest Color Value in a Directed Graph](https://leetcode.com/problems/largest-color-value-in-a-directed-graph/) | [C++](./algorithms/cpp/largestColorValueInADirectedGraph/LargestColorValueInADirectedGraph.cpp)|Hard| diff --git a/algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp b/algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp new file mode 100644 index 00000000..64c56c9a --- /dev/null +++ b/algorithms/cpp/rotatingTheBox/RotatingTheBox.cpp @@ -0,0 +1,95 @@ +// Source : https://leetcode.com/problems/rotating-the-box/ +// Author : Hao Chen +// Date : 2021-05-22 + +/***************************************************************************************************** + * + * 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 '.'. + ******************************************************************************************************/ + +class Solution { +public: + void rotate(vector>& src, vector>& dest) { + int m = src.size(); + for(int row=0; row>& box) { + int m = box.size(); + int n = box[0].size(); + for(int col=0; col=0; row-- ) { + + if (box[row][col] == '#') { + box[row][col] = '.'; + box[bottom][col] = '#'; + bottom--; + }else if (box[row][col] == '*') { + bottom = row-1; + } + } + } + } + vector> rotateTheBox(vector>& box) { + int row = box.size(); + int col = box[0].size(); + vector> result(col, vector(row,'.')); + rotate(box, result); + gravity(result); + return result; + } +};