Advertisement

Rotate Image - LeetCode 48 Solution

Rotate Image - Complete Solution Guide

Rotate Image is LeetCode problem 48, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place , which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Constraints: n == matrix

Detailed Explanation

The problem requires rotating an `n x n` 2D matrix representing an image by 90 degrees clockwise in-place. This means modifying the original matrix directly without using extra space for a new matrix. The input is a 2D array (matrix) of integers, and the output is the modified input matrix after the rotation.

Solution Approach

The provided solution approach involves two main steps: transposing the matrix and then reversing each row. Transposing involves swapping elements across the main diagonal (i.e., swapping matrix[i][j] with matrix[j][i]). Reversing each row involves swapping the elements from the start and end of the row, moving towards the middle.

Step-by-Step Algorithm

  1. Step 1: Iterate through the matrix to transpose it. This involves iterating over the upper triangle of the matrix (i.e., i from 0 to n-1, and j from i+1 to n-1) and swapping matrix[i][j] with matrix[j][i]. This avoids redundant swaps.
  2. Step 2: Iterate through each row of the matrix.
  3. Step 3: Reverse each row by using two pointers (left and right) initialized to the start and end of the row. Swap the elements at the left and right pointers, and then move the left pointer one step to the right and the right pointer one step to the left. Continue until the left and right pointers meet.
  4. Step 4: The matrix is now rotated 90 degrees clockwise.

Key Insights

  • Insight 1: Rotating a matrix 90 degrees clockwise can be achieved by first transposing the matrix (swapping rows and columns) and then reversing each row.
  • Insight 2: The in-place constraint means you must manipulate the original matrix directly. This limits your options but also simplifies the space complexity.
  • Insight 3: Understanding the index transformations during transpose and row reversal is crucial for correct implementation.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: Array, Math, Matrix.

Companies

Asked at: AMD, Accenture, Adobe, Amazon, Apple, Bloomberg, Capital One, Cisco, ConsultAdd, DE Shaw, Goldman Sachs, Google, IBM, Infosys, Intel, J.P. Morgan, Mastercard, Meta, Microsoft, Netflix, Nvidia, Oracle, PayPal, Qualcomm, Rakuten, Roblox, Tinkoff, Uber, WatchGuard, Yahoo, Yandex, ZScaler, Zoho.