Skip to content

Commit 8ede1ed

Browse files
authored
Merge pull request #231 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 7월 1주차 / 3문제
2 parents 045df3a + d11a0bc commit 8ede1ed

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 풀이 1: 정렬을 이용한 풀이
2+
import java.util.*;
3+
4+
class Solution {
5+
public String solution(String[] participant, String[] completion) {
6+
Arrays.sort(participant);
7+
Arrays.sort(completion);
8+
9+
for (int i = 0; i < completion.length; i++) {
10+
if (!participant[i].equals(completion[i])) {
11+
return participant[i];
12+
}
13+
}
14+
15+
return participant[participant.length - 1];
16+
}
17+
}
18+
19+
// 풀이2: 해시맵을 이용한 풀이
20+
import java.util.*;
21+
22+
class Solution {
23+
public String solution(String[] participant, String[] completion) {
24+
String answer = "";
25+
26+
HashMap<String, Integer> dict = new HashMap<>();
27+
28+
for (String p : participant) dict.put(p, dict.getOrDefault(p, 0) + 1);
29+
for (String c : completion) dict.put(c, dict.get(c) - 1);
30+
31+
for (String key: dict.keySet()) {
32+
if (dict.get(key) != 0) {
33+
answer = key;
34+
}
35+
}
36+
37+
return answer;
38+
}
39+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
def check_remove(m, n, grid):
2+
"""
3+
2×2 같은 블록을 찾아서, 지울 칸을 True로 표시한 2D 배열을 반환한다.
4+
"""
5+
mark = [[False] * n for _ in range(m)]
6+
for i in range(m-1):
7+
for j in range(n-1):
8+
current = grid[i][j]
9+
if current and grid[i][j+1] == current \
10+
and grid[i+1][j] == current \
11+
and grid[i+1][j+1] == current:
12+
mark[i][j] = mark[i][j+1] = mark[i+1][j] = mark[i+1][j+1] = True
13+
return mark
14+
15+
def remove_blocks(m, n, grid, mark):
16+
"""
17+
mark 배열이 True인 칸으르 찾아서 빈칸으로 바꾸고,
18+
삭제된 블록의 총 개수를 반환한다.
19+
"""
20+
removed = 0
21+
for i in range(m):
22+
for j in range(n):
23+
if mark[i][j]:
24+
grid[i][j] = ""
25+
removed += 1
26+
return removed
27+
28+
def down_blocks(m, n, grid):
29+
"""
30+
중력을 적용하여, 각 열마다 빈칸 위의 블록을 아래로 당긴다.
31+
"""
32+
for j in range(n):
33+
stack = []
34+
for i in range(m):
35+
if grid[i][j] != "":
36+
stack.append(grid[i][j])
37+
38+
for i in range(m-1, -1, -1):
39+
if stack:
40+
grid[i][j] = stack.pop()
41+
42+
else:
43+
grid[i][j] = ""
44+
45+
def solution(m, n, board):
46+
# 1. 입력 문자열을 2D 리스트로 변환
47+
grid = [list(row) for row in board]
48+
total_removed = 0
49+
50+
while True:
51+
# 2. 지울 칸 표시
52+
mark = check_remove(m, n, grid)
53+
# 3. 표시된 칸 삭제 및 개수 세기
54+
removed = remove_blocks(m, n, grid, mark)
55+
if removed == 0:
56+
# 더이상 지울 블록이 없다면 반복 종료
57+
break
58+
# 4. 전체 삭제 개수에 해당 칸의 삭제 개수 누적해서 더하기
59+
total_removed += removed
60+
# 5. 중력 적용
61+
down_blocks(m, n, grid)
62+
63+
return total_removed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import heapq
2+
3+
4+
def solution(operations):
5+
min_heap = []
6+
max_heap = []
7+
number_count = {} # 각 숫자가 큐에 몇 번 남았는지 카운트
8+
size = 0 # 현재 큐에 남아있는 총 원소 수
9+
10+
for op in operations:
11+
cmd, num_str = op.split(' ')
12+
num = int(num_str)
13+
14+
if cmd == 'I':
15+
# 삽입
16+
heapq.heappush(min_heap, num)
17+
heapq.heappush(max_heap, -num)
18+
number_count[num] = number_count.get(num, 0) + 1
19+
size += 1
20+
else: # cmd == 'D'
21+
if size == 0:
22+
# 큐가 비어있다면 연산 무시
23+
continue
24+
25+
if num == 1:
26+
# 최댓값 삭제
27+
while max_heap:
28+
x = -heapq.heappop(max_heap)
29+
if number_count.get(x, 0) > 0:
30+
# num이 1번 이상 등장했다면, 카운트 반영
31+
number_count[x] -= 1
32+
size -= 1
33+
break
34+
else:
35+
# num == -1, 최솟값 삭제
36+
while min_heap:
37+
x = heapq.heappop(min_heap)
38+
if number_count.get(x, 0) > 0:
39+
# num이 1번 이상 등장했다면, 카운트 반영
40+
number_count[x] -= 1
41+
size -= 1
42+
break
43+
44+
# 연산 후 큐가 비어있다면 [0, 0]
45+
if size == 0:
46+
return [0, 0]
47+
48+
# 남아있는 최댓값 찾기
49+
max_value = 0
50+
while max_heap:
51+
x = -heapq.heappop(max_heap)
52+
if number_count.get(x, 0) > 0:
53+
max_value = x
54+
break
55+
56+
# 남아있는 최솟값 찾기
57+
min_value = 0
58+
while min_heap:
59+
x = heapq.heappop(min_heap)
60+
if number_count.get(x, 0) > 0:
61+
min_value = x
62+
break
63+
64+
return [max_value, min_value]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int solution(int[] people, int limit) {
5+
int boat = 0;
6+
int left = 0;
7+
int right = people.length - 1;
8+
9+
Arrays.sort(people);
10+
11+
while (left <= right) {
12+
if (people[left] + people[right] <= limit) {
13+
left += 1;
14+
right -= 1;
15+
} else {
16+
right -= 1;
17+
}
18+
boat += 1;
19+
20+
}
21+
return boat;
22+
}
23+
}

0 commit comments

Comments
 (0)