본문 바로가기

코딩테스트 연습(with java)/프로그래머스

프로그래머스

ㅠㅠ

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Solution {
    public static int solution(int n, int[] lost, int[] reserve) {
        int answer = 0;
        answer = n-lost.length;
        ArrayList<Integer> arrayLost = new ArrayList<Integer>(Arrays.asList(lost));
        ArrayList<Integer> arrayreserve = new ArrayList<Integer>(Arrays.asList(reserve));
        
        for(int i=0; i<arrayreserve.size(); i++){
            for(int j=0; j<arrayLost.size(); j++){
                if(arrayreserve.get(i)==arrayLost.get(j)-1||arrayreserve.get(i)==arrayLost.get(j)+1){
                    answer++;
                    arrayLost.remove(j);
                }
                else{
                    break;
                }
            }
        }
        return answer;
    }
}