Skip to content
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

Feature/jihye q36 #154

Open
wants to merge 7 commits 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package jihye.thisiscodingtest.part03.Q09_stringCompression;

public class Solution {

public static void main(String[] args) {
String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println(solution(s));
}

public static int solution(String s) {
int unit = 1;
int answer = s.length();

while (true) {
if (unit > s.length() / 2) { //자르는 단위가 절반을 넘어가면 break
break;
} else {
int n = getCompressedStringLength(unit, s);
answer = Math.min(answer, n);
}
unit++;
}

return answer;
}

public static int getCompressedStringLength(int unit, String s) { //문자열 찾기 구간
StringBuilder compressedString = new StringBuilder();
String current = s.substring(0, unit); //하나의 일치하는 스트링 단위
int end = 0;

//문자열을 자르는 단위만큼 잘라서 다음 단위와 일치하는지
//일치한다면 그 수를 count해서 newString에 넣어준다

for (int i = unit; i < s.length(); i += unit) {
int count = 1;

while (isSame(i , s, unit, current)) {
count++;
i = i + unit;
}
if (count > 1) { //count가 2이상일때
compressedString.append(Integer.toString(count)); //newString에 count와 반복되는 string(one)을 넣는다 System.out.println(newString);
}
compressedString.append(current);

if (i + unit > s.length()) { //범위 체크
end = i;
break;
}
current = s.substring(i, i + unit);
}

if (end <= s.length()) { //만일 남은 unit단위 보다 작은 문자열이 남았을시 더해준다.
compressedString.append(s.substring(end));
}
return compressedString.length();
}

public static boolean isSame(int i , String s, int unit, String oneUnit){
return i <= s.length() - unit && s.substring(i, i + unit).equals(oneUnit);
}
}
61 changes: 61 additions & 0 deletions src/jihye/thisiscodingtest/part03/Q36_editing/MainFail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//package jihye.thisiscodingtest.part03.Q36_editing;
//
//import java.util.Scanner;
//
//public class Main {
// static int[] dp;
// static String A;
// static String B;
//
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// A = sc.nextLine();
// B = sc.nextLine();
// StringBuilder sb = new StringBuilder();
//
// dp = new int[B.length()];
//
// System.out.println(findLength(sb, 0));
// }
//
// // 재귀
// public static int findLength(StringBuilder current, int index) {
// if (current.toString() == B) {
// return dp[index];
// }
// if (A.charAt(index) == B.charAt(index)) {
// dp[index] = dp[index - 1];//같다면 굳이 편집을 안해도 되니 값 유지 하지만 초기 값 설정해줘야됨
// return findLength(current.append(A.charAt(index)), index + 1);
// }
//// getExchangeCount(current, index);//
//// getRemoveCount();
//// getAddCount();
//// return count;
// }
//
// //문자를 삭제할 때의 로직
// public int getRemoveCount(StringBuilder current, int index) {
// int count = 0;
// while (!(A.charAt(index) == current.charAt(index))) {//같은 문자가 나올때 까지 삭제한다
// if (current.length() == 0) {
// return 10000;//나올 수 없는 최대의 수
// }
// count++;//그 수를 count
// current.deleteCharAt(index);
// }
// dp[index] = Math.min(dp[index - 1] + count, dp[index]);//기존에 dp[index]에 기록된것과 비교
// return findLength(current, index);
// }
//
// //문자 더하기
// public static int getAddCount(StringBuilder current, int index) {
// dp[index] = Math.min(dp[index - 1] + 1, dp[index]);//문자를 더하기 때문에 +1
// return findLength(current.append(A.charAt(index)), index + 1);
// }
//
// //문자 교체
// public static void getExchangeCount(char before, char current) {
//// dp[index] = Math.min();/
//// return findLength(current.replac());
// }
//}
50 changes: 50 additions & 0 deletions src/jihye/thisiscodingtest/part03/Q36_editing/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package jihye.thisiscodingtest.part03.Q36_editing;

import java.util.Scanner;

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();

int answer = editDistance(str1, str2);
System.out.println(answer);
}

public static int editDistance(String str1, String str2) {
int length1 = str1.length();
int lenght2 = str2.length();

int[][] dp = new int[length1 + 1][lenght2 + 1];

// 아무것도 없는데서 str1까지 만드는데 드는 값
for (int i = 1; i < length1 + 1; i++) {
dp[i][0] = i;
}

// 아무것도 없는데서 str2까지 만드는데 드는 값
for (int j = 1; j < lenght2 + 1; j++) {
dp[0][j] = j;
}

//최소 편집 거리 계산
for (int i = 1; i < length1 + 1; i++) {
for (int j = 1; j < lenght2 + 1; j++) {

// 만약 문자가 같다면 왼쪽위에 해당하는 수 그대로 대입
// 왼쪽위는 문자 더하기 str1에 있는 character 더하기 전 & str2에 있는 character 더하기 전
// su에서 sa로 바뀔 때 왼쪽 위는 s만 있는 상태에서 바꾸는 것 고려
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else { // 문자가 다를경우
//dp[i][j-1]은 삽입하는 케이스
//dp[i-1][j]는 삭제하는 케이스
//dp[i-1][j-1]은 교체하는 케이스
dp[i][j] = 1 + Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]);
}
}
}
return dp[length1][lenght2];
}
}
32 changes: 24 additions & 8 deletions src/jihye/thisiscodingtest/part03/Q40_HideAndSeek/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public Node(int end, int weight) {

public class Solution {

public static ArrayList[] graph;
public static ArrayList<Node>[] graph;
public static boolean[] visited;
public static int INF = Integer.MAX_VALUE;
public static int[] distance;
// public static int[] distance;
public static int N;
public static int M;
static int[] dist;
Expand All @@ -27,6 +27,7 @@ public static void main(String[] args) {
N = sc.nextInt();
M = sc.nextInt();
graph = new ArrayList[N + 1];
dist = new int[N + 1];

Arrays.fill(dist, INF);

Expand All @@ -44,6 +45,21 @@ public static void main(String[] args) {

dijkstra(1);

int maxDistance = 0;
int maxNode = 0;
int sameCount = 0;

for (int i = 1; i < N + 1; i++) {
if (maxDistance < dist[i]) {
maxNode = i;
maxDistance = dist[i];
sameCount = 0;
}
if (maxDistance == dist[i]) {
sameCount++;
}
}
System.out.println(maxNode + " " + maxDistance + " " + sameCount);
}

public static void dijkstra(int start) {
Expand All @@ -61,12 +77,12 @@ public static void dijkstra(int start) {
}
check[cur] = true;

// for (Node node : graph[cur]) {//list에서 node를 하나씩 꺼내서
// if (dist[node.end] > dist[cur] + node.weight) {//만일 현재 노드를 거쳐서 가는 경우가 더 짧은경우
// dist[node.end] = dist[cur] + node.weight;//거리를 더 짧은 걸로 업데이트후
// queue.add(new Node(node.end, dist[node.end]));//큐에 더해준다
// }
// }
for (Node node : graph[cur]) {//list에서 node를 하나씩 꺼내서
if (dist[node.end] > dist[cur] + node.weight) {//만일 현재 노드를 거쳐서 가는 경우가 더 짧은경우
dist[node.end] = dist[cur] + node.weight;//거리를 더 짧은 걸로 업데이트후
queue.add(new Node(node.end, dist[node.end]));//큐에 더해준다
}
}
}
}
}
Expand Down