[프로그래머스] 체육복 12244 (JAVA)

dia·2023년 11월 14일
0

풀이 방식

  1. 주어진 배열 정렬
  2. 잃어버린 사람 중 여벌 옷이 있는지 우선 검사
  3. 잃어버린 사람의 앞뒷번호에 여벌 옷을 가진 사람이 있는지 검사

포인트

카운트 방식

  1. 전체 인원 - 잃어버린 인원
  2. 여벌 옷이 있는 사람 다시 추가
  3. 빌릴 수 있는 사람 다시 추가

-1의 의미

두 배열(목록)의 요소는 학생의 번호를 나타냄
번호를 -1로 만듦으로서 배열(목록)에서 제외


구현

public class NUM42862 {
    public int solution(int n, int[] lost, int[] reserve) {

        int answer = 0;
        answer = n-lost.length;

        Arrays.sort(lost);
        Arrays.sort(reserve);

        for(int i=0; i<lost.length; i++){
            for(int j=0; j<reserve.length; j++){
                if(reserve[j]==lost[i]){
                    answer++;
                    lost[i] =-1;
                    reserve[j] =-1;
                    break;
                }
            }
        }

        for(int i=0; i<lost.length; i++){
            for(int j=0; j<reserve.length; j++){
                if(reserve[j]==lost[i]-1 || reserve[j]==lost[i]+1){
                    answer++;
                    lost[i]=-1;
                    reserve[j]=-1;
                    break;
                }
            }
        }

        return answer;
    }
}

*다른 분들의 코드를 참고하여 작성했습니다

profile
자기 자신을 위한 CS 메모장 (그림은 지인분이 그려주신 것!)

1개의 댓글

comment-user-thumbnail
2023년 11월 14일

유익한 자료 감사합니다.

답글 달기