Skip to content

Commit 5a9f6b0

Browse files
committed
[level 3] Title: [카카오 인턴] 보석 쇼핑, Time: 76.33 ms, Memory: 81.4 MB -BaekjoonHub
1 parent 68132a3 commit 5a9f6b0

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# [level 3] [카카오 인턴] 보석 쇼핑 - 67258
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/67258)
4+
5+
### 성능 요약
6+
7+
메모리: 81.4 MB, 시간: 76.33 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2020 카카오 인턴십
12+
13+
### 채점결과
14+
15+
정확성: 33.3<br/>효율성: 66.7<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 04월 23일 16:36:55
20+
21+
### 문제 설명
22+
23+
<p><strong>[본 문제는 정확성과 효율성 테스트 각각 점수가 있는 문제입니다.]</strong></p>
24+
25+
<p>개발자 출신으로 세계 최고의 갑부가 된 <code>어피치</code>는 스트레스를 받을 때면 이를 풀기 위해 오프라인 매장에 쇼핑을 하러 가곤 합니다.<br>
26+
어피치는 쇼핑을 할 때면 매장 진열대의 특정 범위의 물건들을 모두 싹쓸이 구매하는 습관이 있습니다.<br>
27+
어느 날 스트레스를 풀기 위해 보석 매장에 쇼핑을 하러 간 어피치는 이전처럼 진열대의 특정 범위의 보석을 모두 구매하되 특별히 아래 목적을 달성하고 싶었습니다.<br>
28+
<code>진열된 모든 종류의 보석을 적어도 1개 이상 포함하는 가장 짧은 구간을 찾아서 구매</code></p>
29+
30+
<p>예를 들어 아래 진열대는 4종류의 보석(RUBY, DIA, EMERALD, SAPPHIRE) 8개가 진열된 예시입니다.</p>
31+
<table class="table">
32+
<thead><tr>
33+
<th>진열대 번호</th>
34+
<th>1</th>
35+
<th>2</th>
36+
<th>3</th>
37+
<th>4</th>
38+
<th>5</th>
39+
<th>6</th>
40+
<th>7</th>
41+
<th>8</th>
42+
</tr>
43+
</thead>
44+
<tbody><tr>
45+
<td>보석 이름</td>
46+
<td>DIA</td>
47+
<td>RUBY</td>
48+
<td><strong>RUBY</strong></td>
49+
<td><strong>DIA</strong></td>
50+
<td><strong>DIA</strong></td>
51+
<td><strong>EMERALD</strong></td>
52+
<td><strong>SAPPHIRE</strong></td>
53+
<td>DIA</td>
54+
</tr>
55+
</tbody>
56+
</table>
57+
<p>진열대의 3번부터 7번까지 5개의 보석을 구매하면 모든 종류의 보석을 적어도 하나 이상씩 포함하게 됩니다. </p>
58+
59+
<p>진열대의 3, 4, 6, 7번의 보석만 구매하는 것은 중간에 특정 구간(5번)이 빠지게 되므로 어피치의 쇼핑 습관에 맞지 않습니다.</p>
60+
61+
<p>진열대 번호 순서대로 보석들의 이름이 저장된 배열 gems가 매개변수로 주어집니다. 이때 모든 보석을 하나 이상 포함하는 가장 짧은 구간을 찾아서 return 하도록 solution 함수를 완성해주세요.<br>
62+
가장 짧은 구간의 <code>시작 진열대 번호</code>와 <code>끝 진열대 번호</code>를 차례대로 배열에 담아서 return 하도록 하며, 만약 가장 짧은 구간이 여러 개라면 <code>시작 진열대 번호</code>가 가장 작은 구간을 return 합니다.</p>
63+
64+
<h5><strong>[제한사항]</strong></h5>
65+
66+
<ul>
67+
<li>gems 배열의 크기는 1 이상 100,000 이하입니다.
68+
69+
<ul>
70+
<li>gems 배열의 각 원소는 진열대에 나열된 보석을 나타냅니다.</li>
71+
<li>gems 배열에는 1번 진열대부터 진열대 번호 순서대로 보석이름이 차례대로 저장되어 있습니다.</li>
72+
<li>gems 배열의 각 원소는 길이가 1 이상 10 이하인 알파벳 대문자로만 구성된 문자열입니다.</li>
73+
</ul></li>
74+
</ul>
75+
76+
<hr>
77+
78+
<h5><strong>입출력 예</strong></h5>
79+
<table class="table">
80+
<thead><tr>
81+
<th>gems</th>
82+
<th>result</th>
83+
</tr>
84+
</thead>
85+
<tbody><tr>
86+
<td><code>["DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"]</code></td>
87+
<td>[3, 7]</td>
88+
</tr>
89+
<tr>
90+
<td><code>["AA", "AB", "AC", "AA", "AC"]</code></td>
91+
<td>[1, 3]</td>
92+
</tr>
93+
<tr>
94+
<td><code>["XYZ", "XYZ", "XYZ"]</code></td>
95+
<td>[1, 1]</td>
96+
</tr>
97+
<tr>
98+
<td><code>["ZZZ", "YYY", "NNNN", "YYY", "BBB"]</code></td>
99+
<td>[1, 5]</td>
100+
</tr>
101+
</tbody>
102+
</table>
103+
<h5><strong>입출력 예에 대한 설명</strong></h5>
104+
105+
<p><strong>입출력 예 #1</strong><br>
106+
문제 예시와 같습니다.</p>
107+
108+
<p><strong>입출력 예 #2</strong><br>
109+
3종류의 보석(AA, AB, AC)을 모두 포함하는 가장 짧은 구간은 [1, 3], [2, 4]가 있습니다. <br>
110+
<code>시작 진열대 번호</code>가 더 작은 [1, 3]을 return 해주어야 합니다.</p>
111+
112+
<p><strong>입출력 예 #3</strong><br>
113+
1종류의 보석(XYZ)을 포함하는 가장 짧은 구간은 [1, 1], [2, 2], [3, 3]이 있습니다. <br>
114+
<code>시작 진열대 번호</code>가 가장 작은 [1, 1]을 return 해주어야 합니다.</p>
115+
116+
<p><strong>입출력 예 #4</strong><br>
117+
4종류의 보석(ZZZ, YYY, NNNN, BBB)을 모두 포함하는 구간은 [1, 5]가 유일합니다.<br>
118+
그러므로 [1, 5]를 return 해주어야 합니다.</p>
119+
120+
<p>※ 공지 - 2020년 7월 21일 테스트케이스가 추가되었습니다.</p>
121+
122+
123+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
int n, minLen;
5+
int[] gems;
6+
Map<String, Integer> types;
7+
Map<String, Integer> cnt;
8+
public int[] solution(String[] gems) {
9+
int[] answer = new int[] {1, 1};
10+
11+
preSetting(gems);
12+
if(types.size() != 1) answer = findSection(gems);
13+
14+
return answer;
15+
}
16+
17+
int[] findSection(String[] gems){
18+
int[] answer = new int[2];
19+
int l, r;
20+
21+
l = 0;
22+
r = 1;
23+
cnt.put(gems[l], 1);
24+
while(l <= r & r < n){
25+
// System.out.println(l + " " + gems[l] + " " + r + " " + gems[r]);
26+
27+
if(gems[l].equals(gems[r])) {
28+
l += remove(gems[l]);
29+
// System.out.println(" 삭제:" + l + " " + gems[l] + " " + r + " " + gems[r]);
30+
31+
while(l < r - 1){
32+
if(1 < cnt.get(gems[l])){
33+
// System.out.println(" 연속 삭제:" + l + " " + gems[l] + " " + r + " " + gems[r]);
34+
35+
l += remove(gems[l]);
36+
}else break;
37+
}
38+
}
39+
cnt.put(gems[r], cnt.getOrDefault(gems[r], 0) + 1);
40+
41+
if(types.size() <= cnt.size() && r - l + 1 < minLen){
42+
minLen = r - l + 1;
43+
answer[0] = l + 1;
44+
answer[1] = r + 1;
45+
}
46+
r++;
47+
}
48+
49+
return answer;
50+
}
51+
52+
int remove(String l){
53+
if(cnt.get(l) == 1) cnt.remove(l);
54+
else cnt.put(l, cnt.get(l) - 1);
55+
56+
return 1;
57+
}
58+
59+
void preSetting(String[] gems){
60+
n = gems.length;
61+
minLen = n + 1;
62+
cnt = new HashMap<>();
63+
types = new HashMap<>();
64+
65+
int idx = 1;
66+
for(int i = 0; i < n; i++) {
67+
if(!types.containsKey(gems[i])) types.put(gems[i], idx++);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)