diff --git "a/src/week3/\352\261\260\354\212\244\353\246\204\353\217\210/Vryez11/Solution.java" "b/src/week3/\352\261\260\354\212\244\353\246\204\353\217\210/Vryez11/Solution.java" new file mode 100644 index 0000000..0be0128 --- /dev/null +++ "b/src/week3/\352\261\260\354\212\244\353\246\204\353\217\210/Vryez11/Solution.java" @@ -0,0 +1,31 @@ +package week3.거스름돈.Vryez11; + +public class Solution { + + /** + * + * [프로그래머스] 거스름돈 + * + * 문제 난이도: Lv. 3 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12907 + * 풀이 시간: 20분 + * 풀이 근거: 타일 개수 더하기랑 비슷함, 1원으로 0원에서 1원, 1원에서 1원 더하면 2원 ... DP + */ + + final static int MOD = 1_000_000_007; + + public int solution(int n, int[] money) { + + int[] dp = new int[n + 1]; + dp[0] = 1; + + for (int coin : money) { + for (int price = coin; price <= n; price++) { + + dp[price] = (dp[price] + dp[price - coin]) % MOD; + } + } + + return dp[n]; + } +} diff --git "a/src/week3/\352\262\275\354\243\274\353\241\234\352\261\264\354\204\244/Vryez11/Solution.java" "b/src/week3/\352\262\275\354\243\274\353\241\234\352\261\264\354\204\244/Vryez11/Solution.java" new file mode 100644 index 0000000..d3c5b49 --- /dev/null +++ "b/src/week3/\352\262\275\354\243\274\353\241\234\352\261\264\354\204\244/Vryez11/Solution.java" @@ -0,0 +1,103 @@ +package week3.경주로건설.Vryez11; + +import java.util.Arrays; +import java.util.PriorityQueue; + +public class Solution { + + /** + * + * [프로그래머스] 경주로 건설 + * + * 문제 난이도: Lv. 3 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/67259 + * 풀이 시간: 1시간 오바 (못품..) + * 풀이 근거: 방향 관리와 최소 cost계산을 잘 못함.. + */ + + int[] dx = {0, 0, -1, 1}; + int[] dy = {1, -1, 0, 0}; + + static class Node { + int x; + int y; + int dir; + int cost; + + Node(int x, int y, int dir, int cost) { + this.x = x; + this.y = y; + this.dir = dir; + this.cost = cost; + } + } + + public int solution(int[][] board) { + + int n = board.length; + int INF = Integer.MAX_VALUE; + + int[][][] cost = new int[n][n][4]; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + Arrays.fill(cost[i][j], INF); + } + } + + PriorityQueue pq = + new PriorityQueue<>((a, b) -> a.cost - b.cost); + + if (n > 1 && board[1][0] == 0) { + cost[1][0][0] = 100; + pq.offer(new Node(0, 1, 0, 100)); + } + + if (n > 1 && board[0][1] == 0) { + cost[0][1][3] = 100; + pq.offer(new Node(1, 0, 3, 100)); + } + + while (!pq.isEmpty()) { + + Node now = pq.poll(); + + if (now.cost > cost[now.y][now.x][now.dir]) { + continue; + } + + if (now.x == n - 1 && now.y == n - 1) { + return now.cost; + } + + for (int nd = 0; nd < 4; nd++) { + + int nx = now.x + dx[nd]; + int ny = now.y + dy[nd]; + + if (nx < 0 || nx >= n || ny < 0 || ny >= n) { + continue; + } + + if (board[ny][nx] == 1) { + continue; + } + + int nextCost = now.cost; + + if (now.dir == nd) { + nextCost += 100; + } else { + nextCost += 600; + } + + if (cost[ny][nx][nd] > nextCost) { + cost[ny][nx][nd] = nextCost; + pq.offer(new Node(nx, ny, nd, nextCost)); + } + } + } + + return 0; + } +} \ No newline at end of file diff --git "a/src/week3/\353\211\264\354\212\244\355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/Vryez11/Solution.java" "b/src/week3/\353\211\264\354\212\244\355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/Vryez11/Solution.java" new file mode 100644 index 0000000..5adf188 --- /dev/null +++ "b/src/week3/\353\211\264\354\212\244\355\201\264\353\237\254\354\212\244\355\204\260\353\247\201/Vryez11/Solution.java" @@ -0,0 +1,85 @@ +package week3.뉴스클러스터링.Vryez11; + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + + /** + * + * [프로그래머스] 뉴스 클러스터링 + * + * 문제 난이도: Lv. 2 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/17677 + * 풀이 시간: 30분 + * 풀이 근거: Map을 통해 2자 씩 put / 이를 통해 합집합, 교집합 구하기 + */ + + public int solution(String str1, String str2) { + + Map str1Map = new HashMap<>(); + Map str2Map = new HashMap<>(); + + str1 = str1.toLowerCase(); + str2 = str2.toLowerCase(); + + for (int i = 0; i < str1.length() - 1; i++) { + + char char1 = str1.charAt(i); + char char2 = str1.charAt(i + 1); + + if (!isAlphabetChar(char1, char2)) { + continue; + } + + String word = (char1 + "" + char2); + + str1Map.put(word, str1Map.getOrDefault(word, 0) + 1); + } + for (int i = 0; i < str2.length() - 1; i++) { + + char char1 = str2.charAt(i); + char char2 = str2.charAt(i + 1); + + if (!isAlphabetChar(char1, char2)) { + continue; + } + + String word = (char1 + "" + char2); + + str2Map.put(word, str2Map.getOrDefault(word, 0) + 1); + } + + int union = 0; + int intersection = 0; + + for (String key : str1Map.keySet()) { + + intersection += str1Map.get(key); + + if (str2Map.containsKey(key)) { + union += Math.min(str1Map.get(key), str2Map.get(key)); + } + } + + for (String key : str2Map.keySet()) { + + intersection += str2Map.get(key); + } + + System.out.println("union = " + union); + System.out.println("intersection = " + intersection); + + intersection -= union; + + if (union == 0 && intersection == 0) { + return 65_536; + } + + return (int) (((double) union / intersection) * 65_536); + } + + private boolean isAlphabetChar(char char1, char char2) { + return char1 <= 'z' && char1 >= 'a' && 'a' <= char2 && char2 <= 'z'; + } +} diff --git "a/src/week3/\353\217\204\353\204\233\352\263\274\353\247\211\353\214\200\352\267\270\353\236\230\355\224\204/Solution.java" "b/src/week3/\353\217\204\353\204\233\352\263\274\353\247\211\353\214\200\352\267\270\353\236\230\355\224\204/Solution.java" new file mode 100644 index 0000000..9f76b19 --- /dev/null +++ "b/src/week3/\353\217\204\353\204\233\352\263\274\353\247\211\353\214\200\352\267\270\353\236\230\355\224\204/Solution.java" @@ -0,0 +1,55 @@ +package week3.도넛과막대그래프; + + +class Solution { + + /** + * + * [프로그래머스] 도넛과 막대 그래프 + * + * 문제 난이도: Lv 2 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/258711 + * 풀이 시간: 못 품.. + * 풀이 근거: 문제를 잘 읽자.., in, out 생각을 못했습니다... + */ + + public int[] solution(int[][] edges) { + + int max = 0; + + for (int[] edge : edges) { + max = Math.max(max, Math.max(edge[0], edge[1])); + } + + int[] in = new int[max + 1]; + int[] out = new int[max + 1]; + + for (int[] edge : edges) { + out[edge[0]]++; + in[edge[1]]++; + } + + int create = 0; + int stick = 0; + int eight = 0; + + for (int i = 1; i <= max; i++) { + + if (in[i] == 0 && out[i] >= 2) { + create = i; + } + + if (in[i] >= 1 && out[i] == 0) { + stick++; + } + + if (in[i] >= 2 && out[i] == 2) { + eight++; + } + } + + int donut = out[create] - stick - eight; + + return new int[]{create, donut, stick, eight}; + } +} diff --git "a/src/week3/\354\227\254\355\226\211\352\262\260\353\241\234/Vryez11/Solution.java" "b/src/week3/\354\227\254\355\226\211\352\262\260\353\241\234/Vryez11/Solution.java" new file mode 100644 index 0000000..822baa5 --- /dev/null +++ "b/src/week3/\354\227\254\355\226\211\352\262\260\353\241\234/Vryez11/Solution.java" @@ -0,0 +1,104 @@ +package week3.여행결로.Vryez11; + +import java.util.*; + +public class Solution { + + /** + * + * [프로그래머스] 여행경로 + * + * 문제 난이도: LV. 3 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/43164 + * 풀이 시간: 30분 + * 풀이 근거: 티켓을 먼저 정렬시켜서, 한 공항에서 다른 여러 공항이 있을 때, 사전 순으로 가도록 함. 그 다음 dfs로 해결 + */ + + private Map portNumMap; + private List[] lists; + + public String[] solution(String[][] tickets) { + + Arrays.sort(tickets, (o1, o2) -> { + if (o1[0].equals(o2[0])) { + return o1[1].compareTo(o2[1]); + } + return o1[0].compareTo(o2[0]); + }); + + portNumMap = new HashMap<>(); + int idx = 0; + + for (String[] ticket : tickets) { + + if (!portNumMap.containsKey(ticket[0])) { + portNumMap.put(ticket[0], idx++); + } + + if (!portNumMap.containsKey(ticket[1])) { + portNumMap.put(ticket[1], idx++); + } + } + + lists = new ArrayList[idx]; + + for (int i = 0; i < idx; i++) { + lists[i] = new ArrayList<>(); + } + + for (int i = 0; i < tickets.length; i++) { + int from = portNumMap.get(tickets[i][0]); + lists[from].add(i); + } + + boolean[] visited = new boolean[tickets.length]; + List answer = new ArrayList<>(); + + dfs("ICN", 0, tickets, visited, answer); + + return answer.toArray(new String[0]); + } + + private boolean dfs(String now, + int depth, + String[][] tickets, + boolean[] visited, + List answer) { + + answer.add(now); + + if (depth == tickets.length) { + return true; + } + + Integer nowNum = portNumMap.get(now); + + if (nowNum == null) { + answer.remove(answer.size() - 1); + return false; + } + + for (int ticketIdx : lists[nowNum]) { + + if (visited[ticketIdx]) { + continue; + } + + visited[ticketIdx] = true; + + if (dfs(tickets[ticketIdx][1], + depth + 1, + tickets, + visited, + answer)) { + return true; + } + + visited[ticketIdx] = false; + } + + answer.remove(answer.size() - 1); + + return false; + } +} \ No newline at end of file diff --git "a/src/week3/\354\240\220\355\224\204\354\231\200\354\210\234\352\260\204\354\235\264\353\217\231/Vryez11/Solution.java" "b/src/week3/\354\240\220\355\224\204\354\231\200\354\210\234\352\260\204\354\235\264\353\217\231/Vryez11/Solution.java" new file mode 100644 index 0000000..c53b6b4 --- /dev/null +++ "b/src/week3/\354\240\220\355\224\204\354\231\200\354\210\234\352\260\204\354\235\264\353\217\231/Vryez11/Solution.java" @@ -0,0 +1,30 @@ +package week3.점프와순간이동.Vryez11; + +public class Solution { + + /** + * + * [프로그래머스] 점프와 순간 이동 + * + * 문제 난이도: Lv2 + * 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/12980 + * 풀이 시간: 20분 + * 풀이 근거: 2로 나눠지면 순간 이동 / 아니면 한 칸 앞으로 가기 + */ + + public int solution(int n) { + + int battery = 0; + + while (n > 0) { + if (n % 2 == 0) { + n /= 2; + } else { + n -= 1; + battery += 1; + } + } + + return battery; + } +}