diff --git a/src/jihye/thisiscodingtest/part03/Q09_stringCompression/OtherSolution.java b/src/jihye/thisiscodingtest/part03/Q09_stringCompression/OtherSolution.java new file mode 100644 index 0000000..3f3e5eb --- /dev/null +++ b/src/jihye/thisiscodingtest/part03/Q09_stringCompression/OtherSolution.java @@ -0,0 +1,7 @@ +package jihye.thisiscodingtest.part03.Q09_stringCompression; + +public class OtherSolution { + public static void main(String[] args) { + + } +} diff --git a/src/jihye/thisiscodingtest/part03/Q09_stringCompression/Solution.java b/src/jihye/thisiscodingtest/part03/Q09_stringCompression/Solution.java new file mode 100644 index 0000000..189bced --- /dev/null +++ b/src/jihye/thisiscodingtest/part03/Q09_stringCompression/Solution.java @@ -0,0 +1,62 @@ +package jihye.thisiscodingtest.part03.Q09_stringCompression; + +public class Solution { + private static boolean check = false; + + public static void main(String[] args) { + String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + System.out.println(solution(s)); + } + + public static int solution(String s) { + int unit = 1; + int ans = Integer.MAX_VALUE; + + while (true) { + if (unit > s.length() / 2) {//자르는 단위가 절반을 넘어가면 break + break; + } else { + int n = findCompression(unit, s); + if (check == true) { + ans = Math.min(ans, n); + } + } + unit++; + check = false; + } + + if (ans == Integer.MAX_VALUE) { + return s.length(); + } + return ans; + } + + public static int findCompression(int unit, String s) {//문자열 찾기 구간 + String newString = ""; + String one = s.substring(0, unit);//하나의 일치하는 스트링 단위 + int end = 0; + for (int i = unit; i < s.length(); i += unit) { + int count = 1; + while (i <= s.length() - unit && s.substring(i, i + unit).equals(one)) { + count++; + i = i + unit; + } + if (count != 1) {//count가 2이상일때 + check = true; + newString += count + one;//newString에 count와 반복되는 string(one)을 넣는다 System.out.println(newString); + } else { + newString += one; + } + if (i + unit > s.length()) {//범위 체크 + end = i; + break; + } + one = s.substring(i, i + unit); + + } + if (end <= s.length()) {//만일 남은 unit단위 보다 작은 문자열이 남았을시 더해준다. + newString += s.substring(end); + } + return newString.length(); + } +} diff --git a/src/jihye/thisiscodingtest/part03/Q39_marsExploration/MainFail.java b/src/jihye/thisiscodingtest/part03/Q39_marsExploration/MainFail.java new file mode 100644 index 0000000..f41125e --- /dev/null +++ b/src/jihye/thisiscodingtest/part03/Q39_marsExploration/MainFail.java @@ -0,0 +1,86 @@ +//package jihye.thisiscodingtest.part03.Q39_marsExploration; +// +// +//import java.util.Arrays; +//import java.util.LinkedList; +//import java.util.Queue; +//import java.util.Scanner; +// +////class Coordinate { +//// int x; +//// int y; +//// +//// Coordinate(int x, int y) { +//// this.x = x; +//// this.y = y; +//// } +////} +// +//public class MainFail { +// static int dx[] = {-1, 1, 0, 0}; +// static int dy[] = {0, 0, -1, 1}; +// static int N; +// +// public static void main(String[] args) { +// Scanner sc = new Scanner(System.in); +// int T = sc.nextInt(); +// int[][] map; +// +// for (int i = 0; i < T; i++) { +// N = sc.nextInt(); +// map = new int[N][N]; +// +// //각 map을 입력 받는다. +// for (int j = 0; j < N; j++) { +// for (int k = 0; k < N; k++) { +// map[j][k] = sc.nextInt(); +// } +// } +// +// System.out.println(findRoute(map)); +// } +// } +// +// public static int findRoute(int[][] map) { +// int x = 0; +// int y = 0; +// boolean[][] visited = new boolean[N][N]; +// int[][] distance = new int[N][N]; +// Queue queue = new LinkedList<>(); +// Coordinate coordinate = new Coordinate(0, 0); +// +// +// //copy +// for(int i=0; i < N; i++){ +// Arrays.fill(distance[i], 1250); +// } +// +// queue.add(coordinate); +// distance[0][0] = map[0][0]; +// +// //queue가 빌때 까지 +// while (!queue.isEmpty()) { +// +// Coordinate curr = queue.poll(); +// x = curr.x; +// y = curr.y; +// +// for (int i = 0; i < 4; i++) { +// int nx = x + dx[i]; +// int ny = y + dy[i]; +// if (0 <= nx && nx < N && 0 <= ny && ny < N) {//범위를 넘지 않느다면 +// if (visited[nx][ny] == false) {//그리고 방문하지 않은곳이라면 +// distance[nx][ny] = Math.min(distance[x][y] + map[nx][ny], distance[nx][ny]); +// //최단 거리를 구한다. +// curr = new Coordinate(nx, ny); +// queue.add(curr); +// visited[nx][ny] = true; +//// System.out.println(nx +" " +ny+" " + distance[nx][ny]); +// } +// } +// } +// +// } +// return distance[N - 1][N - 1]; +// } +//} diff --git a/src/jihye/thisiscodingtest/part03/Q39_marsExploration/Solution.java b/src/jihye/thisiscodingtest/part03/Q39_marsExploration/Solution.java new file mode 100644 index 0000000..60cf9b8 --- /dev/null +++ b/src/jihye/thisiscodingtest/part03/Q39_marsExploration/Solution.java @@ -0,0 +1,97 @@ +package jihye.thisiscodingtest.part03.Q39_marsExploration; + +import java.util.*; + + +class Coordinate implements Comparable{ + int x; + int y; + int dist; + + Coordinate(int x, int y, int dist) { + this.x = x; + this.y = y; + this.dist = dist; + } + + @Override + public int compareTo(Coordinate other) { + if (this.dist < other.dist) { + return -1; + } + return 1; + } +} + +public class Solution { + + static int dx[] = {-1, 1, 0, 0}; + static int dy[] = {0, 0, -1, 1}; + static int N; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int T = sc.nextInt(); + int[][] map; + + for (int i = 0; i < T; i++) { + N = sc.nextInt(); + map = new int[N][N]; + + //각 map을 입력 받는다. + for (int j = 0; j < N; j++) { + for (int k = 0; k < N; k++) { + map[j][k] = sc.nextInt(); + } + } + + System.out.println(findRoute(map)); + } + } + + public static int findRoute(int[][] map) { + int x = 0; + int y = 0; + int dist = 0; + boolean[][] visited = new boolean[N][N]; + int[][] distance = new int[N][N]; + PriorityQueue pq = new PriorityQueue<>();; + Coordinate coordinate = new Coordinate(0, 0, map[0][0]); + + + // + for (int i = 0; i < N; i++) { + Arrays.fill(distance[i], Integer.MAX_VALUE); + } + + pq.add(coordinate); + distance[0][0] = map[0][0]; + + //queue가 빌때 까지 + while (!pq.isEmpty()) { + + //queue에서 꺼낸다. + Coordinate curr = pq.poll(); + x = curr.x; + y = curr.y; + dist = curr.dist; + + //이미 처리 된적 있으면 무 + if (distance[x][y] < dist) continue; + + for (int i = 0; i < 4; i++) {//인접한 노드 + int nx = x + dx[i]; + int ny = y + dy[i]; + if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue; //범위를 넘지 않느다면 + int cost = dist + map[nx][ny]; + + if (cost < distance[nx][ny]) {//가중치가 더 작다면 + distance[nx][ny] = cost;//distance갱신 + pq.add(new Coordinate(nx,ny,cost)); + } + + } + } + return distance[N - 1][N - 1]; + } +} diff --git a/src/jihye/thisiscodingtest/part03/Q40_HideAndSeek/Solution.java b/src/jihye/thisiscodingtest/part03/Q40_HideAndSeek/Solution.java index 504cea5..2a40212 100644 --- a/src/jihye/thisiscodingtest/part03/Q40_HideAndSeek/Solution.java +++ b/src/jihye/thisiscodingtest/part03/Q40_HideAndSeek/Solution.java @@ -13,7 +13,7 @@ public Node(int end, int weight) { public class Solution { - public static ArrayList[] graph; + public static ArrayList[] graph; public static boolean[] visited; public static int INF = Integer.MAX_VALUE; public static int[] distance; @@ -61,12 +61,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]));//큐에 더해준다 + } + } } } }