알고리즘/Lv2. 프로그래머스

[프로그래머스] 의상 -java

signal시노 2023. 8. 17. 17:28

출처 : https://school.programmers.co.kr/learn/courses/30/lessons/42578#qna

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import java.util.*;
class Solution {
    public int solution(String[][] clothes) {
        int answer = 0;
        //종류와 옷의 개수를 담을 map
        Map<String,Integer> map=new HashMap<>();
        for(int i=0;i<clothes.length;i++){
            //같은 종류라면 +1
                map.put(clothes[i][1],map.getOrDefault(clothes[i][1],0)+1);
        }
        int cnt=1;
        for(String clothes1 : map.keySet()){
            //옷의 종류가 하나가 아니라면
            if(map.size()!=1){
            //검은색 선글라스와 흰색 선글라스가 있다면 검은색/흰색/안씀 으로 안쓰는 경우의 수까지 더한 뒤 각각 곱해주기
            cnt*=(map.get(clothes1)+1);
            }
            //옷의 종류가 한가지라면 벨류값만 더하기
            else answer+=map.get(clothes1);
        }
        if(cnt!=1)
            //모두 다 입지 않는 경우의 수를 빼줘야 함으로 -1
        answer+=cnt-1;
        return answer;
    }
}

 

 

설명은 주석에 작성하였습니다.