Last active
November 30, 2024 15:46
-
-
Save asunar/3778c443ad256c1575eed34a24ca870a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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. | |
| Input: box = [["#",".","#"]] | |
| Output: [["."], | |
| ["#"], | |
| ["#"]] | |
| Input: box = [["#",".","*","."], | |
| ["#","#","*","."]] | |
| Output: [["#","."], | |
| ["#","#"], | |
| ["*","*"], | |
| [".","."]] | |
| Input: box = [["#","#","*",".","*","."], | |
| ["#","#","#","*",".","."], | |
| ["#","#","#",".","#","."]] | |
| Output: [[".","#","#"], | |
| [".","#","#"], | |
| ["#","#","*"], | |
| ["#","*","."], | |
| ["#",".","*"], | |
| ["#",".","."]] | |
| Constraints: | |
| m == box.length | |
| n == box[i].length | |
| 1 <= m, n <= 500 | |
| box[i][j] is either '#', '*', or '.' | |
| */ | |
| /** | |
| * @param {character[][]} box | |
| * @return {character[][]} | |
| */ | |
| var rotateTheBox = function(box) { | |
| // In the rotatedBox array | |
| // box's length will be column length | |
| // subarray's length will be row length | |
| const rotatedColumnLength = box.length | |
| const rotatedRowLength = box[0].length | |
| const rotatedBox = createdRotatedBox(rotatedRowLength, rotatedColumnLength) | |
| // populate rotatedBox | |
| for(let r = 0; r < box.length; r++){ | |
| for(let c = 0; c < box[0].length; c++){ | |
| const rotatedBoxIndices = getRotatedBoxIndex(r, c, box.length) | |
| const rowIndexInRotatedBox = rotatedBoxIndices.rowIndex | |
| const colIndexInRotatedBox = rotatedBoxIndices.columnIndex | |
| rotatedBox[rowIndexInRotatedBox][colIndexInRotatedBox] = box[r][c] | |
| } | |
| } | |
| // const columnBeforeGravity = ["#", "#", "#", ".", "#", "."] | |
| // const columnBeforeGravity = ["#", "#", "#", "*", ".", "."] | |
| // const columnBeforeGravity = ["#",".","*","."] | |
| // const columnBeforeGravity = ["#","#","*",".","*","."] | |
| // const columnBeforeGravity = ["#","#","#",".","#","."] | |
| // console.log("columnBeforeGravity\t", columnBeforeGravity) | |
| // const columnAfterGravity = applyGravity(columnBeforeGravity) | |
| // console.log("columnAfterGravity\t", columnAfterGravity) | |
| const colsInRotatedBox = getColumns(rotatedBox) | |
| const colsAfterGravity = colsInRotatedBox.map(c => applyGravity(c)) | |
| const boxAfterGravity = columnsToArray(colsAfterGravity) | |
| return boxAfterGravity | |
| // console.log("A: 0, 0", getRotatedBoxIndex(0, 0, 3)) | |
| // console.log("B: 0, 1", getRotatedBoxIndex(0, 1, 3)) | |
| // console.log("C: 0, 2", getRotatedBoxIndex(0, 2, 3)) | |
| // console.log("D: 0, 3", getRotatedBoxIndex(0, 3, 3)) | |
| // console.log("-----------------------") | |
| // console.log("E: 1, 0", getRotatedBoxIndex(1, 0, 3)) | |
| // console.log("F: 1, 1", getRotatedBoxIndex(1, 1, 3)) | |
| // console.log("G: 1, 2", getRotatedBoxIndex(1, 2, 3)) | |
| // console.log("H: 1, 3", getRotatedBoxIndex(1, 3, 3)) | |
| // console.log("-----------------------") | |
| // console.log("I: 2, 0", getRotatedBoxIndex(2, 0, 3)) | |
| // console.log("J: 2, 1", getRotatedBoxIndex(2, 1, 3)) | |
| // console.log("K: 2, 2", getRotatedBoxIndex(2, 2, 3)) | |
| // console.log("L: 2, 3", getRotatedBoxIndex(2, 3, 3)) | |
| }; | |
| // ["#","#","*",".","*","."] | |
| const applyGravity= (items) => { | |
| // const dotIndex = items.indexOf(".") | |
| // if(dotIndex === -1) return items | |
| // const hashIndex = items.indexOf("#") | |
| // if(hashIndex > items.lastIndexOf(".")) return items | |
| // if(items.indexOf("*") !== -1 | |
| // && items.indexOf("*") > items.indexOf("#") | |
| // && items.indexOf(".") !== -1 | |
| // && items.indexOf(".") < items.indexOf("#")) return items | |
| let swapped = false | |
| for(let i = 0; i < items.length; i++){ | |
| if(items[i] === "#" && items[i+1] === "."){ | |
| swapped = true | |
| let temp = items[i] | |
| items[i] = items[i+1] | |
| items[i+1] = temp | |
| } | |
| } | |
| if(swapped) return applyGravity(items) | |
| return items | |
| } | |
| const createdRotatedBox = (rowLength, columnLength) => { | |
| // console.log(box) | |
| const rotatedBox = []; | |
| for(let r = 0; r < rowLength; r++){ | |
| const rowArray = []; | |
| for(let c = 0; c < columnLength; c++){ | |
| rowArray.push("X") | |
| } | |
| rotatedBox.push(rowArray) | |
| } | |
| return rotatedBox | |
| } | |
| const getColumns = function (array) { | |
| // Check if the input is a valid 2D array | |
| if (!Array.isArray(array) || array.length === 0 || !Array.isArray(array[0])) { | |
| return []; | |
| } | |
| // Get the number of columns based on the first row's length | |
| const numColumns = array[0].length; | |
| // Initialize an array to store columns | |
| const columns = Array.from({ length: numColumns }, () => []); | |
| // Iterate through each row | |
| for (const row of array) { | |
| // Ensure the current row has the same length as the first row | |
| if (row.length !== numColumns) { | |
| throw new Error('All rows must have the same length'); | |
| } | |
| // Add each element to its corresponding column | |
| for (let colIndex = 0; colIndex < numColumns; colIndex++) { | |
| columns[colIndex].push(row[colIndex]); | |
| } | |
| } | |
| return columns; | |
| } | |
| const columnsToArray = function (columns) { | |
| // Check if the input is a valid columns array | |
| if (!Array.isArray(columns) || columns.length === 0) { | |
| return []; | |
| } | |
| // Validate that all columns have the same length | |
| const numRows = columns[0].length; | |
| if (!columns.every(column => column.length === numRows)) { | |
| throw new Error('All columns must have the same length'); | |
| } | |
| // Initialize the 2D array | |
| const twoDArray = []; | |
| // Iterate through row indices | |
| for (let rowIndex = 0; rowIndex < numRows; rowIndex++) { | |
| // Create a new row by taking the rowIndex-th element from each column | |
| const row = columns.map(column => column[rowIndex]); | |
| twoDArray.push(row); | |
| } | |
| return twoDArray; | |
| } | |
| const getRotatedBoxIndex = (rowIndex, columnIndex, rowLength) => ({rowIndex: columnIndex, columnIndex: rowLength - 1 - rowIndex}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment