개발/코딩

[프로그래밍] 프로그래머스 예제문제 풀어보기 <신고 결과 받기> 재도전2(다시 66.7...)

mabb 2022. 3. 12. 18:06
반응형

 이번에는 HashMap을 이용하여 id=number 를 매칭하여 활용하여봄.

     if(thisMemberReportMap.size() >=k ){
                for(String thisMemberReporter: thisMemberReportMap.keySet()){       
                    answer[idNumberMap.get(thisMemberReporter)]++;
                }

 

            }
굵게 친 부분이 제대로 작동하여 짜릿하였으나 여전히 시간초과로 66.7초
 
출처: 프로그래머스 채점결과

도대체 3번 테스트는 어떤 인자를 제공하길래 시간초과가 계속해서 나오는 것인지..

            

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.Collections;
 
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        
        int[] answer = new int[id_list.length];
        Map<String,Integer> idNumberMap = new HashMap<>();
        for(int n = 0; n<id_list.length; n++){
            idNumberMap.put(id_list[n],n);
        }
        
 
        for(String member: id_list){
            Map<String,String> thisMemberReportMap = new HashMap<>();
            Map<String,Integer> succeessReporterMap = new HashMap<>();
            int successCount = 0;
            for(String reportRec:report){
                String reporter = reportRec.split(" ")[0];
                String defendant = reportRec.split(" ")[1];
                
                if(member.equals(defendant)){
                  thisMemberReportMap.put(reporter,member);
                }                
            }
            
            if(thisMemberReportMap.size() >=k ){
                for(String thisMemberReporter: thisMemberReportMap.keySet()){
                    
                    answer[idNumberMap.get(thisMemberReporter)]++;
                }
                
            }
            
        }
     
            
                
                
        return answer;
    }
}
cs

 

코드는 조금 짧아진 것 같으나 속도는 여전함..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
 
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        
        int[] answer = new int[id_list.length];
        Map<String,Integer> idNumberMap = new HashMap<>();
        
        for(int n =0; n< id_list.length; n++){
            idNumberMap.put(id_list[n],n);
        }
        for(String member: id_list) {
            Set<String> thisMemberReportRecord = new HashSet<>();
            
            for(String record:report) {
                String repoter= record.split(" ")[0];
                String defendant = record.split(" ")[1];
                
                if(member.equals(defendant)) {
                    thisMemberReportRecord.add(repoter);    
                }
            }
            if(thisMemberReportRecord.size()>=k) {
                for(String TMR:thisMemberReportRecord) {                    
                    answer[idNumberMap.get(TMR)]++;                                        
                }
            }
        }                                      
        return answer;
    }
}
cs
반응형