From addf9e4e697744de60f2f8af5554c733bc295271 Mon Sep 17 00:00:00 2001 From: sujunghwang <64738942+sujunghwang@users.noreply.github.com> Date: Thu, 23 Feb 2023 09:45:08 +0900 Subject: [PATCH 1/3] =?UTF-8?q?Week05=20PRG=2042584=20=EC=A3=BC=EC=8B=9D?= =?UTF-8?q?=EA=B0=80=EA=B2=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sujunghwang/week05/PRG_42584.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 sujunghwang/week05/PRG_42584.java diff --git a/sujunghwang/week05/PRG_42584.java b/sujunghwang/week05/PRG_42584.java new file mode 100644 index 0000000..6592743 --- /dev/null +++ b/sujunghwang/week05/PRG_42584.java @@ -0,0 +1,27 @@ +/* +https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=java + +주어진 배열을 탐색하면서 현재 값보다 작은 값이 언제 나오는지 구하면 되는 문제 +문제 카테고리는 스택/큐 였는데 모르겠어서 그냥 배열로 이중 for loop 돌림 +효율성 문제 생길 줄 알았는데 안생겨서 그냥 넘어가기로 함 +*/ + +class Solution { + public int[] solution(int[] prices) { + int len = prices.length; + int[] answer = new int[len]; + + for(int i = 0; i < len-1; i++){ + answer[i] = 1; + for(int j = i+1; j < len-1; j++){ + if(prices[j] >= prices[i]){ + answer[i] += 1; + } else { + break; + } + } + } + + return answer; + } +} From 03076d23fd3da5630006d2d4a78f9a3d9a97544e Mon Sep 17 00:00:00 2001 From: sujung Date: Sat, 25 Feb 2023 23:04:54 +0900 Subject: [PATCH 2/3] remove --- sujunghwang/week05/PRG_42584.java | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 sujunghwang/week05/PRG_42584.java diff --git a/sujunghwang/week05/PRG_42584.java b/sujunghwang/week05/PRG_42584.java deleted file mode 100644 index 6592743..0000000 --- a/sujunghwang/week05/PRG_42584.java +++ /dev/null @@ -1,27 +0,0 @@ -/* -https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=java - -주어진 배열을 탐색하면서 현재 값보다 작은 값이 언제 나오는지 구하면 되는 문제 -문제 카테고리는 스택/큐 였는데 모르겠어서 그냥 배열로 이중 for loop 돌림 -효율성 문제 생길 줄 알았는데 안생겨서 그냥 넘어가기로 함 -*/ - -class Solution { - public int[] solution(int[] prices) { - int len = prices.length; - int[] answer = new int[len]; - - for(int i = 0; i < len-1; i++){ - answer[i] = 1; - for(int j = i+1; j < len-1; j++){ - if(prices[j] >= prices[i]){ - answer[i] += 1; - } else { - break; - } - } - } - - return answer; - } -} From dbcf4abb831cf4905428c2d0b8d622ea8ff528d6 Mon Sep 17 00:00:00 2001 From: sujung Date: Sat, 25 Feb 2023 23:31:24 +0900 Subject: [PATCH 3/3] =?UTF-8?q?Week05=20SWEA=201248=20=EA=B3=B5=ED=86=B5?= =?UTF-8?q?=EC=A1=B0=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\206\265\354\241\260\354\203\201.java" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "sujunghwang/week05/SWEA_\352\263\265\355\206\265\354\241\260\354\203\201.java" diff --git "a/sujunghwang/week05/SWEA_\352\263\265\355\206\265\354\241\260\354\203\201.java" "b/sujunghwang/week05/SWEA_\352\263\265\355\206\265\354\241\260\354\203\201.java" new file mode 100644 index 0000000..a98d880 --- /dev/null +++ "b/sujunghwang/week05/SWEA_\352\263\265\355\206\265\354\241\260\354\203\201.java" @@ -0,0 +1,91 @@ +package B형특강; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class SWEA_공통조상 { + + static int V, E, A, B; + static Node[] nodes; + static List ancestorA, ancestorB; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + + StringTokenizer st; + + for (int tc = 1; tc <= T; tc++) { + // 정점의 개수 V(10 ≤ V ≤ 10000), 간선의 개수 E, 공통 조상을 찾는 두 개의 정점 번호 (A,B). + st = new StringTokenizer(br.readLine()); + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + A = Integer.parseInt(st.nextToken()); + B = Integer.parseInt(st.nextToken()); + nodes = new Node[V+1]; + ancestorA = new ArrayList<>(); + ancestorB = new ArrayList<>(); + + for (int i = 0; i < V + 1; i++) { + nodes[i] = new Node(); + } + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < E; i++) { + int p = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + nodes[p].children.add(c); + nodes[c].parents = p; + } + + // 조상 리스트 + traverse(A, ancestorA); + traverse(B, ancestorB); + + int ans = 0; + for (int i = 0; i < V; i++) { + if(!ancestorA.get(i).equals(ancestorB.get(i))) break; + ans = ancestorA.get(i); + } + System.out.printf("#%d %d %d\n", tc, ans, dfs(ans)); + } + } + + // 서브트리 개수 구하기 + public static int dfs(int idx){ + if(nodes[idx].children.size() == 0) return 1; + int res = 1; + // 자식 노드의 서브트리 개수를 구하는 방법 + for (int child : nodes[idx].children) { + res += dfs(child); + } + return res; + } + + public static void traverse(int idx, List ancestor){ + int parent = nodes[idx].parents; + if(parent != 0){ + traverse(parent, ancestor); + } + // 루트 노드부터 순서대로 조상 추가됨 + ancestor.add(idx); + } + + public static class Node { + List children; + int parents; + + Node() { + this.children = new ArrayList<>(); + this.parents = 0; + } + } +} +/* + +*/ +