[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Lv2. ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ

2023. 2. 21. 11:37ใ†์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_2023

728x90

 

๋ฌธ์ œ ์„ค๋ช…

2์ฐจ์› ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, arr1์— arr2๋ฅผ ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 
์ œํ•œ ์กฐ๊ฑด
  • ํ–‰๋ ฌ arr1, arr2์˜ ํ–‰๊ณผ ์—ด์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ํ–‰๋ ฌ arr1, arr2์˜ ์›์†Œ๋Š” -10 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ๊ณฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
 
์ž…์ถœ๋ ฅ ์˜ˆ
arr1                                                   arr2                                                   return
[[1, 4], [3, 2], [4, 1]] [[3, 3], [3, 3]] [[15, 15], [15, 15], [15, 15]]
[[2, 3, 2], [4, 2, 4], [3, 1, 4]] [[5, 4, 3], [2, 4, 1], [3, 1, 1]] [[22, 22, 11], [36, 28, 18], [29, 20, 14]]

 

 

๋‚˜์˜ ํ’€์ด

public int[][] solution(int[][] arr1, int[][] arr2) {
   int[][] answer = new int[arr1.length][arr2[0].length];
   
   for (int i = 0; i < arr1.length; i++)
   {
      for (int j = 0; j < arr2[0].length; j++)
      {
         for (int k = 0; k < arr1[0].length; k++)
         {
            answer[i][j] += arr1[i][k] * arr2[k][j];
         }
      }
   }
   
   return answer;
}

 

์ฒซ๋ฒˆ์งธ ์˜ˆ์ œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด 

answer[0][0] = arr1[0][0] * arr2[0][0] + arr1[0][1] * arr2[1][0] , answer[0][1] = arr1[0][0] * arr2[0][1] + arr1[0][1] * arr2[1][1] 

answer[1][0] = arr1[1][0] * arr2[0][0] + arr1[1][1] * arr2[1][0] , answer[1][1] = arr1[1][0] * arr2[0][1] + arr1[1][1] * arr2[1][1]

answer[2][0] = arr1[2][0] * arr2[0][0] + arr1[2][1] * arr2[1][0] , answer[2][1] = arr1[2][0] * arr2[0][1] + arr1[2][1] * arr2[1][1] 

 

์ด๋ฅผ ๊ณต์‹ํ™” ํ•˜๋ฉด 

answer[i][j] += arr1[i][k] * arr2[k][j] ๋กœ ์‚ผ์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90