220116_알고리즘

Minseok-Choi·2022년 1월 16일
0

알고리즘

목록 보기
6/13
post-thumbnail

프로그래머스 신고 결과 받기

  • level1 신고 결과 받기
  • 객체를 만들어서 구현해보려고 했다.
  • 해결하고 난 뒤, 굉장히 단순한 방법이 떠올랐다.
  • 단순히 Map<String, List<String>> reportMap 이거 하나만으로도 해결할 수 있지 않았을까?
  • 간략한 구현방법에 대해서만 기록해둔 뒤 내일 구현하고 글을 수정해야겠다.
import java.util.*;


class Solution {
    public int[] solution(String[] idList, String[] report, int k) {
        report = Arrays.asList(report).stream().distinct().toArray(String[]::new);
        List<Id> ids = generateIds(idList);
        report(ids,report);
        checkSuspended(ids,k);
        sendMails(ids);

        int[] answer = ids.stream().mapToInt(id-> id.mails).toArray();
        return answer;
    }

    public static class Id {
        String name;
        int reportedCount;
        Set<String> reportedBy;
        boolean suspended;
        int mails;

        Id(String name) {
            reportedCount = 0;
            this.name = name;
            reportedBy = new HashSet<>();
            suspended = false;
            mails = 0;
        }

        void reportBy(String id) {
            reportedBy.add(id);
            reportedCount++;
        }

       void isSuspended(int k) {
            if(k <= reportedCount) {
                suspended = true;
            }
        }

        void getMails(Set<String> ids) {
            for (String id : ids) {
                if(id.equals(this.name)) {
                    mails++;
                }
            }
        }
    }

    public static List<Id> generateIds(String[] idList) {
        List<Id> ids = new ArrayList<>();
        for (String id : idList) {
            ids.add(new Id(id));
        }
        return ids;
    }

    private static Id getId(List<Id> ids, String id) {
        for (Id id1 : ids) {
            if(id1.name.equals(id)) {
                return id1;
            }
        }
        return null;
    }

    public static void report(List<Id> ids, String[] reportInfo) {
        for (String report : reportInfo) {
            String[] reportCase = report.split(" ");
            Id reported = getId(ids,reportCase[1]);
            reported.reportBy(reportCase[0]);
        }
    }

    public static void checkSuspended(List<Id> ids,int k) {
        for (Id id : ids) {
            id.isSuspended(k);
        }
    }

   public static void sendMails(List<Id> ids) {
        for (Id id : ids) {
            if(id.suspended == true) {
                Set<String> temp = id.reportedBy;

                for (Id id1 : ids) {
                    id1.getMails(temp);
                }
            }
        }
    }

}
profile
차곡차곡

0개의 댓글

관련 채용 정보