본문 바로가기
알고리즘

[Algorithm] 6월8일 알고리즘 연습 (프로그래머스 level2) - 에이젠

by eigen96 2022. 6. 8.
728x90

Lv. 2 위장 - 성공

 

테스트케이스를 통과해도 채점결과가 처참한 경우가 많은 것 같다...

 

내풀이(1) : 정확도 25% : 한가지 or 모든 악세사리를 착용하는 경우만 생각함. 세가지 카테고리중 두개만 선택하는 경우 같은 케이스를 배제하는 실수.

해결 : 각각의 카테고리 마다 케이스는 카테고리개수 중 하나를 선택 OR 선택하지 않는다의 경우의수가 존재.

모두 곱한 후 아무선택도 하지 않는 경우를 배제.

class Solution {
    public int solution(String[][] clothes) {
        int answer = 0;
        
        Map<String, Integer> map = new HashMap<String, Integer>();
        
        for(int i = 0; i< clothes.length ; i++){
            
            if(map.containsKey(clothes[i][1])){
                int numbering  = map.get(clothes[i][1]);
                map.put(clothes[i][1],numbering + 1);
            }else{
                map.put(clothes[i][1],1);
            }
                    
        }
        ArrayList<Integer> list = new ArrayList<Integer>();
        int total = 0;
        int cross = 1;
        for(String key : map.keySet()){
            total = total + map.get(key);
            list.add(map.get(key));
        }
        if(list.size() == 1){
            cross = 0;
        }else{
            for(int a : list){
                cross = cross*a;
            }
        }
        answer = cross + total;
        return answer;
    } 
}

내풀이(2) : 정확도 100%

class Solution {
    public int solution(String[][] clothes) {
        int answer = 0;
        
        Map<String, Integer> map = new HashMap<String, Integer>();
        
        for(int i = 0; i< clothes.length ; i++){
            
            if(map.containsKey(clothes[i][1])){
                int numbering  = map.get(clothes[i][1]);
                map.put(clothes[i][1],numbering + 1);
                
            }else{
                map.put(clothes[i][1],1);
            }
                    
        }
        int total = 1;
        for(String testCase : map.keySet()){
            int number = map.get(testCase);
            
            total = total * (number + 1);
        }
        answer = total-1;
        return answer;
    } 
}

 

728x90

댓글