반응형

배운것 

1. stream 연습~~

- stream은  util에서 퉁치면 안되고 따로 import 해줘야한다.

import java.util.*;
import java.util.stream.*;

 

- distinct() 사용

요소의 중복을 제거.

리턴 타입 : Stream, IntStream, LongStream, DoubleStream 

public class DistinctExample {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 3}; // int[] 배열

        // int[]에서 중복을 제거하고 List<Integer>로 변환
        List<Integer> distinctList = Arrays.stream(nums) // int[]에서 IntStream으로 변환
            .distinct() // 중복 제거
            .boxed() // int를 Integer로 변환
            .collect(Collectors.toList()); // List<Integer>로 수집

        System.out.println(distinctList); // [1, 2, 3] 출력
    }
}

 

- filter() 사용

매개값으로 주어진 Predicate가 true를 리턴하는 요소만 필터링

List<String> nameList = reportList
                                .stream()
                                .filter(rep -> rep.startsWith(id+" "))
                                .map(repp -> repp.split(" ")[1])
                                .collect(Collectors.toList());

 

- count() 사용

요소 개수 반환 

리턴타입 : long

long cnt = nameList
                .stream()
                .filter(reportee -> reporteeCount.getOrDefault(reportee,0) >= k)
                .count();

 

 

2. startsWith(), endsWith()  사용해봄

써본적이 없는데 써봤다.

boolean startsWith(String str)
boolean endsWith(String str)

 

 

두번째 다른방법으로 풀기

다른사람 답안을 보면서 따라해봤다.

제일 오래걸린 시간 : 테스트3 (6000.64ms, 555MB)

뭐야 더오래걸림

report 길이가  1 ~ 200,000 인데 아무리 중복을 제거했어도 두번돌아서 그런가

import java.util.*;
import java.util.stream.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        //1. report에서 중복을 제거
        List<String> reportList = Arrays.stream(report)
            .distinct()
            .collect(Collectors.toList());
        
        //2. id별로 신고당한 횟수를 누적한다.
        HashMap<String,Integer> reporteeCount = new HashMap<>();
        for(String rep : reportList){
            String reportee = rep.split(" ")[1];
            int cnt = reporteeCount.getOrDefault(reportee,0);
            reporteeCount.put(reportee,cnt+1);
        }
        
        //3. id 돌면서 메일 횟수 카운트
        int[] answer = new int[id_list.length];
        answer = Arrays.stream(id_list)
            .mapToInt(id ->{ //id별 처리
                //사용자가 신고한 리스트
                List<String> nameList = reportList
                                .stream()
                                .filter(rep -> rep.startsWith(id+" ")) //비슷한 이름 방지
                                .map(repp -> repp.split(" ")[1])
                                .collect(Collectors.toList());
                
                //사용자가 신고한 리스트에서 k 가 넘는 요소의 개수 산출
                long cnt = nameList.stream()
                                    .filter(reportee -> reporteeCount.getOrDefault(reportee,0) >= k)
                                    .count();
                return (int)cnt;
                }).toArray();

        return answer;  
    }  
}

 

 

 

 

첫번째 풀었던 것

제일 오래걸린시간 : 테스트21 (311.44ms, 164MB)

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        HashMap<String,Set<String>> reporterList = new HashMap<>();
        for(String rep : report){
            String reporter = rep.split(" ")[0]; //신고한사람 muzi
            String reportee = rep.split(" ")[1]; //신고당한사람 frodo
            
            //ID당 유저가 신고한 ID들
            Set<String> temp = reporterList.getOrDefault(reporter,new HashSet<String>());
            temp.add(reportee);
            reporterList.put(reporter,temp);        
        }
        
        //신고당한 id별로 횟수 정리
        HashMap<String,Integer> reporteeCount = new HashMap<>();
        Collection<Set<String>> reportees = reporterList.values();
        //ArrayList<String> bannedIds = new ArrayList<>(); //정지된 id
        for(Set<String> list : reportees){
            for(String name : list){
                int cnt = reporteeCount.getOrDefault(name,0);
                reporteeCount.put(name,cnt+1);
                //if(cnt >= k) bannedIds.add(name);
            }
        }
        
        int[] answer = new int[id_list.length];
        //정지된 신고 리스트 id가 정지되었나 검사
        for(int i=0; i<id_list.length;i++){
            String id = id_list[i];
            Set<String> reportList = reporterList.getOrDefault(id,new HashSet<String>());
            int mailCnt = 0;
            //신고 리스트에 있는 아이디가 정지되었나 검사
            for(String reportee : reportList){
                if(reporteeCount.getOrDefault(reportee,0) >= k ){
                    mailCnt += 1;
                }
            }
            answer[i] = mailCnt;
        }
        
      
        return answer;
    }
    

    

}

//사용자 누적 신고 횟수 k번 이상시 
//동일 유저에 대한 신고 횟수 중복제거 어떻게
//신고자에게 메일

//1. report 돌면서 id별로 누구를 신고했는지 정리(중복x) HashMap<String,Set<>>;
    //예 "muzi" <frodo, apeach>
//2. HashMap 돌면서 id별 신고당한 누적횟수 정리 HashMap, 3회 이상인경우 arraylist에 추가
    //예 "muzi", 3   ["muzi","frodo"]
//3. id list 돌면서 신고list꺼내서 배열에 있는지 확인하여 메일 횟수 ++

 

 

 

반응형

+ Recent posts