Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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 29 30
Tags more
Archives
Today
Total
관리 메뉴

개발의변화

프로그래머스 LV3 표병합 본문

알고리즘

프로그래머스 LV3 표병합

refindmySapporo 2024. 2. 14. 18:46
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/150366

 

프로그래머스

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

programmers.co.kr

 

구현을 하는데 있어서 어떠한 저장방식을 사용하느냐에 대한 깊은 고민이 필요한 문제였고

또한 구현이기에 디테일한 방식을 활용해야 했던 문제였던 것 같았다.

UPDATE에 대한 문자열을 저장하는 이차원 배열과 유니온파인드 형식을 활용하여 병합데이터를 획득한 방식으로 활용했다.

import java.util.*;
	
	public class Solution {
		static int N = 51;
	    static String [][] arr = new String[N][N]; //값 저장하는 이차원 배열
	    static int [][][] mergeArr = new int [N][N][2];
	    public String[] solution(String[] commands) {
	        ArrayList <String> answer = new ArrayList <> ();
	        
	        for (String command : commands) {
	            String [] s = command.split(" ");
	            if (s[0].equals("UPDATE")) {
	                //좌표 업데이트
	                if (s.length == 4) { 
	                    int r = Integer.parseInt(s[1]) ;
	                    int c = Integer.parseInt(s[2]) ;
	                    String value = s[3];
	                    // 선언된 요소 변경
	                    arr[r][c] = value;
	                    // 병합되어 있다면 그것도 변경
//	                    System.out.println("1번"+Arrays.toString(mergeArr[r][c]));
	                    if (mergeArr[r][c][0] != 0)
	                    for (int i = 0; i < N; i++) {
	                        for (int j = 0; j < N; j++) {
	                            if (mergeArr[i][j][0] == mergeArr[r][c][0] && mergeArr[i][j][1] == mergeArr[r][c][1]) {
	                                arr[i][j] = value;
	                            } 
	                        }
	                    }
	                }
	                //값 업데이트
	                else if (s.length == 3) {
	                    String value1 = s[1];
	                    String value2 = s[2];
	                    for (int i = 0; i < N; i++) {
	                        for (int j = 0; j < N; j++) {
	                            if(arr[i][j]!=null && arr[i][j].equals(value1)) {
	                                arr[i][j] = value2;
	                            }
	                        }
	                    }
	                }
	            }
	            // 병합
	            else if (s[0].equals("MERGE")) {
//	            	System.out.println("??MERGE");
	                int r1 = Integer.parseInt(s[1]) ;
	                int c1 = Integer.parseInt(s[2]) ;
	                int r2 = Integer.parseInt(s[3]) ;
	                int c2 = Integer.parseInt(s[4]) ;
	                if (arr[r1][c1] != null) {
	                    if (mergeArr[r1][c1][0] == 0) mergeArr[r1][c1] = new int [] {r1,c1};
	                    if (mergeArr[r2][c2][0] != 0) {
                            int x = mergeArr[r2][c2][0];
                            int y = mergeArr[r2][c2][1];
	                        for (int i = 0; i < N; i++) {
	                            for (int j = 0; j < N; j++) {
                                    // System.out.println("mergeArr"+mergeArr[r2][c2][0]+":"+mergeArr[r2][c2][1]);
                                    // System.out.println("merge"+mergeArr[i][j][0]+":"+mergeArr[i][j][1]);
	                                if (mergeArr[i][j][0] == x && mergeArr[i][j][1] == y) {
	                                    arr[i][j] = arr[r1][c1];
	                                    mergeArr[i][j] = new int [] {mergeArr[r1][c1][0],mergeArr[r1][c1][1]};
	                                } 
	                            }
	                        }          
	                    }
	                    mergeArr[r2][c2] = new int [] {mergeArr[r1][c1][0],mergeArr[r1][c1][1]};
	                    arr[r2][c2] = arr[r1][c1];
	                }
	                else if (arr[r2][c2] != null) {
	                    if (mergeArr[r2][c2][0] == 0) mergeArr[r2][c2] = new int [] {r2,c2};
	                    if (mergeArr[r1][c1][0] != 0) {
                            int x = mergeArr[r1][c1][0];
                            int y = mergeArr[r1][c1][1];
	                        for (int i = 0; i < N; i++) {
	                            for (int j = 0; j < N; j++) {
	                                if (mergeArr[i][j][0] == x && mergeArr[i][j][1] == y) {
	                                arr[i][j] = arr[r2][c2];
	                                mergeArr[i][j] = new int [] {mergeArr[r2][c2][0],mergeArr[r2][c2][1]};
	                                } 
	                            }
	                        }                         
	                    }

	                    mergeArr[r1][c1] = new int [] {mergeArr[r2][c2][0],mergeArr[r2][c2][1]};
	                    arr[r1][c1] = arr[r2][c2];                  
	                }
	                //둘 다 널일때
	                else {
	                    if (mergeArr[r1][c1][0] == 0) mergeArr[r1][c1] = new int [] {r1,c1};
	                    if (mergeArr[r2][c2][0] != 0) {
                            int x = mergeArr[r2][c2][0];
                            int y = mergeArr[r2][c2][1];
	                        for (int i = 0; i < N; i++) {
	                            for (int j = 0; j < N; j++) {
	                                if (mergeArr[i][j][0] == x && mergeArr[i][j][1] == y) {
	                                    arr[i][j] = arr[r1][c1];
	                                    mergeArr[i][j] = new int [] {mergeArr[r1][c1][0],mergeArr[r1][c1][1]};
	                                } 
	                            }
	                        }          
	                    }
	                    mergeArr[r2][c2] = new int [] {mergeArr[r1][c1][0],mergeArr[r1][c1][1]};
	                    arr[r2][c2] = arr[r1][c1];                  
	                }
	            }
	            else if (s[0].equals("UNMERGE")) {

	                int r = Integer.parseInt(s[1]) ;
	                int c = Integer.parseInt(s[2]) ;
                    
	            	String temp = arr[r][c];
	                if(mergeArr[r][c][0] != 0) {
                        int x = mergeArr[r][c][0];
                        int y = mergeArr[r][c][1];
		                for (int i = 0; i < N; i++) {
		                    for (int j = 0; j < N; j++) {
		                        if ( mergeArr[i][j][0] == x && mergeArr[i][j][1] == y) {
                                    // System.out.println(mergeArr[i][j][0]);
		                                arr[i][j] = null;
		                                mergeArr[i][j] = new int[] {0,0};
		                        }       
		                    }
		                }	                	
	                }
                    mergeArr[r][c][0] = 0;
                    mergeArr[r][c][1] = 0;
	                arr[r][c] = temp;

	            }
	            //PRINT할 때 
	            else {
	                int r = Integer.parseInt(s[1]);
	                int c = Integer.parseInt(s[2]);
                    answer.add(arr[r][c] != null ? arr[r][c] : "EMPTY");
	            }
// 	            System.out.println("한바퀴 arr________________________");
// 	            for (int i = 0; i < N; i++) {
// 	            	System.out.println(Arrays.toString(arr[i]));
// 	            }
	            
// 	            System.out.println("mergeARrr________________________");
// 	            for (int i = 0; i < N; i++) {
// 	            	System.out.println(Arrays.deepToString(mergeArr[i]));
// 	            }            
// 	            System.out.println("한바퀴________________________");
	        }
	        String [] x = new String[answer.size()];
            for (int i = 0; i < answer.size(); i++) {
                x[i] = answer.get(i);
            }
            return x;
	    }
	    
	    
	}
반응형