주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.

주어진 2N x 2N 정수 행렬. 행이나 열을 원하는 횟수와 순서로 되돌릴 수 있습니다. 작업은 왼쪽 상단의 최대 합계를 계산하는 것입니다. 엔엑스엔 부분행렬, 즉 (0 0)에서 (N - 1 N - 1)까지의 부분행렬 요소의 합입니다.

예:  

입력 : 와[][] = {



                    112 42 83 119

                    56 125 56 49

                    15 78 101 43

                    62 98 114 108

                  }

출력 : 414

주어진 행렬의 크기는 4 X 4이므로 최대화해야 합니다. 

왼쪽 위 2 X 2 행렬의 합, 즉 

mat[0][0] + mat[0][1] + mat[1][0] + mat[1][1]의 합입니다.

다음 작업은 합계를 최대화합니다.

1. 열 2를 뒤집습니다.

112 42 114 119

56 125 101 49

15 78 56 43

62 98 83 108

2. 역행 0

119 114 42 112

56 125 101 49

15 78 56 43

62 98 83 108

왼쪽 위 행렬의 합 = 119 + 114 + 56 + 125 = 414.

왼쪽 위 부분행렬의 합을 최대화하려면 왼쪽 위 부분행렬의 각 셀에 대해 관찰할 수 있는 4개의 후보가 있습니다. 즉, 교체할 수 있는 왼쪽 위 오른쪽 위 왼쪽 아래 및 오른쪽 아래 부분행렬의 해당 셀을 의미합니다. 

이제 각 셀이 어디에 있든 관찰하여 왼쪽 위 하위 행렬에 있는 다른 셀의 순서를 변경하지 않고 왼쪽 위 하위 행렬에 있는 해당 후보 값으로 바꿀 수 있습니다. 다이어그램은 4개 후보의 최대값이 오른쪽 위 하위 행렬에 있는 인스턴스를 보여줍니다. 그것이 왼쪽 아래 또는 오른쪽 아래 부분행렬에 있는 경우 먼저 행이나 열을 뒤집어 오른쪽 위 부분행렬에 넣은 다음 다이어그램에 표시된 것과 동일한 작업 순서를 따를 수 있습니다. 

이 매트릭스에서 26 후보자는 최대 4명이며, 23 로 교환해야 합니다. 26 왼쪽 위 부분행렬의 셀 순서를 변경하지 않고

행렬

역행 2 
 

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.


역방향 열 2 
 

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.


역행 7 
 

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.


역방향 열 6 
 

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.


역행 2 
 

주어진 2N X 2N 행렬에서 N X N 왼쪽 위 부분 행렬의 합을 최대화합니다.

다음은 이 접근 방식의 구현입니다. 

C++
   // C++ program to find maximum value of top N/2 x N/2   // matrix using row and column reverse operations   #include          #define R 4   #define C 4   using     namespace     std  ;   int     maxSum  (  int     mat  [  R  ][  C  ])   {      int     sum     =     0  ;      for     (  int     i     =     0  ;     i      <     R     /     2  ;     i  ++  )      for     (  int     j     =     0  ;     j      <     C     /     2  ;     j  ++  )     {      int     r1     =     i  ;      int     r2     =     R     -     i     -     1  ;      int     c1     =     j  ;      int     c2     =     C     -     j     -     1  ;      // We can replace current cell [i j]      // with 4 cells without changing affecting      // other elements.      sum     +=     max  (  max  (  mat  [  r1  ][  c1  ]     mat  [  r1  ][  c2  ])      max  (  mat  [  r2  ][  c1  ]     mat  [  r2  ][  c2  ]));      }      return     sum  ;   }   // Driven Program   int     main  ()   {      int     mat  [  R  ][  C  ]      =     {     112       42       83       119       56       125       56       49        15       78       101       43       62       98       114       108     };      cout      < <     maxSum  (  mat  )      < <     endl  ;      return     0  ;   }   
Java
   // Java program to find maximum value of top N/2 x N/2   // matrix using row and column reverse operations   class   GFG     {      static     int     maxSum  (  int     mat  [][]  )      {      int     sum     =     0  ;      int     maxI     =     mat  .  length  ;      int     maxIPossible     =     maxI     -     1  ;      int     maxJ     =     mat  [  0  ]  .  length  ;      int     maxJPossible     =     maxJ     -     1  ;      for     (  int     i     =     0  ;     i      <     maxI     /     2  ;     i  ++  )     {      for     (  int     j     =     0  ;     j      <     maxJ     /     2  ;     j  ++  )     {      // We can replace current cell [i j]      // with 4 cells without changing affecting      // other elements.      sum     +=     Math  .  max  (      Math  .  max  (  mat  [  i  ][  j  ]        mat  [  maxIPossible     -     i  ][  j  ]  )      Math  .  max  (  mat  [  maxIPossible     -     i  ]      [  maxJPossible     -     j  ]        mat  [  i  ][  maxJPossible     -     j  ]  ));      }      }      return     sum  ;      }      // Driven Program      public     static     void     main  (  String  []     args  )      {      int     mat  [][]     =     {     {     112       42       83       119     }      {     56       125       56       49     }      {     15       78       101       43     }      {     62       98       114       108     }     };      System  .  out  .  println  (  maxSum  (  mat  ));      }   }   /* This Java code is contributed by Rajput-Ji*/   
