강의

멘토링

커뮤니티

Inflearn Community Q&A

alswlghddlek4469's profile image
alswlghddlek4469

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

7. Peak

코드 질문이요

Written on

·

259

0

 function solution(arr) {
            let answer = 0;
            let arrLenth = arr.length;
            let test = 0;
            for (let i = 0; i < arrLenth; i++) {
                for (let j = 0; j < arrLenth; j++) {
                    test++;
                    if (arr[i - 1] && arr[i - 1][j] && arr[i - 1][j] > arr[i][j]) continue;
                    if (arr[i + 1] && arr[i + 1][j] && arr[i + 1][j] > arr[i][j]) continue;
                    if (arr[i][j - 1] && arr[i][j - 1] > arr[i][j]) continue;
                    if (arr[i][j + 1] && arr[i][j + 1] > arr[i][j]) continue;
                    answer++;
                }
            }
            console.log(answer);
        }
 
 
별로 안좋은 코드일까요?
javascript코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

영상에서 가르쳐준 대로 dx, dy 배열을 사용하시면 좋을 것 같습니다. 그리고 나중에는 4방향만 아니라 대각선까지 포함해 8방향을 탐색해야 하는 문제도 있습니다.

alswlghddlek4469's profile image
alswlghddlek4469

asked

Ask a question