Last active
July 15, 2018 19:02
-
-
Save asethia/8e8b77c67beeaf9fd79b433908f00601 to your computer and use it in GitHub Desktop.
Given an NxN matrix, write a method to rotate the by 90 degrees.
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
import org.scalatest.{Matchers, FunSpec, WordSpec} | |
import scala.util.Try | |
/** | |
* Given an image represented by an NxN matrix, | |
* write a method to rotate the by 90 degrees. | |
* do this in place. | |
* Time Complexity O(n*n) | |
* Created by Arun Sethia on 7/8/18. | |
*/ | |
trait RotateMatrix { | |
/** | |
* rotate matrix to 90 degrees using space complexity O(nxn) | |
* Time Complexity O(nxn) | |
* @param data | |
* @return | |
*/ | |
def matrixRotate(data: Array[Array[Int]]): Array[Array[Int]] = { | |
data.head.length match { | |
case 0 => data | |
case _ => { | |
//get number of elements in each sub array (like cols) | |
val len = data.length - 1 | |
val rotated = (0 to len).map(i => (0 to len).map(j => data(j)(i)).toArray.reverse).toArray | |
rotated | |
} | |
} | |
} | |
/** | |
* rotate matrix by 90 degree using space complexity O(1) | |
* Time Complexity O(nxn) | |
* @param data | |
* @return | |
*/ | |
def inPlaceMatrixRotate(data: Array[Array[Int]]): Array[Array[Int]] = { | |
//if array is for 3x3 dimension then len will be 3 | |
//if array is for 4x4 dimension then len will be 4 | |
val len = length(data) | |
//total number of level rotation is require | |
val level = len / 2 - 1 | |
for (l <- 0 to level) { | |
//decrement the last as on level increases | |
val last = len - l - 1 | |
for (i <- l to (last - 1)) { | |
//temporary holder | |
var tmp = data(i)(last) | |
data(i)(last) = data(l)(i) | |
data(l)(i) = tmp | |
tmp = data(last)(last - i + l) | |
data(last)(last - i + l) = data(l)(i) | |
data(l)(i) = tmp | |
tmp = data(last - i + l)(l) | |
data(last - i + l)(l) = data(l)(i) | |
data(l)(i) = tmp | |
} | |
} | |
printData(data) | |
data | |
} | |
/** | |
* print array | |
* | |
* @param data | |
*/ | |
private def printData(data: Array[Array[Int]]): Unit = { | |
println(data.map(_.mkString(" ")).mkString("\n")) | |
println("\n") | |
} | |
/** | |
* create test matrix array | |
* | |
* @param dimension | |
* @return | |
*/ | |
def createArray(dimension: Int): Array[Array[Int]] = { | |
(0 to (dimension * dimension - 1)).toArray.grouped(dimension).toArray | |
} | |
/** | |
* get array size | |
* | |
* @param data | |
* @return | |
*/ | |
private def length(data: Array[Array[Int]]): Int = { | |
Try { | |
data.head.length | |
}.getOrElse(0) | |
} | |
} | |
class RotateMatrixSpec extends WordSpec | |
with Matchers | |
with RotateMatrix { | |
"rotate 3x3 metric to 90 degere" should { | |
"return rotated matrix" in { | |
val data: Array[Array[Int]] = createArray(3) | |
assert(matrixRotate(data) === Array(Array(6, 3, 0), Array(7, 4, 1), Array(8, 5, 2))) | |
} | |
} | |
"rotate 4x4 metric to 90 degere" should { | |
"return rotated matrix" in { | |
val data: Array[Array[Int]] = createArray(4) | |
assert(matrixRotate(data) === Array(Array(12, 8, 4, 0), Array(13, 9, 5, 1), Array(14, 10, 6, 2), Array(15, 11, 7, 3))) | |
} | |
} | |
"inplace rotate 3x3 metric to 90 degere" should { | |
"return rotated matrix" in { | |
val data: Array[Array[Int]] = createArray(3) | |
assert(inPlaceMatrixRotate(data) === Array(Array(6, 3, 0), Array(7, 4, 1), Array(8, 5, 2))) | |
} | |
} | |
"inplace rotate 4x4 metric to 90 degere" should { | |
"return rotated matrix" in { | |
val data: Array[Array[Int]] = createArray(4) | |
assert(inPlaceMatrixRotate(data) === Array(Array(12, 8, 4, 0), Array(13, 9, 5, 1), Array(14, 10, 6, 2), Array(15, 11, 7, 3))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment