2024-TEAM-05 / algorithm-for-kakao

์นด์นด์˜ค ๊ธฐ์ถœ ๋ฌธ์ œ ๊ฐ€์ฆˆ์•„๐Ÿฃ
0 stars 0 forks source link

[2023 KAKAO BLIND RECRUITMENT] ํ‘œ ๋ณ‘ํ•ฉ #11

Open uijin-j opened 2 months ago

uijin-j commented 2 months ago

๐Ÿ”— ํ‘œ ๋ณ‘ํ•ฉ

hye-on commented 2 months ago

๐Ÿ“‘ ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ

์ฝ”๋“œ ํ’€์ด

```cpp #include #include #include #include using namespace std; //11:23 ~ 1:04 ๋ถ„ // ๊ตฌํ˜„? // ์œ„์น˜ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด a , ๊ฐ’ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด b //update - b ๊ฐ’ ์‚ฝ์ž… //update - b ๊ฐ’ ๊ต์ฒด //Merge - r2,c2์— ๋‹ค r1,c1์„ ๋„ฃ์–ด์คŒ // - ๊ฐ’์€ a๋ฅผ ์ฐธ๊ณ ํ•ด์„œ r1,c1๊ฒƒ์œผ๋กœ ๋‹ค b์— ๋„ฃ์–ด์คŒ ๋น„์–ด ์žˆ์œผ๋ฉด r2,c2๊บผ //Unmerge - a์—์„œ r,c์— ์žˆ๋Š” point์— ์žˆ๋Š” ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š”๊ฑฐ ๋‹ค ํ•ด์ œ. ๊ฐ’์žˆ์œผ๋ฉด r,c์—๋งŒ ๋„ฃ์–ด์คŒ pair p[51][51]; string map[51][51]; vector token; string word; void update(string cmd){ int idx = cmd.find(' '); string t = cmd.substr(idx+1); vector tokens = token; stringstream ss(t); // ์ž…๋ ฅ์„ ๊ณต๋ฐฑ์œผ๋กœ ๋ถ„๋ฆฌ while (ss >> word) { tokens.push_back(word); } //cout< solution(vector commands) { vector answer; //์ดˆ๊ธฐํ™” for(int i=1;i<=50;i++){ for(int j=1;j<=50;j++){ p[i][j] = {to_string(i),to_string(j)}; } } for(int i=0;i

์ฝ”๋ฉ˜ํŠธ

- ์˜คํƒ€ ํ•˜๋‚˜ ๋•Œ๋ฌธ์— 30๋ถ„ ๊ฑธ๋ ธ๋„ค์š”.. - updateํ•  ๋•Œ string ํŒŒ์‹ฑํ•˜๋Š” ๋ถ€๋ถ„์ด ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค.
uijin-j commented 2 months ago

๐Ÿ“‘ ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ

์ฝ”๋“œ ํ’€์ด

```java import java.util.*; // 17:25 START! // ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ? class Solution { int[] parents; String[] values; public String[] solution(String[] commands) { List printedList = new ArrayList<>(); parents = new int[2501]; values = new String[2501]; for(int i = 1; i <= 2500; ++i) { parents[i] = i; values[i] = ""; } for(String command : commands) { String[] args = command.split(" "); String cmd = args[0]; if(cmd.equals("UPDATE") && args.length == 4) { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); int point = getPoint(x, y); values[find(point)] = args[3]; continue; } if(cmd.equals("UPDATE")) { String from = args[1]; String to = args[2]; for(int point = 1; point <= 2500; ++point) { if(values[find(point)].equals(from)) { values[find(point)] = to; } } continue; } if(cmd.equals("MERGE")) { int x1 = Integer.parseInt(args[1]); int y1 = Integer.parseInt(args[2]); int x2 = Integer.parseInt(args[3]); int y2 = Integer.parseInt(args[4]); int from = getPoint(x1, y1); int to = getPoint(x2, y2); union(from, to); continue; } if(cmd.equals("UNMERGE")) { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); int point = getPoint(x, y); int target = find(point); String before = values[find(point)]; boolean[] check = new boolean[2501]; for(int i = 1; i <= 2500; ++i) { if(find(i) == target) { check[i] = true; } } for(int i = 1; i <= 2500; ++i) { if(check[i]) parents[i] = i; } values[target] = ""; values[point] = before; continue; } if(cmd.equals("PRINT")) { int x = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); int point = (x - 1) * 50 + y; String toPrint = values[find(point)].isBlank() ? "EMPTY" : values[find(point)]; printedList.add(toPrint); continue; } } String[] answer = new String[printedList.size()]; for(int i = 0; i < printedList.size(); ++i) { answer[i] = printedList.get(i); } return answer; } public int find(int point) { if(parents[point] == point) return point; return parents[point] = find(parents[point]); } public void union(int p1, int p2) { int parent1 = find(p1); int parent2 = find(p2); if(parent1 == parent2) return; String value = (values[parent1].isBlank()) ? values[parent2] : values[parent1]; parents[parent2] = parent1; values[parent1] = value; values[parent2] = ""; } public int getPoint(int x, int y) { return (x - 1) * 50 + y; } } ```

์ฝ”๋ฉ˜ํŠธ