IT 개발자_S

[프로그래머스] 위장 본문

카테고리 없음

[프로그래머스] 위장

Soso12 2022. 3. 25. 11:13
반응형

● 해쉬를 사용하요 알고리즘을 만들 수 있다.

https://programmers.co.kr/learn/courses/30/lessons/42578

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

 

접근방법

1. 해쉬맵을 사용하여 중복을 제거하고 

2. 해당 의상의종류별로 cnt 를하여 

3. 경우의수를 조합 의상종류별 *, 각 의상종류별의 합

 

import java.util.*;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 0;
        answer = process(clothes);
        return answer;
    
    }
    
    public int process(String[][] clothes){
        
        int result = 0;
        int mapCnt = 0;
        int forCnt = 1;
        int loopCnt = 0;
        HashMap<String, Integer> map = new HashMap<>();

        for(int i = 0; i <=clothes[0].length; i++){
            System.out.println("@" + " " + clothes[i][1]) ;
            if(map.get(clothes[i][1])!= null){

                 map.put(clothes[i][1] , map.get(clothes[i][1]) +1);
            }else{
                
                map.put(clothes[i][1] , 1);
            }

            
            
        }
        
        for(String key : map.keySet()){
            System.out.println("$" + " " + key + " " + map.get(key)) ; 
            forCnt = forCnt* map.get(key);
            mapCnt = mapCnt+ map.get(key);
            loopCnt++;

        }
        
        System.out.print(mapCnt + " " + forCnt) ;
        if (loopCnt == 1){
            mapCnt = 0;
        }
            
        result = mapCnt + forCnt;
        return result;
    }
}

 

반응형
Comments