code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

03 Rotate Image

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * You are given an n x n 2D matrix that represents an image. Rotate the image by 90 degrees (clockwise).
 *
 * Example
 *
 * For
 *
 * a = [[1, 2, 3],
 *      [4, 5, 6],
 *      [7, 8, 9]]
 * the output should be
 *
 * rotateImage(a) =
 *     [[7, 4, 1],
 *      [8, 5, 2],
 *      [9, 6, 3]]
 */

function rotateImage(a) {
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < i; j++) {
      let temp = a[i][j]
      a[i][j] = a[j][i]
      a[j][i] = temp
    }    
  }
  for (let i = 0; i < a.length; i++) {
    for (let j = 0; j < a[0].length/2; j++) {
      let temp = a[i][j]      
      a[i][j] = a[i][a[0].length-1-j]
      a[i][a[0].length-1-j] = temp     
    }    
  }
  return a
}

test('rotate Image', () => {
  expect(
    rotateImage([
      [1, 2, 3],
      [4, 5, 6],
      [7, 8, 9],
    ]),
  ).toEqual([
    [7, 4, 1],
    [8, 5, 2],
    [9, 6, 3],
  ])
})

Created 2020-03-31T14:01:50+00:00 · Edit