Transpose Matrix

🏠 ⬅️ ➡️

Given a 2D integer array matrix, return the transpose of matrix.

The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]

Example 2:

Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 1000
  • 1 <= m * n <= 105
  • -109 <= matrix[i][j] <= 109

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: matrix(3, 3)

    matrix(:, :) = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(matrix))

    write(*, '(A)') 'Example 1:'
    call print_matrix(transpose(matrix))

    matrix(:, :) = reshape([1, 2, 3, 4, 5, 6], shape(matrix))

    write(*, '(A)') 'Example 2:'
    call print_matrix(transpose(matrix))

contains

    subroutine print_matrix(matrix)
        implicit none
        integer, intent(in) :: matrix(:, :)
        integer :: i, j

        do i = 1, size(matrix, 1)
            write(*, '(*(I2, 1X))') (matrix(i, j), j=1, size(matrix, 2))
        end do
    end subroutine print_matrix

end program main
Compiled
Executed
Correct
module transpose

implicit none

contains

function transpose(matrix) result(transposed_matrix)

integer, intent(in) :: matrix(:, :)
integer :: transposed_matrix(size(matrix, 2), size(matrix, 1))
integer :: i, j

do i = 1, size(matrix, 1)
    do j = 1, size(matrix, 2)
        transposed_matrix(j, i) = matrix(i, j)
    end do
end do

end function transpose

end module transpose

program main

use transpose

implicit none

integer, allocatable :: matrix(:, :)
integer :: i, j

! Example 1
allocate(matrix(3, 3))
matrix = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3])
write (*, '(A)') 'Example 1'
write (*, '(A)') 'Input:'
write (*, '(A)') 'matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
write (*, '(A)') 'Output:'
write (*, '(A)') '[[1, 4, 7], [2, 5, 8], [3, 6, 9]]'
write (*, '(A)') 'Actual:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') 'Expected:'
write (*, '(A)') '[[1, 4, 7], [2, 5, 8], [3, 6, 9]]'
write (*, '(A)') 'Diff:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') '--------------------------------------------------------------------------------'

! Example 2
allocate(matrix(2, 3))
matrix = reshape([1, 2, 3, 4, 5, 6], [2, 3])
write (*, '(A)') 'Example 2'
write (*, '(A)') 'Input:'
write (*, '(A)') 'matrix = [[1, 2, 3], [4, 5, 6]]'
write (*, '(A)') 'Output:'
write (*, '(A)') '[[1, 4], [2, 5], [3, 6]]'
write (*, '(A)') 'Actual:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') 'Expected:'
write (*, '(A)') '[[1, 4], [2, 5], [3, 6]]'
write (*, '(A)') 'Diff:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') '--------------------------------------------------------------------------------'

! Example 3
🌐 Data from online sources
def new21Game(n: int, k: int, maxPts: int) -> float:
    if k == 0 or n >= k + maxPts:
        return 1

    dp = [0] * (n + 1)
    dp[0] = 1
    s, ans = 1, 0

    for i in range(1, n + 1):
        dp[i] = s / maxPts
        if i < k:
            s += dp[i]
        else:
            ans += dp[i]
        if i >= maxPts:
            s -= dp[i - maxPts]

    return ans
1. If Alice has already reached k points or n >= k + maxPts, there is no chance of drawing more cards, so return 1.
  1. Create a dynamic programming array (dp) to store the probability of reaching each point from 0 to n. Initialize dp[0] as 1.
  2. Keep a rolling sum of probabilities and iterate through the range [1, n]. Calculate the probability of reaching points i as the rolling sum divided by maxPts.
  3. If i < k, Alice needs to draw more cards, so update the rolling sum by adding the i-th probability.
  4. If i >= k, Alice already reached the target, so update the answer by adding the i-th probability.
  5. If i >= maxPts, remove the (i-maxPts)-th probability from the rolling sum since it's not in the range of possibilities.
  6. Return the accumulated probability after the iteration.
🌐 Data from online sources
#include <vector>
using namespace std;

double new21Game(int n, int k, int maxPts) {
    if (k == 0 || n >= k + maxPts) {
        return 1;
    }

    vector<double> dp(n + 1, 0);
    dp[0] = 1;
    double sum = 1, ans = 0;

    for (int i = 1; i <= n; ++i) {
        dp[i] = sum / maxPts;
        if (i < k) {
            sum += dp[i];
        } else {
            ans += dp[i];
        }
        if (i >= maxPts) {
            sum -= dp[i - maxPts];
        }
    }

    return ans;
}
1. If Alice has already reached k points or n >= k + maxPts, there is no chance of drawing more cards, so return 1.
  1. Create a dynamic programming array (dp) to store the probability of reaching each point from 0 to n. Initialize dp[0] as 1.
  2. Keep a rolling sum of probabilities and iterate through the range [1, n]. Calculate the probability of reaching points i as the rolling sum divided by maxPts.
  3. If i < k, Alice needs to draw more cards, so update the rolling sum by adding the i-th probability.
  4. If i >= k, Alice already reached the target, so update the answer by adding the i-th probability.
  5. If i >= maxPts, remove the (i-maxPts)-th probability from the rolling sum since it's not in the range of possibilities.
  6. Return the accumulated probability after the iteration.