프로그래머스-단체사진 찍기

S_H_H·2023년 6월 16일
0

프로그래머스

목록 보기
12/15

프로그래머스 로고

프로그래머스 - 단체사진 찍기


문제 설명

문제 풀이

풀이 설명

어피치, 콘, 프로도, 제이지 등등 총 8명이 위치할 수 있는 배치의 모든 경우의 수를 만들고 나서
주어진 조건에 따라 소거법으로 진행

코드 작성

        public List<String> allList = new ArrayList<>();

        public int solution(int n, String[] dataList) {
            int answer = 0;
            allList = new ArrayList<>();

            this.permutation("","ACFJMNRT");

            for (String data : dataList) {
                String first = data.substring(0,1);
                String second = data.substring(2,3);
                String compare = data.substring(3,4);
                int count = Integer.parseInt(data.substring(4,5));

                Predicate<String> predicate = null;
                Function<String, Integer> function = (z) -> Math.abs(z.indexOf(first) - z.indexOf(second)) -1;

                if ("=".equals(compare)) predicate = (x -> function.apply(x) == count);
                else if (">".equals(compare)) predicate = (x -> function.apply(x) > count);
                else if ("<".equals(compare)) predicate = (x -> function.apply(x) < count);

                allList = allList.stream().filter(predicate).collect(Collectors.toList());
            }

            answer = allList.size();
            return answer;
        }

        public void permutation(String prefix, String s) {
            int n = s.length();
            if(n==0)
                allList.add(prefix); // all에 삽입
            else {
                for(int i=0; i<n; i++) {
                    permutation(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1));
                }
            }
        }
profile
LEVEL UP

0개의 댓글