Skip to content

Commit 73abf7c

Browse files
committed
[level 2] Title: 의상, Time: 0.11 ms, Memory: 81.9 MB -BaekjoonHub
1 parent a49de17 commit 73abf7c

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [level 2] 의상 - 42578
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/42578)
4+
5+
### 성능 요약
6+
7+
메모리: 81.9 MB, 시간: 0.11 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 해시
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 03월 05일 00:30:35
20+
21+
### 문제 설명
22+
23+
<p>코니는 매일 다른 옷을 조합하여 입는것을 좋아합니다.</p>
24+
25+
<p>예를 들어 코니가 가진 옷이 아래와 같고, 오늘 코니가 동그란 안경, 긴 코트, 파란색 티셔츠를 입었다면 다음날은 청바지를 추가로 입거나 동그란 안경 대신 검정 선글라스를 착용하거나 해야합니다.</p>
26+
<table class="table">
27+
<thead><tr>
28+
<th>종류</th>
29+
<th>이름</th>
30+
</tr>
31+
</thead>
32+
<tbody><tr>
33+
<td>얼굴</td>
34+
<td>동그란 안경, 검정 선글라스</td>
35+
</tr>
36+
<tr>
37+
<td>상의</td>
38+
<td>파란색 티셔츠</td>
39+
</tr>
40+
<tr>
41+
<td>하의</td>
42+
<td>청바지</td>
43+
</tr>
44+
<tr>
45+
<td>겉옷</td>
46+
<td>긴 코트</td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
<ul>
51+
<li>코니는 각 종류별로 최대 1가지 의상만 착용할 수 있습니다. 예를 들어 위 예시의 경우 동그란 안경과 검정 선글라스를 동시에 착용할 수는 없습니다. </li>
52+
<li>착용한 의상의 일부가 겹치더라도, 다른 의상이 겹치지 않거나, 혹은 의상을 추가로 더 착용한 경우에는 서로 다른 방법으로 옷을 착용한 것으로 계산합니다.</li>
53+
<li>코니는 하루에 최소 한 개의 의상은 입습니다.</li>
54+
</ul>
55+
56+
<p>코니가 가진 의상들이 담긴 2차원 배열 clothes가 주어질 때 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성해주세요.</p>
57+
58+
<hr>
59+
60+
<h5>제한사항</h5>
61+
62+
<ul>
63+
<li>clothes의 각 행은 [의상의 이름, 의상의 종류]로 이루어져 있습니다.</li>
64+
<li>코니가 가진 의상의 수는 1개 이상 30개 이하입니다.</li>
65+
<li>같은 이름을 가진 의상은 존재하지 않습니다.</li>
66+
<li>clothes의 모든 원소는 문자열로 이루어져 있습니다.</li>
67+
<li>모든 문자열의 길이는 1 이상 20 이하인 자연수이고 알파벳 소문자 또는 '_' 로만 이루어져 있습니다.</li>
68+
</ul>
69+
70+
<h5>입출력 예</h5>
71+
<table class="table">
72+
<thead><tr>
73+
<th>clothes</th>
74+
<th>return</th>
75+
</tr>
76+
</thead>
77+
<tbody><tr>
78+
<td>[["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]</td>
79+
<td>5</td>
80+
</tr>
81+
<tr>
82+
<td>[["crow_mask", "face"], ["blue_sunglasses", "face"], ["smoky_makeup", "face"]]</td>
83+
<td>3</td>
84+
</tr>
85+
</tbody>
86+
</table>
87+
<h5>입출력 예 설명</h5>
88+
89+
<p>예제 #1<br>
90+
headgear에 해당하는 의상이 yellow_hat, green_turban이고 eyewear에 해당하는 의상이 blue_sunglasses이므로 아래와 같이 5개의 조합이 가능합니다.</p>
91+
<div class="highlight"><pre class="codehilite"><code>1. yellow_hat
92+
2. blue_sunglasses
93+
3. green_turban
94+
4. yellow_hat + blue_sunglasses
95+
5. green_turban + blue_sunglasses
96+
</code></pre></div>
97+
<p>예제 #2<br>
98+
face에 해당하는 의상이 crow_mask, blue_sunglasses, smoky_makeup이므로 아래와 같이 3개의 조합이 가능합니다.</p>
99+
<div class="highlight"><pre class="codehilite"><code>1. crow_mask
100+
2. blue_sunglasses
101+
3. smoky_makeup
102+
</code></pre></div>
103+
<hr>
104+
105+
<p>※ 공지 - 2023년 4월 21일 문제 지문이 리뉴얼되었습니다.</p>
106+
107+
108+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(String[][] clothes) {
5+
int answer = 1; // 각 의상 종류별 선택 가능 개수를 곱하기 위한 변수 초기화
6+
Map<String, Integer> map = new HashMap<String, Integer>(); // 의상 종류별 개수를 저장할 HashMap 선언
7+
8+
// 의상 데이터를 HashMap에 저장
9+
for (String[] cloth : clothes) {
10+
// cloth[1]은 의상의 종류, 해당 종류의 의상 개수를 1 증가시킴
11+
map.put(cloth[1], map.getOrDefault(cloth[1], 0) + 1);
12+
}
13+
14+
// 경우의 수 계산
15+
for (Integer value : map.values()) {
16+
// 해당 종류의 의상을 선택하지 않는 경우(1)를 포함하여 경우의 수를 곱함
17+
answer *= value + 1;
18+
}
19+
20+
// 최소한 하나 이상의 의상을 입어야 하므로 아무것도 입지 않는 경우(1)를 제외하고 반환
21+
return answer - 1;
22+
}
23+
}

0 commit comments

Comments
 (0)