The challenge
In this challenge your mission is to rotate matrix counter-clockwise N
times.
So, you will have 2 inputs:
1) matrix 2) a number, how many times to turn it
And the output is turned matrix.
Example:
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] times_to_turn = 1
It should return this:
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]])
Note: all matrixes will be square. Also, random tests will have big numbers in the input (times to turn)
The solution in Java code
Option 1:
public class Solution { public static int[][] rotateCounterclockwise(int[][] matrix, int times) { int matlen = matrix.length; for (int n = 0; n < times % 4; n++) { int[][] element = new int[matlen][matlen]; for (int i = 0; i < matlen; i++) { for (int j = 0; j < matlen; j++) { element[i][j] = matrix[j][matlen - 1 - i]; } } matrix = element; } return matrix; } }
Option 2:
public class Solution { public static int[][] rotateCounterclockwise(int[][] matrix, int times) { int size = matrix.length; int mat[][] = new int[size][size]; int temp[][]; times = times % 4; if (times==0) return matrix; while(times-->0) { for(int i=0,y=0;i<size;i++,y++) for(int j=0,x=size-1;j<size;j++,x--) mat[x][y]=matrix[i][j]; if(times>0){ temp=matrix; matrix=mat; mat=temp; } } return mat; } }
Option 3:
public class Solution { public static int[][] rotateCounterclockwise(int[][] matrix, int times) { times = times % 4; while(times > 0) { matrix = rotateOnce(matrix); times--; } return matrix; } private static int[][] rotateOnce(int[][] matrix) { int[][] r = new int[matrix.length][matrix.length]; for (int i = r.length-1; i >=0; i--) { for (int j = 0; j < r.length; j++) { r[r.length-1-i][j] = matrix[j][i]; } } return r; } }
Test cases to validate our solution
import org.junit.Test; import java.util.Random; import java.util.function.BiFunction; import java.util.function.IntFunction; import java.util.function.IntSupplier; import java.util.stream.IntStream; import java.util.stream.Stream; import org.junit.Assert; public class SolutionTest { @Test public void exampleTest() { Assert.assertArrayEquals(new int[][] { {2, 4}, {1, 3}}, Solution.rotateCounterclockwise(new int[][] { {1, 2}, {3, 4}}, 1)); Assert.assertArrayEquals(new int[][] { {4, 8, 12, 16}, {3, 7, 11, 15}, {2, 6, 10, 14}, {1, 5, 9, 13}}, Solution.rotateCounterclockwise(new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}, 1)); Assert.assertArrayEquals(new int[][] { {16, 15, 14, 13}, {12, 11, 10, 9}, {8, 7, 6, 5}, {4, 3, 2, 1}}, Solution.rotateCounterclockwise(new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}, 2)); Assert.assertArrayEquals(new int[][] { {57, 49, 41, 33, 25, 17, 9, 1}, {58, 50, 42, 34, 26, 18, 10, 2}, {59, 51, 43, 35, 27, 19, 11, 3}, {60, 52, 44, 36, 28, 20, 12, 4}, {61, 53, 45, 37, 29, 21, 13, 5}, {62, 54, 46, 38, 30, 22, 14, 6}, {63, 55, 47, 39, 31, 23, 15, 7}, {64, 56, 48, 40, 32, 24, 16, 8}}, Solution.rotateCounterclockwise(new int[][] { {1, 2, 3, 4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16}, {17, 18, 19, 20, 21, 22, 23, 24}, {25, 26, 27, 28, 29, 30, 31, 32}, {33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48}, {49, 50, 51, 52, 53, 54, 55, 56}, {57, 58, 59, 60, 61, 62, 63, 64}}, 3)); } }