반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- SQL
- softeer
- 백준
- 해결
- 배열 돌리기 5
- 14942
- 티스토리챌린지
- 프로그래머스
- 10기
- SSAFY
- 퍼즐 조각 채우기
- 조건에 부합하는 중고거래 상태 구하기
- MySQL
- 싸피
- 핵심
- 등산코스 정하기
- java
- 설명
- 142085
- 산 모양 타일링
- 수료
- 후기
- 카카오
- 24955
- 오블완
- PCCP
- 숫자 이어 붙이기
- 정기 코딩 인증평가
- 소프티어
- 165672
Archives
- Today
- Total
개발 쥬스
[프로그래머스/Java] [Kakao 블라인드 기출] 표 병합 본문
반응형
🔗 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/150366#
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
💬 회고점
일반적인 구현 문제였지만, MERGE 명령어를 구현하는 과정에서 많은 시간이 소요되었습니다. 최종적으로는 union 기법을 활용해 MERGE의 내용을 구현했고, MERGE에서 둘 중 하나만 값을 가지고 있으면 그 값으로 업데이트 하는 것에서 로직 오류가 있었습니다. 해당 오류를 고치는 과정에서도 시간이 걸렸습니다.
결국 원인을 찾아서 해결했지만,
union 기법을 다양하게 활용할 수 있다는 생각을 들게 해주는 문제였고 테스트도 좀 더 꼼꼼히 하며 문제를 해결해야겠다는 생각이 들었습니다.
✏️ 코드
import java.util.*;
class Solution {
private static final int MAX = 50;
private int[] parent = new int[MAX * MAX];
private String[][] board = new String[MAX][MAX];
public List<String> solution(String[] commands) {
List<String> answer = new ArrayList<>();
initParent();
initBoard();
for (String command : commands) {
String[] c = command.split(" ");
if ("PRINT".equals(c[0])) {
answer.add(print(c[1], c[2]));
continue;
}
if ("UPDATE".equals(c[0])) {
int size = c.length;
if (size == 3) {
switchValue(c[1], c[2]);
} else {
updateTarget(c[1], c[2], c[3]);
}
continue;
}
if ("MERGE".equals(c[0])) {
doMerge(c[1], c[2], c[3], c[4]);
continue;
}
if ("UNMERGE".equals(c[0])) {
doUnmerge(c[1], c[2]);
continue;
}
}
return answer;
}
private void doUnmerge(String r, String c) {
int r_int = Integer.parseInt(r) - 1;
int c_int = Integer.parseInt(c) - 1;
int ori_parent = parent[MAX * r_int + c_int];
String keep = board[r_int][c_int];
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
if (parent[MAX * i + j] == ori_parent) {
board[i][j] = "EMPTY";
parent[MAX * i + j] = MAX * i + j;
}
}
}
board[r_int][c_int] = keep;
}
private void doMerge(String originR, String originC,
String otherR, String otherC) {
if (originR.equals(otherR) && originC.equals(otherC)) {
return;
}
int ori_r = Integer.parseInt(originR) - 1;
int ori_c = Integer.parseInt(originC) - 1;
int otr_r = Integer.parseInt(otherR) - 1;
int otr_c = Integer.parseInt(otherC) - 1;
String inputValue;
boolean flag = true;
int ori_parent = parent[MAX * ori_r + ori_c];
int otr_parent = parent[MAX * otr_r + otr_c];
if (parent[ori_parent] == parent[otr_parent]) {
return;
}
if ("EMPTY".equals(board[ori_r][ori_c]) && !"EMPTY".equals(board[otr_r][otr_c])) {
inputValue = board[otr_r][otr_c];
flag = false;
} else {
inputValue = board[ori_r][ori_c];
}
parent[otr_parent] = ori_parent;
board[otr_r][otr_c] = inputValue;
board[otr_parent / MAX][otr_parent % MAX] = inputValue;
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
int idx = MAX * i + j;
if (flag && parent[idx] == otr_parent) {
parent[idx] = ori_parent;
board[i][j] = inputValue;
} else if (!flag && parent[idx] == ori_parent) {
board[i][j] = inputValue;
} else if (!flag && parent[idx] == otr_parent) {
parent[idx] = ori_parent;
}
}
}
}
private void updateTarget(String r, String c, String value) {
int r_int = Integer.parseInt(r) - 1;
int c_int = Integer.parseInt(c) - 1;
int ori_parent = parent[MAX * r_int + c_int];
board[r_int][c_int] = value;
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
if (parent[MAX * i + j] == ori_parent) {
board[i][j] = value;
}
}
}
}
private void switchValue(String from, String to) {
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
if (from.equals(board[i][j])) {
board[i][j] = to;
}
}
}
}
private String print(String r, String c) {
return board[Integer.parseInt(r) - 1][Integer.parseInt(c) - 1];
}
private void initBoard() {
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
board[i][j] = "EMPTY";
}
}
}
private void initParent() {
for (int i = 0; i <= MAX * MAX - 1; ++i) {
parent[i] = i;
}
}
}
반응형
'알고리즘' 카테고리의 다른 글
[백준/Java] 1038. 감소하는 수 (1) | 2025.01.28 |
---|---|
[백준/Java] 13549 숨바꼭질 3 (1) | 2025.01.22 |
[백준/Java] 24955 숫자 이어 붙이기 (0) | 2025.01.20 |
[프로그래머스/Java] [2022 카카오 인턴십 기출문제] 등산코스 정하기 (0) | 2025.01.17 |
[프로그래머스/MySQL] 상품 별 오프라인 매출 구하기 (1) | 2025.01.15 |