강의

멘토링

커뮤니티

Inflearn Community Q&A

lucky9512182542's profile image
lucky9512182542

asked

Introduction to Java Algorithm Problem Solving: Coding Test Preparation

10. Peak

오답질문

Written on

·

152

0

어떤 이유로 오답이 출력되는지 잘모르겠습니다..ㅜ
 
	public int solution(int n, int[][] arr) {
		int answer = 0;
		int a[] = {-1, 0, 1, 0};
		int b[] = {0, 1, 0, -1};
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				for(int k=0; k<4; k++) {
					int nx = i + a[k];
					int ny = j + b[k];
					if(nx>=0 && nx<n && ny>=0 && ny<n && arr[i][j]>arr[nx][ny]) {
						answer++;
					}
					else {
						continue;
					}
				}
			}
		}
		return answer;
	}
java코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

if(nx>=0 && nx<n && ny>=0 && ny<n && arr[i][j]>arr[nx][ny]) {

위에 조건문이 k for 문을 반복하는 동안 즉 4번이 모두 참이어야 answer를 한 번 증가시켜주는 겁니다.

 

 

lucky9512182542's profile image
lucky9512182542

asked

Ask a question