강의

멘토링

커뮤니티

Inflearn Community Q&A

sdag36921206's profile image
sdag36921206

asked

Introduction to Java Algorithm Problem Solving: Coding Test Preparation

11. Choosing a Temporary Class President

문제에서 중복된 값이 있으면 가장 작은 수를 리턴하라고 하는데

Written on

·

268

1

문제에서 중복된 값이 있으면 가장 작은 수를 리턴하라고 하는데

이걸 만족하려면, 가장 앞 for문의 i 를 역으로 계산해야하지 않나요? 

for(int i = n; i >=1; i--) 이렇게요.

코테 준비 같이 해요! java

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

for(int j=1; j<=n; j++) 반복문이 끝나고

if(cnt>max){
	max=cnt;
	answer=i;
}

위에 코드를 할 때 if(cnt>max) 에서 cnt가 max 보다 클때만 answer 값을 i로 바꾸기 때문에 작은 번호를 계속유지합니다.

만약 if(cnt>=max) 라 했다면 같은 값일 때도 바꾸니까 기존 작은 번호를 나중에 처리하는 큰 번호의 i값으로 바꿔버리겠죠.

둘의 차이를 한 번 생각해보세요.

sdag36921206's profile image
sdag36921206

asked

Ask a question