본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] 행렬의 곱셈

by 현장 2026. 1. 2.

-Code

class Solution {
    public int[][] solution(int[][] arr1, int[][] arr2) {
        // 결과로 반환할 배열 선언
        int totalRow = arr1.length;
        int totalCol = arr2[0].length;
        int[][] answer = new int[totalRow][totalCol];
        // 계산시 사용하는 arr1은 col부분, arr2는 row부분 횟수
        int calcCnt = arr1[0].length;
        
        for (int row = 0; row < totalRow; row++) {
            for (int col = 0; col < totalCol; col++) {
                // arr1의 가로와 arr2의 세로를 곱한후 합
                for(int calc = 0; calc < calcCnt; calc++) {
                    answer[row][col] += arr1[row][calc] * arr2[calc][col];
                }
            }
        }
        
        return answer;
    }
}