Skip to content

solve(압축, 프렌즈4블록): wonho_230905 #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions wonho/week26/압축.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.*;

class 압축 {
private Map<String, Integer> indexes = new HashMap<>();
private int currentIndex = 1;

public int[] solution(String msg) {
initMap();

List<Integer> result = new ArrayList<>();
int start = 0;

while (start < msg.length()) {
int end = start + 1;
while (end <= msg.length()) {
String sub = msg.substring(start, end);
if (indexes.get(sub) == null) {
indexes.put(sub, currentIndex);
currentIndex++;
result.add(indexes.get(msg.substring(start, end - 1)));
start = end - 1;
break;
}
end++;
}
if (start != end - 1) {
start++;
continue;
}

if (start == msg.length() - 1 || indexes.get(msg.substring(start)) != null) {
result.add(indexes.get(msg.substring(start)));
start++;
}
}
if (result.size() == 0) {
result.add(indexes.get(msg.substring(0)));
}
return result.stream().mapToInt(i -> i).toArray();
}

public void initMap() {
for (char c = 'A'; c <= 'Z'; c++) {
indexes.put(String.valueOf(c), currentIndex);
currentIndex++;
}
}
}
72 changes: 72 additions & 0 deletions wonho/week26/프렌즈4블록.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package week26;

import java.util.*;

class 프렌즈4블록 {
private char[][] map;

public int solution(int m, int n, String[] board) {
int answer = 0;
map = new char[m][n];

for (int i = 0; i < m; i++) {
map[i] = board[i].toCharArray();
}

while (true) {
boolean transformed = false;
char[][] prevMap = new char[m][n];
for (int i = 0; i < m; i++) {
prevMap[i] = Arrays.copyOf(map[i], n);
}

for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < n - 1; j++) {
char stand = prevMap[i][j];
if (stand == '0') {
continue;
}
if (stand != prevMap[i + 1][j] || stand != prevMap[i][j + 1] || stand != prevMap[i + 1][j + 1]) {
continue;
}
transformed = true;
if (map[i][j] != '0') {
answer++;
map[i][j] = '0';
}
if (map[i + 1][j] != '0') {
answer++;
map[i + 1][j] = '0';
}
if (map[i][j + 1] != '0') {
answer++;
map[i][j + 1] = '0';
}
if (map[i + 1][j + 1] != '0') {
answer++;
map[i + 1][j + 1] = '0';
}
}
}
if (!transformed) {
return answer;
}

for (int col = 0; col < n; col++) {
for (int row = m - 1; row >= 0; row--) {
if (map[row][col] == '0') {
int start = row;
while (start >= 0 && map[start][col] == '0') {
start--;
}
if (start == -1) {
break;
}
map[row][col] = map[start][col];
map[start][col] = '0';
}
}
}
}
}
}