์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ๋ด‰์šฐ๋ฆฌ

2023. 10. 12. 14:58ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1217

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ๊ฒฉ์žํŒ ์ตœ๋Œ€ํ•ฉ

https://hyejin.tistory.com/1216 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ๋“ฑ์ˆ˜ ๊ตฌํ•˜๊ธฐ https://hyejin.tistory.com/1215 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ์ ์ˆ˜ ๊ณ„์‚ฐ https

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

 

10. ๋ด‰์šฐ๋ฆฌ 

 

์„ค๋ช…

์ง€๋„ ์ •๋ณด๊ฐ€ N*N ๊ฒฉ์žํŒ์— ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ๊ฒฉ์ž์—๋Š” ๊ทธ ์ง€์—ญ์˜ ๋†’์ด๊ฐ€ ์“ฐ์—ฌ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ ๊ฒฉ์žํŒ์˜ ์ˆซ์ž ์ค‘ ์ž์‹ ์˜ ์ƒํ•˜์ขŒ์šฐ ์ˆซ์ž๋ณด๋‹ค ํฐ ์ˆซ์ž๋Š” ๋ด‰์šฐ๋ฆฌ ์ง€์—ญ์ž…๋‹ˆ๋‹ค. ๋ด‰์šฐ๋ฆฌ ์ง€์—ญ์ด ๋ช‡ ๊ฐœ ์žˆ๋Š” ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๊ฒฉ์ž์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™” ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

๋งŒ์•ฝ N=5 ์ด๊ณ , ๊ฒฉ์žํŒ์˜ ์ˆซ์ž๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด ๋ด‰์šฐ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋Š” 10๊ฐœ์ž…๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค.(2<=N<=50)

๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N์ค„์— ๊ฑธ์ณ ๊ฐ ์ค„์— N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ž์—ฐ์ˆ˜๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

 

์ถœ๋ ฅ

๋ด‰์šฐ๋ฆฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

5
5 3 7 2 3
3 7 1 6 1
7 2 5 3 4
4 3 6 4 1
8 7 3 5 2

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

10

 

 

๋ฌธ์ œ ํ’€์ด 1

public int solution(int n, int[][] arr)
{
   int answer = 0;
   int[][] tmp = new int[n + 2][n + 2];
   
   for (int i = 0; i < tmp.length; i++)
   {
      for (int j = 0; j < tmp[i].length; j++)
      {
         if (i == 0 || j == 0 || i == tmp.length - 1 || j == tmp.length - 1) {
            tmp[i][j] = 0;
         }else {
            tmp[i][j] = arr[i - 1][j - 1];
         }
      }
   }
   
   for (int i = 0; i < tmp.length; i++)
   {
      for (int j = 0; j < tmp[i].length; j++)
      {
         if (!(i == 0 || j == 0 || i == tmp.length - 1 || j == tmp.length - 1))
         {
            if ((tmp[i][j] > tmp[i][j - 1]) && (tmp[i][j] > tmp[i - 1][j]) && (tmp[i][j] > tmp[i + 1][j]) && (tmp[i][j] > tmp[i][j + 1]))
            {
               answer++;
            }
         }
      }
   }
   
   return answer;
}

๐Ÿ‘ฉ‍๐Ÿ’ป : ๋‚˜๋Š”.. ์šฐ์„  ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ tmp ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์—ˆ๋‹ค. ๋์€ ๋ชจ๋‘ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , ๊ทธ ์•ˆ์— ๊ฐ’์„ arr์˜ ๋ฐฐ์—ด ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋„ฃ์–ด์คฌ๋‹ค. 

 

๊ทธ ๋‹ค์Œ.. ๋์˜ ๋ถ€๋ถ„๋“ค์ด ์•„๋‹ˆ๋ผ๋ฉด

if (!(i == 0 || j == 0 || i == tmp.length - 1 || j == tmp.length - 1))

tmp[i][j]์˜ ๊ฐ’์„ ๋™์„œ๋‚จ๋ถ์œผ๋กœ ๋น„๊ตํ•ด์„œ tmp[i][j]๊ฐ’์ด ๊ฐ€์žฅ ํฌ๋‹ค๋ฉด answer์— +1 ํ•˜๋„๋ก ํ–ˆ๋‹ค..! 

 

-> ์ด ๋ฌธ์ œ๋Š” if ๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ.. ๋งŒ์•ฝ ๋™์„œ๋‚จ๋ถ์ด ์•„๋‹ˆ๋ผ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋น„๊ตํ•˜๋ผ.ํ–ˆ์œผ๋ฉด 8๋ฒˆ์˜ ๋น„๊ต๋ฅผ ํ–ˆ์–ด์•ผ ํ•˜๋Š”๋ฐ 

๊ต‰์žฅํžˆ ์ฝ”๋“œ๋„ ๋”๋Ÿฝ๊ณ ... ๋ณ„๋กœ์ผ ๊ฒƒ์ด๋‹ค.. 

 

 

 

๋ฌธ์ œ ํ’€์ด 2

int[] dx = { -1, 0, 1, 0 };
int[] dy = { 0, 1, 0, -1 };
public int solution2(int n, int[][] arr)
{
   int answer = 0;
   
   for (int i = 0; i < n; i++)
   {
      for (int j = 0; j < n; j++)
      {
         boolean flag = true;
         for (int k = 0; k < 4; k++)
         {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if ( nx >= 0 && nx < n && ny >= 0 && ny < n && arr[nx][ny] > arr[i][j]) {
               flag = false;
               break;
            }
         }
         if (flag)
            answer++;
      }
   }
   
   return answer;
}

๐Ÿ‘พ : ๊ทธ๋ž˜์„œ ๊ฐ•์‚ฌ๋‹˜์€ 

int[] dx = { -1, 0, 1, 0 };
int[] dy = { 0, 1, 0, -1 };

-> ๋™์„œ๋‚จ๋ถ์— ๊ฐ๊ฐ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋‘๊ณ , 

3์ค‘ for๋ฌธ์„ ๋Œ๊ฒŒ ํ–ˆ๋‹ค. k = 0  i + dx[0],  j + dy[0] ๋ฅผ ํ•˜๋ฉด '๋ถ' ๊ฐ’์— ํ•ด๋‹นํ•˜๊ณ , k = 1 -> i + dx[1], j + dy[1] ์€ '๋™'๊ฐ’์— ํ•ด๋‹นํ•œ๋‹ค. 

 

์ด๋•Œ 

int nx = i + dx[k];
int ny = j + dy[k];

nx, ny ๊ฐ’์ด 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์•„์•ผ ํ•˜๊ณ , n ๋ณด๋‹ค๋Š” ์ž‘์•„์•ผ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋”ฐ๋ผ์„œ arr[i][j]์˜ ๊ฐ’์ด arr[i +dx[k]][j+dy[k]] ๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๋ง์€ ๋ด‰์šฐ๋ฆฌ๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ boolean flag ๊ฐ’์„ false๋กœ ์ง€์ •ํ•ด์ค€๋‹ค. ๊ทธ ๋‹ค์Œ ๋”์ด์ƒ ๋น„๊ตํ•  ํ•„์š” ์—†์œผ๋‹ˆ break ํ•ด์ฃผ๊ณ ,, 

 

๋น„๊ต ํ›„ flag ๊ฐ’์ด true์ด๋ฉด answer +1 ํ•ด์ค€๋‹ค. 

 

 

 

728x90