Python3
   # Python3 program to find the maximum value   # of top N/2 x N/2 matrix using row and   # column reverse operations   def   maxSum  (  mat  ):   Sum   =   0   for   i   in   range  (  0     R   //   2  ):   for   j   in   range  (  0     C   //   2  ):   r1     r2   =   i     R   -   i   -   1   c1     c2   =   j     C   -   j   -   1   # We can replace current cell [i j]   # with 4 cells without changing/affecting   # other elements.   Sum   +=   max  (  max  (  mat  [  r1  ][  c1  ]   mat  [  r1  ][  c2  ])   max  (  mat  [  r2  ][  c1  ]   mat  [  r2  ][  c2  ]))   return   Sum   # Driver Code   if   __name__   ==   '__main__'  :   R   =   C   =   4   mat   =   [[  112     42     83     119  ]   [  56     125     56     49  ]   [  15     78     101     43  ]   [  62     98     114     108  ]]   print  (  maxSum  (  mat  ))   # This code is contributed   # by Rituraj Jain   
C#
   // C# program to find maximum value   // of top N/2 x N/2 matrix using row   // and column reverse operations   using     System  ;   class     GFG     {      static     int     R     =     4  ;      static     int     C     =     4  ;      static     int     maxSum  (  int  [     ]     mat  )      {      int     sum     =     0  ;      for     (  int     i     =     0  ;     i      <     R     /     2  ;     i  ++  )     {      for     (  int     j     =     0  ;     j      <     C     /     2  ;     j  ++  )     {      int     r1     =     i  ;      int     r2     =     R     -     i     -     1  ;      int     c1     =     j  ;      int     c2     =     C     -     j     -     1  ;      // We can replace current cell [i j]      // with 4 cells without changing affecting      // other elements.      sum     +=     Math  .  Max  (      Math  .  Max  (  mat  [  r1       c1  ]     mat  [  r1       c2  ])      Math  .  Max  (  mat  [  r2       c1  ]     mat  [  r2       c2  ]));      }      }      return     sum  ;      }      // Driven Code      public     static     void     Main  ()      {      int  [     ]     mat     =     {     {     112       42       83       119     }      {     56       125       56       49     }      {     15       78       101       43     }      {     62       98       114       108     }     };      Console  .  Write  (  maxSum  (  mat  ));      }   }   // This code is contributed   // by ChitraNayal   
PHP
      // PHP program to find maximum value    // of top N/2 x N/2 matrix using row    // and column reverse operations   function   maxSum  (  $mat  )   {   $R   =   4  ;   $C   =   4  ;   $sum   =   0  ;   for   (  $i   =   0  ;   $i    <   $R   /   2  ;   $i  ++  )   for   (  $j   =   0  ;   $j    <   $C   /   2  ;   $j  ++  )   {   $r1   =   $i  ;   $r2   =   $R   -   $i   -   1  ;   $c1   =   $j  ;   $c2   =   $C   -   $j   -   1  ;   // We can replace current cell [i j]   // with 4 cells without changing    // affecting other elements.   $sum   +=   max  (  max  (  $mat  [  $r1  ][  $c1  ]   $mat  [  $r1  ][  $c2  ])   max  (  $mat  [  $r2  ][  $c1  ]   $mat  [  $r2  ][  $c2  ]));   }   return   $sum  ;   }   // Driver Code   $mat   =   array  (  array  (  112     42     83     119  )   array  (  56     125     56     49  )   array  (  15     78     101     43  )   array  (  62     98     114     108  ));   echo   maxSum  (  $mat  )   .   '  n  '  ;   // This code is contributed   // by Mukul Singh   ?>   
JavaScript
    <  script  >   // Javascript program to find maximum value of top N/2 x N/2   // matrix using row and column reverse operations          let     R     =     4  ;      let     C     =     4  ;          function     maxSum  (  mat  )      {      let     sum     =     0  ;          for     (  let     i     =     0  ;     i      <     R     /     2  ;     i  ++  )     {      for     (  let     j     =     0  ;     j      <     C     /     2  ;     j  ++  )     {      let     r1     =     i  ;      let     r2     =     R     -     i     -     1  ;      let     c1     =     j  ;      let     c2     =     C     -     j     -     1  ;          // We can replace current cell [i j]      // with 4 cells without changing affecting      // other elements.      sum     +=     Math  .  max  (  Math  .  max  (  mat  [  r1  ][  c1  ]     mat  [  r1  ][  c2  ])      Math  .  max  (  mat  [  r2  ][  c1  ]     mat  [  r2  ][  c2  ]));      }      }          return     sum  ;      }      // Driven Program      let     mat     =     [[  112       42       83       119  ]         [  56       125       56       49  ]         [  15       78       101       43  ]         [  62       98       114       108  ]];      document  .  write  (  maxSum  (  mat  ));          // This code is contributed by avanitrachhadiya2155    <  /script>   

산출
414 

시간 복잡도: O(N 2 ).
보조 공간: O(1) 변수에 일정한 공간을 사용하기 때문에

 

퀴즈 만들기

인기 기사

범주

재미있는 기사