Skip to content

solve(n진수게임, 파일명정렬, 압축, 프렌즈4블록): hyeongu_230905 #189

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
23 changes: 23 additions & 0 deletions hyeongu/week_26/n진수게임.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class n진수게임 {
public String solution(int n, int t, int m, int p) {
StringBuilder sb = new StringBuilder();
int maxSize = m * t;

// 1부터 n진수로 바꿔서 계속 더함
// m명의 사람이 t번씩 말하는 길이까지 구함
for(int i = 0; sb.length() <= maxSize; i++){
sb.append(Integer.toString(i, n));
}

// 왜 답을 대문자로 해놓는거야 귀찮게
String total = sb.toString().toUpperCase();
sb.setLength(0);

// 튜브가 말할 숫자만 뽑아서 저장
for(int i = p - 1; i < maxSize; i += m){
sb.append(total.charAt(i));
}

return sb.toString();
}
}
73 changes: 73 additions & 0 deletions hyeongu/week_26/파일명정렬.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;

class 파일명정렬 {
static List<File> list = new ArrayList<>();

static class File implements Comparable<File>{
String fileName; // 전체 파일 이름
String head; // head 부분
int number; // index 부분
int index; // 입력 순서

public File(String fileName, String head, int number) {
this.fileName = fileName;
this.head = head.toUpperCase();
this.number = number;
this.index = list.size();
}

// 문제에 주어진 조건에 맞게 compare 규칙 정의
public int compareTo(File f){
if(this.head.equals(f.head)){
if(this.number == f.number){
return this.index - f.index;
}
return this.number - f.number;
}
return this.head.compareTo(f.head);
}
}

public String[] solution(String[] files) {
String[] answer = new String[files.length];

for(String str : files){
list.add(findHead(str));
}

Collections.sort(list);

for(int i = 0; i < files.length; i++){
answer[i] = list.get(i).fileName;
}

return answer;
}

// 주어진 string에서 숫자가 나오는 순간을 찾고 앞부분을 head로 저장
public static File findHead(String str){
for(int i = 0; i < str.length(); i++){
char now = str.charAt(i);

if(isNumber(now)){
return new File(str, str.substring(0, i), findNumber(str, i));
}
}
return null;
}

// 이어진 숫자를 구해서 return
public static int findNumber(String str, int start){
for(int i = start; i < str.length(); i++){
char now = str.charAt(i);
if(!isNumber(now)){
return Integer.parseInt(str.substring(start, i));
}
}
return Integer.parseInt(str);
}

public static boolean isNumber(char c){
return c >= '0' && c <= '9';
}
}
37 changes: 37 additions & 0 deletions hyeongu/week_27/압축.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.*;

class 압축 {
public int[] solution(String msg) {
ArrayList<Integer> al = new ArrayList<>();
HashMap<String, Integer> hm = new HashMap<>();
int count = 1;
// A ~ Z 맵 초기화
for(char c = 'A'; c <= 'Z'; c++) {
hm.put(c + "", count++);
}

String tmp = msg;
while(tmp.length()>0) {
int i;
for(i = 1; i < tmp.length(); i++) {
// 사전에 없는 단어가 될 때까지 탐색
// 없는 단어일 경우 리스트와 맵에 추가
if(!hm.containsKey(tmp.substring(0, i + 1))) {
al.add(hm.get(tmp.substring(0, i)));
hm.put(tmp.substring(0, i + 1), count++);
break;
}
}
if(i == tmp.length()) {
al.add(hm.get(tmp));
}
tmp = tmp.substring(i);
}

int[] answer = new int[al.size()];
for(int j = 0; j < al.size(); j++) {
answer[j] = al.get(j);
}
return answer;
}
}
55 changes: 55 additions & 0 deletions hyeongu/week_27/프렌즈4블록.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.*;
class 프렌즈4블록 {
public int solution(int m, int n, String[] board) {
boolean[][] check = new boolean[n][m];
int answer = 0;
boolean isBreak = true;
ArrayList<Character>[] arr = new ArrayList[n];

for(int i = 0; i < n; i++) {
arr[i] = new ArrayList<Character>();
}
for(int i = m; i > 0; i--) {
for(int j = 0; j < n; j++) {
arr[j].add(board[i - 1].charAt(j));
}
}

// 아래에서부터 시작하는 ArrayList를 이용해 remove하면 자동으로 내려오게 구현
while(isBreak){
//삭제하는 블럭이 있을경우 true, 없는경우 while문 종료
isBreak = false;
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < arr[i].size() - 1; j++) {
// 현재 칸 만큼 옆의 칸이 높이가 올라오지 않은 경우
if(arr[i + 1].size() <= j + 1) {
break;
}

// 사각형 모양으로 다 같으면 기록
if(arr[i].get(j) == arr[i].get(j + 1)) {
if(arr[i + 1].get(j) == arr[i + 1].get(j + 1)&&arr[i].get(j) == arr[i + 1].get(j)) {
check[i][j] = true;
check[i][j + 1] = true;
check[i + 1][j] = true;
check[i + 1][j + 1] = true;
isBreak = true;
}
}
}
}

// 기록된 블록을 위에서부터 제거
for(int i = 0; i < n; i++) {
for(int j = arr[i].size() - 1; j >= 0; j--) {
if(check[i][j]) {
arr[i].remove(j);
answer++;
check[i][j] = false;
}
}
}
}
return answer;
}
}