diff --git a/minsung/S2_BinarySearch/BOJ_12015_LonggestIncreas.java b/minsung/S2_BinarySearch/BOJ_12015_LonggestIncreas.java new file mode 100644 index 0000000..d82b2bf --- /dev/null +++ b/minsung/S2_BinarySearch/BOJ_12015_LonggestIncreas.java @@ -0,0 +1,142 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class BOJ_12015_LonggestIncreas { + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + StringTokenizer st = new StringTokenizer(br.readLine()); + + // 이떄 맨 앞 부터 linkedlist 에 넣어주는데, + // 두가지 케이스 + // 1. 증가가 가능한 상태라면, 가장 뒤 인덱스에 넣어줌 + // 2. 증가가 불가능한 상태라면? + // 넣을 위치를 이분탐색으로 찾고, 교체 함 + // 왜 교체냐? 추후에 수열의 구성이 바뀔 수 있음 + // 교체를 해도 문제가 없는이유? 어차피 증가수열의 최대 길이만 궁금하기 떄문에 길이에는 변화가 없음 + + int[] arr = new int[N]; + + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(st.nextToken()); + } + ArrayList ans = new ArrayList<>(); + // 인덱스 예쁘게 맞춰주자 + ans.add(0); + + for (int i = 0; i < N; i++) { + int input = arr[i]; + // 증가가 가능한 경우 + if (input > ans.get(ans.size() - 1)) { + ans.add(input); + } else { + // 교체할 위치를 찾자 + int left = 1; + int right = ans.size() - 1; + + while (left <= right) { + int mid = (left + right) / 2; + + if (ans.get(mid) < input) + left = mid + 1; + else + right = mid - 1; + + } + ans.set(left, input); + } + } + + System.out.println(ans.size() - 1); + + } +} + +//import java.io.BufferedReader; +//import java.io.IOException; +//import java.io.InputStreamReader; +//import java.util.ArrayList; +//import java.util.List; +//import java.util.StringTokenizer; +// +//public class BOJ_12015_LonggestIncreas { +// static int[] arr; +// static int mid; +// +// public static void main(String[] args) throws NumberFormatException, IOException { +// +// BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); +// +// int N = Integer.parseInt(br.readLine()); +// +// StringTokenizer st = new StringTokenizer(br.readLine()); +// +// arr = new int[N]; +// +// for (int i = 0; i < N; i++) { +// arr[i] = Integer.parseInt(st.nextToken()); +// } +// +// // 증가하는 수열의 갯수로 가능한 수를 이분탐색으로 찾아볼것임 +// // 최소 한개는 증가수열 +// int left = 1; +// // 최대 N개가 증가수열 +// int right = N; +// boolean flag = false; +// while (left <= right) { +// // mid 개로 증가수열 가능? +// mid = (left + right) / 2; +// // mid개로 조합 뽑아 +// combination = new ArrayList<>(); +// comb(0); +// // 모든 조합중에서 항상 증가수열인게 있는가? +// for (List li : combination) { +// flag = false; +// for (int i = 0; i < mid - 1; i++) { +// // 한번이라도 감소 or 같다면 +// if (li.get(i + 1) - li.get(i) <= 0) { +// flag = true; +// break; +// } +// +// } +// // 한번도 감소를 안했다면 ->증가수열 +// if (!flag) +// break; +// } +// +// // mid 개로 증가수열 가능하다 -> 하지만 더 많은 개수로 가능하다면 그게 답이 된다. +// // 더 큰 범위를 탐색해봐야한다. +// if (!flag) +// left = mid + 1; +// // mid 개로 증가수열을 만들 수 없다면 -> 더 좁은 범위를 탐색해 봐야한다. +// else +// right = mid - 1; +// +// } +// +// System.out.println(right); +// +// } +// +// static List> combination = new ArrayList<>(); +// static List current = new ArrayList<>(); +// +// static void comb(int idx) { +// if (current.size() == mid) { +// combination.add(new ArrayList<>(current)); +// return; +// } +// +// for (int i = idx; i < arr.length; i++) { +// current.add(arr[i]); +// comb(i + 1); +// current.remove(current.size() - 1); +// } +// +// } +// +//} diff --git a/minsung/S2_BinarySearch/BOJ_1477_BuildRestArea.java b/minsung/S2_BinarySearch/BOJ_1477_BuildRestArea.java new file mode 100644 index 0000000..9a26cc8 --- /dev/null +++ b/minsung/S2_BinarySearch/BOJ_1477_BuildRestArea.java @@ -0,0 +1,119 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.StringTokenizer; + +public class BOJ_1477_BuildRestArea { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + int L = Integer.parseInt(st.nextToken()); + + List highway = new LinkedList<>(); + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + + highway.add(Integer.parseInt(st.nextToken())); + + } + + highway.add(0); + highway.add(L); + + Collections.sort(highway); + // 휴게소의 간격을 찾는 이분탐색방법 + int left = 1; + int right = L; + int mid = 0;// 찾고자 하는 휴게소의 간격 + while (left <= right) { + + mid = (left + right) / 2; + + int cnt = 0; // 해당 간격일 때 지어지는 화장실 수 + for (int i = 0; i < N + 1; i++) { + + cnt += (highway.get(i + 1) - highway.get(i) - 1) / mid; + + } + + // 더 지어야 한다면? 간격을 좁혀라 + if (cnt <= M) { + right = mid - 1; + } else { + // 개수가 지어야할 수 보다 많으면? 화장실 간격을 넖혀야 함 + left = mid + 1; + } + + } + + System.out.println(left); + + } +} + +//최초의 풀이 : 그리디 방식 -> 그리디는 언제 최적해를 찾지 못하는가?? +// public static void main(String[] args) throws IOException { +// +// BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); +// +// StringTokenizer st = new StringTokenizer(br.readLine()); +// +// int N = Integer.parseInt(st.nextToken()); +// int M = Integer.parseInt(st.nextToken()); +// int L = Integer.parseInt(st.nextToken()); +// +// List highway = new LinkedList<>(); +// st = new StringTokenizer(br.readLine()); +// for (int i = 0; i < N; i++) { +// +// highway.add(Integer.parseInt(st.nextToken())); +// +// } +// +// highway.add(0); +// highway.add(L); +// +// Collections.sort(highway); +// +// for (int i = 0; i < M; i++) { +// int maxDiff = -1; +// int build = -1; +// int idx = -1; +// for (int j = 0; j < highway.size() - 1; j++) { +// +// int f = highway.get(j); +// int r = highway.get(j + 1); +// +// if (r - f > maxDiff) { +// maxDiff = r - f; +// build = (r + f) / 2; +// idx = j + 1; +// } +// } +// +// highway.add(idx, build); +// } +// +// int ans = -1; +// for (int j = 0; j < highway.size() - 1; j++) { +// +// int f = highway.get(j); +// int r = highway.get(j + 1); +// +// ans = Math.max(ans, r - f); +// } +// +// System.out.println(ans); +// +// br.close(); +// +// } \ No newline at end of file diff --git a/minsung/S2_BinarySearch/BOJ_2295_SumOfThreeNums.java b/minsung/S2_BinarySearch/BOJ_2295_SumOfThreeNums.java new file mode 100644 index 0000000..b3f8932 --- /dev/null +++ b/minsung/S2_BinarySearch/BOJ_2295_SumOfThreeNums.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; + +public class BOJ_2295_SumOfThreeNums { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int N = Integer.parseInt(br.readLine()); + + int[] arr = new int[N]; + + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(br.readLine()); + + } + + Arrays.sort(arr); + + HashSet hashset = new HashSet<>(); + + for (int i = 0; i < N; i++) { + for (int j = i; j < N; j++) { + hashset.add(arr[i] + arr[j]); + } + } + int ans = 01; + out: for (int i = N - 1; i >= 0; i--) { + for (int j = i; j >= 0; j--) { + if (hashset.contains(arr[i] - arr[j])) { + ans = i; + + break out; + } + } + } + + System.out.println(arr[ans]); + + br.close(); + + } + +} \ No newline at end of file diff --git a/minsung/S2_BinarySearch/BOJ_2792_JewelryBox.java b/minsung/S2_BinarySearch/BOJ_2792_JewelryBox.java new file mode 100644 index 0000000..26ad1fc --- /dev/null +++ b/minsung/S2_BinarySearch/BOJ_2792_JewelryBox.java @@ -0,0 +1,52 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class BOJ_2792_JewelryBox { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + int[] jewelry = new int[M]; + for (int i = 0; i < M; i++) { + jewelry[i] = Integer.parseInt(br.readLine()); + } + + Arrays.sort(jewelry); + + // 나눠주는 최소 보석 갯수 + int left = 1; + // 나눠주는 최대 보석 갯수 + int right = jewelry[M - 1]; + + while (left <= right) { + int mid = (left + right) / 2; + // 몇명에게 나눠 주었나. + int cnt = 0; + + for (int i = 0; i < M; i++) { + if (jewelry[i] % mid == 0) + cnt += jewelry[i] / mid; + else + cnt += 1 + jewelry[i] / mid; + } + + if (cnt <= N) + right = mid - 1; + else + left = mid + 1; + + } + + System.out.println(left); + + } + +} diff --git a/minsung/S2_BinarySearchTree/BOJ_1202_Theif.java b/minsung/S2_BinarySearchTree/BOJ_1202_Theif.java new file mode 100644 index 0000000..a3ded5b --- /dev/null +++ b/minsung/S2_BinarySearchTree/BOJ_1202_Theif.java @@ -0,0 +1,61 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.PriorityQueue; +import java.util.StringTokenizer; +import java.util.TreeMap; + +class Qubic { + int weight; + int price; + + public Qubic(int weight, int price) { + this.weight = weight; + this.price = price; + } +} + +public class BOJ_1202_Theif { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + PriorityQueue pq = new PriorityQueue<>((a, b) -> b.price - a.price); + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + pq.add(new Qubic(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()))); + } + + TreeMap weightMap = new TreeMap<>(); + for (int i = 0; i < K; i++) { + int weight = Integer.parseInt(br.readLine()); + weightMap.put(weight, weightMap.getOrDefault(weight, 0) + 1); + } + + long ans = 0L; + + while (!pq.isEmpty()) { + Qubic qubic = pq.poll(); + + // 무게일치 + if (weightMap.containsKey(qubic.weight) && weightMap.get(qubic.weight) > 0) { + weightMap.put(qubic.weight, weightMap.get(qubic.weight) - 1); + ans += qubic.price; + } else { + // 일치안하면 가능한 가방중 제일작은거 + Integer availableWeight = weightMap.ceilingKey(qubic.weight); + if (availableWeight != null && weightMap.get(availableWeight) > 0) { + weightMap.put(availableWeight, weightMap.get(availableWeight) - 1); + ans += qubic.price; + } + } + } + + System.out.println(ans); + } +} diff --git a/minsung/S2_BinarySearchTree/BOJ_21939_Recommend.java b/minsung/S2_BinarySearchTree/BOJ_21939_Recommend.java new file mode 100644 index 0000000..882bdb8 --- /dev/null +++ b/minsung/S2_BinarySearchTree/BOJ_21939_Recommend.java @@ -0,0 +1,82 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.StringTokenizer; +import java.util.TreeSet; + +class Problem implements Comparable { + int P; + int L; + + public Problem(int p, int l) { + P = p; + L = l; + } + + @Override + public int compareTo(Problem other) { + // 난이도로 비교, 난이도가 같다면 문제 번호로 비교 + if (this.L != other.L) { + return Integer.compare(this.L, other.L); + } else { + return Integer.compare(this.P, other.P); + } + } + +} + +public class BOJ_21939_Recommend { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + HashMap hash = new HashMap<>(); + + TreeSet ts = new TreeSet<>(); + + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int P = Integer.parseInt(st.nextToken()); + Problem pro = new Problem(P, Integer.parseInt(st.nextToken())); + ts.add(pro); + hash.put(P, pro); + } + + int K = Integer.parseInt(br.readLine()); + + for (int i = 0; i < K; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + String action = st.nextToken(); + + switch (action) { + case "recommend": + int opt = Integer.parseInt(st.nextToken()); + if (opt == 1) { + System.out.println(ts.pollLast().P); + } else { + System.out.println(ts.pollFirst().P); + + } + break; + case "add": + int P = Integer.parseInt(st.nextToken()); + int L = Integer.parseInt(st.nextToken()); + Problem pro = new Problem(P, L); + ts.add(pro); + hash.put(P, pro); + + break; + case "solved": + int p = Integer.parseInt(st.nextToken()); + + ts.remove(hash.remove(p)); + break; + } + + } + + } + +} diff --git a/minsung/S2_BinarySearchTree/BOJ_5639_Tree.java b/minsung/S2_BinarySearchTree/BOJ_5639_Tree.java new file mode 100644 index 0000000..5cec1af --- /dev/null +++ b/minsung/S2_BinarySearchTree/BOJ_5639_Tree.java @@ -0,0 +1,81 @@ +import java.io.IOException; +import java.util.Scanner; + +class Node { + + Node left; + Node right; + int data; + + public Node(int data) { + + this.data = data; + + } + +} + +class BinaryTree { + public BinaryTree() { + } + + Node root; + + void insertTree(int N) { + if (root == null) { + root = new Node(N); + } else { + recurInsert(root, N); + } + } + + private void recurInsert(Node root, int value) { + + if (value < root.data) { + if (root.left == null) + root.left = new Node(value); + else + recurInsert(root.left, value); + + } else { + if (root.right == null) + root.right = new Node(value); + else + recurInsert(root.right, value); + } + + } + +} + +public class BOJ_5639_Tree { + + public static void main(String[] args) throws IOException { + + Scanner sc = new Scanner(System.in); + + BinaryTree tree = new BinaryTree(); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + if (line.isEmpty()) + break; // 빈 줄에서 종료 + int N = Integer.parseInt(line.trim()); // 문자열로 읽고 정수로 변환 + tree.insertTree(N); + } + + reverse(tree.root); + System.out.println(sb.toString()); + + } + + static StringBuilder sb = new StringBuilder(); + + public static void reverse(Node node) { + if (node.left != null) + reverse(node.left); + if (node.right != null) + reverse(node.right); + sb.append(node.data).append("\n"); + } + +} \ No newline at end of file diff --git a/minsung/S2_Graph/BOJ_1707_BipartiteGraph.java b/minsung/S2_Graph/BOJ_1707_BipartiteGraph.java new file mode 100644 index 0000000..2e5e5b9 --- /dev/null +++ b/minsung/S2_Graph/BOJ_1707_BipartiteGraph.java @@ -0,0 +1,64 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class BOJ_1707_BipartiteGraph { + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + int T = Integer.parseInt(br.readLine()); + + for (int t = 1; t <= T; t++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int V = Integer.parseInt(st.nextToken()); + int E = Integer.parseInt(st.nextToken()); + + // LinkedList -> ArrayList로 변경 + List> adjList = new ArrayList<>(); + + for (int i = 0; i <= V; i++) { + adjList.add(new ArrayList<>()); + } + + for (int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + adjList.get(a).add(b); + adjList.get(b).add(a); + } + + Queue queue = new LinkedList<>(); + int[] colors = new int[V + 1]; + boolean flag = false; + + out: for (int i = 1; i <= V; i++) { + if (colors[i] == 0) { + colors[i] = 1; + queue.add(i); + + while (!queue.isEmpty()) { + int tmp = queue.poll(); + + for (int vertex : adjList.get(tmp)) { + if (colors[vertex] == 0) { + colors[vertex] = -colors[tmp]; + queue.add(vertex); + } else if (colors[vertex] == colors[tmp]) { + flag = true; + break out; + } + } + } + } + } + + // println 대신 StringBuilder 사용 + sb.append(flag ? "NO" : "YES").append("\n"); + } + + // 한 번에 출력 + System.out.print(sb); + } +} \ No newline at end of file diff --git a/minsung/S2_Graph/BOJ_2617_FindGusel.java b/minsung/S2_Graph/BOJ_2617_FindGusel.java new file mode 100644 index 0000000..74a7c96 --- /dev/null +++ b/minsung/S2_Graph/BOJ_2617_FindGusel.java @@ -0,0 +1,71 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.StringTokenizer; + +public class BOJ_2617_FindGusel { + static List> leAdjList = new ArrayList<>(); + static List> grAdjList = new ArrayList<>(); + static int V, E; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + + for (int i = 0; i <= V; i++) { + grAdjList.add(new ArrayList<>()); + leAdjList.add(new ArrayList<>()); + } + + for (int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + grAdjList.get(b).add(a); + leAdjList.get(a).add(b); + } + + int mid = (V + 1) / 2; + int ans = 0; + int bigger, less; + + for (int i = 1; i <= V; i++) { + less = bfs(i, leAdjList); + bigger = bfs(i, grAdjList); + if (bigger >= mid || less >= mid) + ans++; + } + + System.out.println(ans); + } + + static int bfs(int start, List> adjList) { + boolean[] visited = new boolean[V + 1]; + int count = 0; + Queue queue = new LinkedList<>(); + + visited[start] = true; + queue.offer(start); + + while (!queue.isEmpty()) { + int curr = queue.poll(); + for (int i : adjList.get(curr)) { + if (!visited[i]) { + visited[i] = true; + count++; + queue.offer(i); + } + } + } + + return count; + } +} \ No newline at end of file diff --git a/minsung/S2_Hash/BOJ_1351_InfintySeq.java b/minsung/S2_Hash/BOJ_1351_InfintySeq.java new file mode 100644 index 0000000..9855d25 --- /dev/null +++ b/minsung/S2_Hash/BOJ_1351_InfintySeq.java @@ -0,0 +1,37 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class BOJ_1351_InfintySeq { + static Map hash = new HashMap<>(); + static int P, Q; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + long N = sc.nextLong(); + P = sc.nextInt(); + Q = sc.nextInt(); + + hash.put(0l, 1l); + + System.out.println(seq(N)); + + + sc.close(); + + } + + static long seq(long N) { + + if (hash.containsKey(N)) + return hash.get(N); + + if (hash.containsKey(N / P) && hash.containsKey(N / Q)) + hash.put(N, hash.get(N / P) + hash.get(N / Q)); + + return seq(N / P) + seq(N / Q); + + } + +} diff --git a/minsung/S2_Hash/BOJ_20166_HoSuk.java b/minsung/S2_Hash/BOJ_20166_HoSuk.java new file mode 100644 index 0000000..3eea7c9 --- /dev/null +++ b/minsung/S2_Hash/BOJ_20166_HoSuk.java @@ -0,0 +1,64 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.StringTokenizer; + +public class BOJ_20166_HoSuk { + + static HashMap map = new HashMap<>(); + static int[] dx = {-1, 1, 0, 0, -1, -1, 1, 1}; + static int[] dy = {0, 0, -1, 1, -1, 1, -1, 1}; + static int n, m; + static char[][] land; + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + int k = Integer.parseInt(st.nextToken()); + + land = new char[n][m]; + + for (int i = 0; i < n; i++) { + land[i] = br.readLine().toCharArray(); + } + + //모든 곳에서 시작 + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + dfs(i, j, 1, land[i][j] + ""); + } + } + + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < k; i++) { + String s = br.readLine(); + sb.append(map.getOrDefault(s, 0)).append("\n"); + } + + System.out.print(sb); + } + + private static void dfs(int x, int y, int len, String now) { + + + //getOrDefault 쩌네 + map.put(now, map.getOrDefault(now, 0) + 1); + + if (len == 5) { + return; + } + + for (int i = 0; i < 8; i++) { + int nx = (x + dx[i] + n) % n; + int ny = (y + dy[i] + m) % m; + + dfs(nx, ny, len + 1, now + land[nx][ny]); + } + } +} \ No newline at end of file diff --git a/minsung/S2_Hash/BOJ_2143_SumOfTwoArr.java b/minsung/S2_Hash/BOJ_2143_SumOfTwoArr.java new file mode 100644 index 0000000..db09a5a --- /dev/null +++ b/minsung/S2_Hash/BOJ_2143_SumOfTwoArr.java @@ -0,0 +1,67 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class BOJ_2143_SumOfTwoArr { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + long T = sc.nextLong(); + int n = sc.nextInt(); + int[] A = new int[n]; + long[] sumA = new long[n * (n + 1) / 2]; + for (int i = 0; i < n; i++) { + A[i] = sc.nextInt(); + } + int idx = 0; + for (int i = 0; i < n; i++) { + long tmp = 0; + for (int j = i; j < n; j++) { + tmp += A[j]; + sumA[idx++] = tmp; + } + } + int m = sc.nextInt(); + int[] B = new int[m]; + long[] sumB = new long[m * (m + 1) / 2]; + for (int i = 0; i < m; i++) { + B[i] = sc.nextInt(); + } + idx = 0; + for (int i = 0; i < m; i++) { + long tmp = 0; + for (int j = i; j < m; j++) { + tmp += B[j]; + sumB[idx++] = T - tmp; + } + } + + Arrays.sort(sumA); + Arrays.sort(sumB); + + + int left = 0; + int right = 0; + long ans = 0; + while (left < sumA.length && right < sumB.length) { + if (sumA[left] == sumB[right]) { + long countA = 1; + long countB = 1; + left++; + right++; + while (left < sumA.length && sumA[left] == sumA[left - 1]) { + countA++; + left++; + } + while (right < sumB.length && sumB[right] == sumB[right - 1]) { + countB++; + right++; + } + ans += countA * countB; + } else if (sumA[left] < sumB[right]) { + left++; + } else { + right++; + } + } + System.out.println(ans); + } +} \ No newline at end of file diff --git a/minsung/S2_MST/boj_1197.java b/minsung/S2_MST/boj_1197.java new file mode 100644 index 0000000..4419e30 --- /dev/null +++ b/minsung/S2_MST/boj_1197.java @@ -0,0 +1,80 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +public class Main { + + static int[] parent; + + static int find(int x) { + if (parent[x] == x) + return x; + return parent[x] = find(parent[x]); + } + + static void union(int a, int b) { + a = find(a); + b = find(b); + if (a < b) + parent[b] = a; + else + parent[a] = b; + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + + int V = Integer.parseInt(st.nextToken()); + int E = Integer.parseInt(st.nextToken()); + + List edges = new ArrayList<>(); + + for (int i = 0; i < E; i++) { + st = new StringTokenizer(br.readLine()); + + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + edges.add(new int[] { a, b, c }); + + } + + Collections.sort(edges, (a, b) ->Integer.compare(a[2],b[2])); + + // Union-Find 초기화 + parent = new int[V + 1]; + for (int i = 1; i <= V; i++) { + parent[i] = i; + } + + long totalWeight = 0; + int count = 0; // 선택한 간선의 개수 + + for (int[] edge : edges) { + int a = edge[0]; + int b = edge[1]; + int cost = edge[2]; + + // 사이클이 생기지 않으면 선택 + if (find(a) != find(b)) { + union(a, b); + totalWeight += cost; + count++; + } + + // V-1개의 간선을 선택하면 종료 + if (count == V - 1) + break; + } + + System.out.println(totalWeight); + } + +} \ No newline at end of file diff --git a/minsung/S2_MST/boj_1647.java b/minsung/S2_MST/boj_1647.java new file mode 100644 index 0000000..f612f76 --- /dev/null +++ b/minsung/S2_MST/boj_1647.java @@ -0,0 +1,83 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; + +public class Main { + + static List edges; + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int M = sc.nextInt(); + + edges = new ArrayList<>(); + for (int i = 0; i < M; i++) { + + } + + for (int i = 0; i < M; i++) { + int a = sc.nextInt(); + int b = sc.nextInt(); + int c = sc.nextInt(); + + edges.add(new int[] { a, b, c }); + + } + ans = 0; + + Collections.sort(edges, (a, b) -> Integer.compare(a[2], b[2])); + + parent = new int[N + 1]; + for (int i = 1; i <= N; i++) { + parent[i] = i; + } + int maxCost = 0; + for (int[] edge : edges) { + + int a = edge[0]; + int b = edge[1]; + int c = edge[2]; + + if (find(a) != find(b)) { + union(a, b); + ans += c; + maxCost = c; + } + + } + + System.out.println(ans - maxCost); + + } + + static int find(int x) { + + if (x != parent[x]) { + return find(parent[x]); + } + + return x; + + } + + static void union(int a, int b) { + + a = find(a); + b = find(b); + + if (a < b) + parent[b] = a; + else + parent[a] = b; + + } + + static int ans; + + static int[] parent; + +} \ No newline at end of file diff --git a/minsung/S2_PriorityQueue/BOJ_13975_File.java b/minsung/S2_PriorityQueue/BOJ_13975_File.java new file mode 100644 index 0000000..147bdc8 --- /dev/null +++ b/minsung/S2_PriorityQueue/BOJ_13975_File.java @@ -0,0 +1,40 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +public class BOJ_13975_File { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + StringBuilder sb = new StringBuilder(); + for (int t = 1; t <= T; t++) { + + int N = Integer.parseInt(br.readLine()); + + PriorityQueue pq = new PriorityQueue<>(); + + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + pq.add(Long.parseLong(st.nextToken())); + } + + long ans = 0; + while (pq.size() != 1) { + long tmp = pq.poll() + pq.poll(); + + ans += tmp; + pq.add(tmp); + + } + + sb.append(ans).append("\n"); + + } + System.out.println(sb.toString()); + } + +} diff --git a/minsung/S2_PriorityQueue/BOJ_1655_SayMid.java b/minsung/S2_PriorityQueue/BOJ_1655_SayMid.java new file mode 100644 index 0000000..fc0d927 --- /dev/null +++ b/minsung/S2_PriorityQueue/BOJ_1655_SayMid.java @@ -0,0 +1,40 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.PriorityQueue; + +public class BOJ_1655_SayMid { + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int N = Integer.parseInt(br.readLine()); + // 작은 수들의 최대힙 + PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); + // 큰 수들의 최소힙 + PriorityQueue minHeap = new PriorityQueue<>(); + + for(int i = 0; i < N; i++) { + int num = Integer.parseInt(br.readLine()); + + // 두 힙의 크기가 같으면 maxHeap에 넣음 + if(maxHeap.size() == minHeap.size()) { + maxHeap.add(num); + } else { + minHeap.add(num); + } + + // maxHeap의 최대값이 minHeap의 최소값보다 크면 교환 + if(!minHeap.isEmpty() && maxHeap.peek() > minHeap.peek()) { + int tmp = maxHeap.poll(); + maxHeap.add(minHeap.poll()); + minHeap.add(tmp); + } + + sb.append(maxHeap.peek()).append('\n'); + } + + System.out.print(sb); + } +} \ No newline at end of file diff --git a/minsung/S2_PriorityQueue/BOJ_1715_SortCard.java b/minsung/S2_PriorityQueue/BOJ_1715_SortCard.java new file mode 100644 index 0000000..a22bc47 --- /dev/null +++ b/minsung/S2_PriorityQueue/BOJ_1715_SortCard.java @@ -0,0 +1,37 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.PriorityQueue; + +public class BOJ_1715_SortCard { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + + int N = Integer.parseInt(br.readLine()); + + if (N == 1) { + System.out.println(0); + return; + } + PriorityQueue pq = new PriorityQueue<>(); + + for (int i = 0; i < N; i++) { + pq.add(Integer.parseInt(br.readLine())); + } + long ans = 0; + while (pq.size() != 1) { + int tmp = pq.poll() + pq.poll(); + + ans += tmp; + pq.add(tmp); + + } + + System.out.println(ans); + + } + +} diff --git a/minsung/S2_Tree/BOJ_15681 b/minsung/S2_Tree/BOJ_15681 new file mode 100644 index 0000000..b33414a --- /dev/null +++ b/minsung/S2_Tree/BOJ_15681 @@ -0,0 +1,70 @@ +import java.util.*; + +public class Main { + static ArrayList> tree; + static ArrayList> children; + static int[] parent; + static int[] size; // 각 노드를 루트로 하는 서브트리의 크기 + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int R = sc.nextInt(); + int Q = sc.nextInt(); + + + tree = new ArrayList<>(); + children = new ArrayList<>(); + for(int i = 0; i <= N; i++) { + tree.add(new ArrayList<>()); + children.add(new ArrayList<>()); + } + + parent = new int[N + 1]; + size = new int[N + 1]; + + + for(int i = 0; i < N-1; i++) { + int U = sc.nextInt(); + int V = sc.nextInt(); + + tree.get(U).add(V); + tree.get(V).add(U); + } + + + makeTree(R, -1); + + + countSubtreeSize(R); + + + for(int i = 0; i < Q; i++) { + int U = sc.nextInt(); + System.out.println(size[U]); + } + } + + + static void makeTree(int current, int prev) { + for(int next : tree.get(current)) { + if(next != prev) { + children.get(current).add(next); + parent[next] = current; + makeTree(next, current); + } + } + } + + + static int countSubtreeSize(int node) { + size[node] = 1; + + for(int child : children.get(node)) { + size[node] += countSubtreeSize(child); + } + + return size[node]; + } +} diff --git a/minsung/Week1_Stack/BOJ_10773_Zero.java b/minsung/Week1_Stack/BOJ_10773_Zero.java new file mode 100644 index 0000000..6166baa --- /dev/null +++ b/minsung/Week1_Stack/BOJ_10773_Zero.java @@ -0,0 +1,33 @@ +package Stack; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BOJ_10773_Zero { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + + Stack stack = new Stack<>(); + for (int t = 0; t < T; t++) { + int k = Integer.parseInt(br.readLine()); + if (k == 0) { + stack.pop(); + } else { + stack.push(k); + } + } + + int sum = 0; + for (int i = 0; i < stack.size(); i++) { + sum += stack.get(i); + } + + System.out.println(sum); + + } + +} diff --git a/minsung/Week1_Stack/BOJ_10799_Stick.java b/minsung/Week1_Stack/BOJ_10799_Stick.java new file mode 100644 index 0000000..e4f029a --- /dev/null +++ b/minsung/Week1_Stack/BOJ_10799_Stick.java @@ -0,0 +1,35 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BOJ_10799_Stick { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String str = br.readLine(); + + Stack laser = new Stack<>(); + Stack stick = new Stack<>(); + + int[] laserP = new int[str.length()]; + int index = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '(') { + if (laser.empty()) { + laser.push(1); + } + + } else if (str.charAt(i) == ')') { + if (!laser.empty()) { + laserP[index++] = i; + laser.pop(); + } + } + } + + + + } + +} diff --git a/minsung/Week1_Stack/BOJ_10799_stick.java b/minsung/Week1_Stack/BOJ_10799_stick.java new file mode 100644 index 0000000..aea20c0 --- /dev/null +++ b/minsung/Week1_Stack/BOJ_10799_stick.java @@ -0,0 +1,36 @@ +package Stack; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BOJ_10799_stick { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String str = br.readLine(); + Stack stick = new Stack<>(); + int result = 0; + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '(') { + // 막대 또는 레이저 시작 + stick.push(str.charAt(i)); + + } else if (str.charAt(i) == ')' && str.charAt(i - 1) == '(') { + // 레이저 -> 현재 막대 개수만큼 result 에 추가 + stick.pop(); + result += stick.size(); + } else if (str.charAt(i) == ')' && str.charAt(i - 1) == ')') { + // 막대의 끝. 막대 개수 1개 추가. + stick.pop(); + result += 1; + } + } + + System.out.println(result); + + br.close(); + } + +} diff --git a/minsung/Week1_Stack/BOJ_10828_Stack.java b/minsung/Week1_Stack/BOJ_10828_Stack.java new file mode 100644 index 0000000..019fbd6 --- /dev/null +++ b/minsung/Week1_Stack/BOJ_10828_Stack.java @@ -0,0 +1,90 @@ +package Stack; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +public class BOJ_10828_Stack { + static int size = -1; + + static List stack = new ArrayList<>(); + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringBuilder sb = new StringBuilder(); + + int N = Integer.parseInt(br.readLine()); + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + String opt = st.nextToken(); + switch (opt) { + case "pop": + System.out.println(pop()); + break; + case "push": + + push(Integer.parseInt(st.nextToken())); + break; + case "size": + System.out.println(size()); + break; + case "empty": + empty(); + break; + case "top": + top(); + break; + + } + } + + } + + static void push(int input) { + stack.add(input); + size++; + + } + + static int pop() { + + if (size >= 0) { + int result = stack.get(size); + stack.remove(size); + size--; + return result; + + } else { + return -1; + } + } + + static int size() { + return size + 1; + + } + + static void empty() { + if (size >= 0) { + System.out.println("0"); + } else { + System.out.println("1"); + } + + } + + static void top() { + if (size >= 0) { + System.out.println(stack.get(size)); + } else { + System.out.println("-1"); + } + } +} diff --git a/minsung/Week1_Stack/BOJ_1874_StackSequence.java b/minsung/Week1_Stack/BOJ_1874_StackSequence.java new file mode 100644 index 0000000..47152fa --- /dev/null +++ b/minsung/Week1_Stack/BOJ_1874_StackSequence.java @@ -0,0 +1,60 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +/* + * N개의 수를 수열로 늘어놓으므로 + * N번 pop 해야함. + * 넣어야 할 수가 현재 수보다 크다면 그 수까지 push + * 넣어야 할 수가 현재 수보다 작다면 그냥 pop + * 하지만 pop할 때, 내가 찾고자 하는 수열이 안나온다면 no + * + */ +public class BOJ_1874_StackSequence { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int N = Integer.parseInt(br.readLine()); + + int[] arr = new int[N]; + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(br.readLine()); + } + + Stack stack = new Stack<>(); + + int value = 0; + // N번의 pop + for (int i = 0; i < N; i++) { + + // 넣어야 할 수와 현재의 수를 비교 하는 과정. + if (arr[i] > value) { + + int lastvalue = value; + + value = arr[i]; + // 현재 수 부터, 넣어야 하는 수 까지 push + for (int j = lastvalue + 1; j <= value; j++) { + stack.push(j); + sb.append("+\n"); + } + } else if (arr[i] != stack.peek()) { + // 내가 원하는 수열이 안나오면 프로그램 종료 + System.out.println("NO"); + return; + } + + stack.pop(); + sb.append("-\n"); + } + + System.out.println(sb.toString()); + + br.close(); + + } + +} diff --git a/minsung/Week1_Stack/BOJ_2841_AilenGuitar.java b/minsung/Week1_Stack/BOJ_2841_AilenGuitar.java new file mode 100644 index 0000000..29c22b4 --- /dev/null +++ b/minsung/Week1_Stack/BOJ_2841_AilenGuitar.java @@ -0,0 +1,67 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; +import java.util.StringTokenizer; + +public class BOJ_2841_AilenGuitar { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + StringTokenizer st1 = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st1.nextToken()); + int P = Integer.parseInt(st1.nextToken()); + // n+1개의 스택을 만들고 초기화 + // 형변환 설명해 주실분 구함... + Stack[] stack = (Stack[]) new Stack[N + 1]; + for (int i = 0; i < N + 1; i++) { + stack[i] = new Stack<>(); + } + + int[] maxP = new int[N + 1]; + int cnt = 0; + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int n = Integer.parseInt(st.nextToken()); + int p = Integer.parseInt(st.nextToken()); + // 비었을땐 넣고 +1 + if (stack[n].empty()) { + stack[n].push(p); + cnt++; + } else if (stack[n].peek() < p) { + // 손가락보다 높은 음을 연주할 때는 손가락 하나만 붙이면 되니까 +1 과 푸시 + stack[n].push(p); + cnt++; + } else if (stack[n].peek() > p) { + // 손가락보다 낮은 음을 연주할 때는 연주해야되는 음보다 우선 손가락이 같거나 낮아져야함. + while (stack[n].peek() > p) { + // 높은음이면 일단 손가락을 빼라 +1 + stack[n].pop(); + cnt++; + if (stack[n].empty()) { + // 그러다 손가락이 다 떨어지면 연주해야 하는 음을 연주 +1 + stack[n].push(p); + cnt++; + break; + } + // 연주해야할 음을 이미 누르고 있다면, 손가락의 이동은 더이상 없으므로 반복문 탈출 + if (stack[n].peek() == p) + break; + else if (stack[n].peek() < p) { + // 연주해야할 음보다 낮은음을 누르고 있다면 손가락 붙여라 +1 + stack[n].push(p); + cnt++; + break; + } + } + } + + } + + System.out.println(cnt); + + } + +} diff --git a/minsung/Week1_Stack/BOJ_3986_GoodWord.java b/minsung/Week1_Stack/BOJ_3986_GoodWord.java new file mode 100644 index 0000000..1277ed5 --- /dev/null +++ b/minsung/Week1_Stack/BOJ_3986_GoodWord.java @@ -0,0 +1,39 @@ +package Stack; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BOJ_3986_GoodWord { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int ans = 0; + int T = Integer.parseInt(br.readLine()); + for (int t = 1; t <= T; ++t) { + String input = br.readLine(); + Stack stk = new Stack<>(); + + for (int i = 0; i < input.length(); ++i) { + if (stk.empty()) { + stk.push(input.charAt(i)); + } else { + if (stk.peek() == input.charAt(i)) { + stk.pop(); + } else { + stk.push(input.charAt(i)); + } + } + } + + if (stk.empty()) + ans++; + + } + + System.out.println(ans); + } + +} diff --git a/minsung/Week1_Stack/BOJ_9012_VPS.java b/minsung/Week1_Stack/BOJ_9012_VPS.java new file mode 100644 index 0000000..e31a5f3 --- /dev/null +++ b/minsung/Week1_Stack/BOJ_9012_VPS.java @@ -0,0 +1,39 @@ +package Stack; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BOJ_9012_VPS { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + out: for (int t = 0; t < N; t++) { + String str = br.readLine(); + + Stack VPS = new Stack<>(); + for (int i = 0; i < str.length(); i++) { + if (str.charAt(i) == '(') { + VPS.push(str.charAt(i)); + } else { + if (VPS.empty()) { + System.out.println("NO"); + continue out; + } + VPS.pop(); + } + } + if (VPS.empty()) { + System.out.println("YES"); + } else { + System.out.println("NO"); + } + } + + br.close(); + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_1021_RotationQueue.java b/minsung/Week2_QueueDeque/BOJ_1021_RotationQueue.java new file mode 100644 index 0000000..b15773e --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_1021_RotationQueue.java @@ -0,0 +1,68 @@ +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Scanner; + +public class BOJ_1021_RotationQueue { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int M = sc.nextInt(); + + Deque deque = new ArrayDeque<>(); + + for (int i = 1; i <= N; i++) { + deque.offer(i); + } + + int ans = 0; + // 왼쪽으로 이동하는게 횟수가 적을지 + // 오른쪽으로 이동하는게 횟수가 적을지 비교 + + for (int t = 0; t < M; t++) { + int element = sc.nextInt(); + int leftcnt = 0; + // 왼쪽으로 해보고 + while (deque.peek() != element) { + deque.offerLast(deque.pollFirst()); + leftcnt++; + } + // 원상복구 + for (int i = 0; i < leftcnt; i++) { + deque.offerFirst(deque.pollLast()); + } + int rightcnt = 0; + // 오른쪽으로 해보고 + while (deque.peek() != element) { + deque.offerFirst(deque.pollLast()); + rightcnt++; + } + // 원상복구 + for (int i = 0; i < rightcnt; i++) { + deque.offerLast(deque.pollFirst()); + } + + // 횟수 비교후 카운팅 + if (leftcnt < rightcnt) { + while (deque.peek() != element) { + deque.offerLast(deque.pollFirst()); + } + deque.poll(); + ans += leftcnt; + } else { + while (deque.peek() != element) { + deque.offerFirst(deque.pollLast()); + } + deque.poll(); + ans += rightcnt; + + } + + } + + System.out.println(ans); + + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_1158_YoSe.java b/minsung/Week2_QueueDeque/BOJ_1158_YoSe.java new file mode 100644 index 0000000..a772df6 --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_1158_YoSe.java @@ -0,0 +1,99 @@ +import java.util.Scanner; + +public class BOJ_1158_YoSe { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int K = sc.nextInt(); + + CircularQueue circularQueue = new CircularQueue(N); + + for (int i = 0; i < N; i++) { + circularQueue.enqueue(i+1); + } + StringBuilder sb = new StringBuilder(); + sb.append("<"); + + int index = 1; + while (!circularQueue.isEmpty()) { + if (index++ % K == 0) + sb.append(circularQueue.dequeue()).append(", "); + else { + circularQueue.enqueue(circularQueue.dequeue()); + } + + } + sb.setLength(sb.length() - 2); + sb.append(">"); + System.out.println(sb.toString()); + + } + +} + +class CircularQueue { + private Object[] object; + private int front; + private int rear; + private int size; + private int capacity; + + public CircularQueue(int capacity) { + this.capacity = capacity; + this.object = new Object[capacity]; + this.front = -1; + this.rear = -1; + this.size = 0; + } + + public boolean isFull() { + return size == capacity; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean enqueue(Object object) { + if (isFull()) { + return false; + } + if (isEmpty()) { + front = 0; + } + rear = (rear + 1) % capacity; + this.object[rear] = object; + size++; + return true; + } + + public Object dequeue() { + if (isEmpty()) { + return null; + } + Object object = this.object[front]; + this.object[front] = null; + front = (front + 1) % capacity; + size--; + if (isEmpty()) { + front = -1; + rear = -1; + } + return object; + } + + public Object peek() { + if (isEmpty()) { + return -1; + } + return object[front]; + } + + public int getSize() { + return size; + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_13335_Truck.java b/minsung/Week2_QueueDeque/BOJ_13335_Truck.java new file mode 100644 index 0000000..ff722ec --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_13335_Truck.java @@ -0,0 +1,62 @@ +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_13335_Truck { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int W = sc.nextInt(); + int L = sc.nextInt(); + + Queue queue = new ArrayDeque<>(); + Queue waitQueue = new ArrayDeque<>(); + + for (int i = 0; i < N; i++) { + waitQueue.offer(sc.nextInt()); + + } + for (int i = 0; i < W; i++) { + queue.offer(0); + + } + + int num = 0; + int bridgeSum = 0; + int time = 0; + do { + time++; + int onTruck = waitQueue.peek(); + int outTruck = queue.peek(); + if (bridgeSum - outTruck + onTruck <= L) { + if (queue.poll() != 0) + num--; + queue.offer(waitQueue.poll()); + bridgeSum = bridgeSum - outTruck + onTruck; + num++; + } else { + if (queue.poll() != 0) { + num--; + bridgeSum -= outTruck; + + } + queue.offer(0); + } + + } while (waitQueue.size() != 0); + + while (num != 0) { + time++; + if (queue.poll() != 0) { + num--; + } + } + + System.out.println(time); + + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_1966_PrinterQueue.java b/minsung/Week2_QueueDeque/BOJ_1966_PrinterQueue.java new file mode 100644 index 0000000..75c5f00 --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_1966_PrinterQueue.java @@ -0,0 +1,61 @@ +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_1966_PrinterQueue { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int T = sc.nextInt(); + + for (int t = 1; t <= T; t++) { + int N = sc.nextInt(); + int M = sc.nextInt(); + + Queue queue = new ArrayDeque<>(); + int[] priArr = new int[N]; + // 입력 받기, 이때 우선순위 배열을 만들어서 최댓값 비교 쉽게 + for (int i = 0; i < N; i++) { + int tmp = sc.nextInt(); + queue.offer(new Paper(tmp, i)); + priArr[i] = tmp; + } + + Arrays.sort(priArr); + int index = N - 1; + int ans = 0; + + while (true) { + // 우선순위 가장 높은 paper 탐색 + while (queue.peek().priority < priArr[index]) { + queue.offer(queue.poll()); + } + // 우선 순위 가장 높은 paper 의 num 확인 + Paper paper = queue.poll(); + index--; + ans++; + if (paper.num == M) { + System.out.println(ans); + break; + } + } + + } + + } + +} + +class Paper { + int priority; + int num; + + public Paper(int priority, int num) { + super(); + this.priority = priority; + this.num = num; + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_2164_Card2.java b/minsung/Week2_QueueDeque/BOJ_2164_Card2.java new file mode 100644 index 0000000..667529d --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_2164_Card2.java @@ -0,0 +1,29 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_2164_Card2 { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + + Queue queue = new LinkedList<>(); + + for (int i = 1; i <= N; i++) { + queue.add(i); + } + + while (queue.size() != 1) { + queue.poll(); + + queue.add(queue.poll()); + } + + System.out.println(queue.poll()); + + } + +} diff --git a/minsung/Week2_QueueDeque/BOJ_2346_Ballon.java b/minsung/Week2_QueueDeque/BOJ_2346_Ballon.java new file mode 100644 index 0000000..930578a --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_2346_Ballon.java @@ -0,0 +1,47 @@ +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Scanner; + +public class BOJ_2346_Ballon { + static class Balloon { + int number; + int move; + + Balloon(int number, int move) { + this.number = number; + this.move = move; + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + Deque deque = new ArrayDeque<>(); + + for (int i = 1; i <= N; i++) { + int move = sc.nextInt(); + deque.add(new Balloon(i, move)); + } + + StringBuilder sb = new StringBuilder(); + + while (!deque.isEmpty()) { + Balloon current = deque.pollFirst(); + sb.append(current.number).append(" "); + + if (deque.isEmpty()) break; + + if (current.move > 0) { + for (int i = 0; i < current.move - 1; i++) { + deque.addLast(deque.pollFirst()); + } + } else { + for (int i = 0; i < Math.abs(current.move); i++) { + deque.addFirst(deque.pollLast()); + } + } + } + + System.out.println(sb.toString().trim()); + } +} \ No newline at end of file diff --git a/minsung/Week2_QueueDeque/BOJ_24511_QueueStack.java b/minsung/Week2_QueueDeque/BOJ_24511_QueueStack.java new file mode 100644 index 0000000..b4f1f38 --- /dev/null +++ b/minsung/Week2_QueueDeque/BOJ_24511_QueueStack.java @@ -0,0 +1,41 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.StringTokenizer; + +public class BOJ_24511_QueueStack { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + ArrayDeque queue = new ArrayDeque<>(); + int N = Integer.parseInt(br.readLine()); + int[] arr = new int[N]; + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + + arr[i] = Integer.parseInt(st.nextToken()); + } + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + int tmp = Integer.parseInt(st.nextToken()); + if (arr[i] == 0) { + // 스택은 아무 의미 없음 큐만 챙겨라! + queue.offerFirst(tmp); + } + } + StringBuilder sb = new StringBuilder(); + int M = Integer.parseInt(br.readLine()); + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < M; i++) { + // 순서대로 queue 에서 뽑기 + queue.offer(Integer.parseInt(st.nextToken())); + sb.append(queue.poll()).append(" "); + } + System.out.println(sb.toString().trim()); + + } + +} diff --git a/minsung/Week3_IM/BOJ_10157.java b/minsung/Week3_IM/BOJ_10157.java new file mode 100644 index 0000000..f823662 --- /dev/null +++ b/minsung/Week3_IM/BOJ_10157.java @@ -0,0 +1,57 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class BOJ_10157 { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int r = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(br.readLine()); + if (K > r * c) { + System.out.println(0); + br.close(); + return; + } + + boolean[][] map = new boolean[r][c]; + + int[] dr = { 0, 1, 0, -1 }; + int[] dc = { 1, 0, -1, 0 }; + + int nr = 0; + int nc = 0; + int x = 0; + int y = 0; + int k = 1; + int index = 0; + while (k < K) { + map[nr][nc] = true; + nr = x + dr[index]; + nc = y + dc[index]; + + k++; + + if (nr < 0 || nr >= r || nc < 0 || nc >= c || map[nr][nc]) { + index = (index + 1) % 4; + nr = x + dr[index]; + nc = y + dc[index]; + } + + x = nr; + y = nc; + } + + StringBuilder sb = new StringBuilder(); + sb.append(nr + 1).append(" ").append(nc + 1); + System.out.println(sb.toString()); + + br.close(); + + } + +} \ No newline at end of file diff --git a/minsung/Week3_IM/BOJ_10158.java b/minsung/Week3_IM/BOJ_10158.java new file mode 100644 index 0000000..f628881 --- /dev/null +++ b/minsung/Week3_IM/BOJ_10158.java @@ -0,0 +1,66 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class BOJ_10158 { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int w = Integer.parseInt(st.nextToken()); + int h = Integer.parseInt(st.nextToken()); + + st = new StringTokenizer(br.readLine()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + + int time = Integer.parseInt(br.readLine()); + + // 주기가 있음 + // 가로로만 봤을때 2w시간 후 (방향까지 같게) 제자리로 복귀 + // 세로로만 봤을때 2h시간 후 (방향까지 같게) 제자리로 복귀 + + // 그러므로 time 의 나머지를 각각 구한다. + + int moveX = time % (2 * w); + int moveY = time % (2 * h); + + // 가로만 따로 생각 + int nx = x; + int dx = 1; + for (int i = 0; i < moveX; i++) { + if (nx + dx >= 0 && nx + dx <= w) { + nx = nx + dx; + continue; + } else { + // 벽에 막히면 방향 전환ㅁ + dx *= -1; + nx = nx + dx; + } + + } + + // 세로만 따로 생각 + int ny = y; + int dy = 1; + for (int i = 0; i < moveY; i++) { + if (ny + dy >= 0 && ny + dy <= h) { + ny = ny + dy; + continue; + } else { + // 벽에 막히면 방향 전환ㅁ + dy *= -1; + ny = ny + dy; + } + + } + + System.out.println(nx + " " + ny); + + br.close(); + } + +} diff --git a/minsung/Week3_IM/BOJ_10163.java b/minsung/Week3_IM/BOJ_10163.java new file mode 100644 index 0000000..5dc8582 --- /dev/null +++ b/minsung/Week3_IM/BOJ_10163.java @@ -0,0 +1,38 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + + public static void main(String[] args) throws NumberFormatException, IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + int[][] map = new int[1001][1001]; + for (int t = 1; t <= T; t++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int r = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + + for (int i = r; i < r + x; i++) { + for (int j = c; j < c + y; j++) { + map[i][j] = t; + } + } + } + int[] ans = new int[T+1]; + for (int i = 0; i < 1001; i++) { + for (int j = 0; j < 1001; j++) { + ans[map[i][j]]++; + } + } + + for (int t = 1; t <= T; t++) { + System.out.println(ans[t]); + } + } + +} diff --git a/minsung/Week3_IM/BOJ_1244.java b/minsung/Week3_IM/BOJ_1244.java new file mode 100644 index 0000000..9ebd57b --- /dev/null +++ b/minsung/Week3_IM/BOJ_1244.java @@ -0,0 +1,63 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class BOJ_1244 { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + boolean[] arr = new boolean[N]; + for (int i = 0; i < N; i++) { + arr[i] = sc.nextInt() == 1 ? true : false; + } + int M = sc.nextInt(); + for (int i = 0; i < M; i++) { + int G = sc.nextInt(); + // 남학생 + if (G == 1) { + int k = sc.nextInt(); + + for (int j = 0; j < N; j++) { + if ((j + 1) % k == 0) { + arr[j] = !arr[j]; + } + } + + } else if (G == 2) { + // 여학생 + int k = sc.nextInt(); + int index = 1; + while (true) { + if (k - 1 - index >= 0 && k - 1 + index < N && arr[k - 1 - index] == arr[k - 1 + index]) { + arr[k - 1 - index] = !arr[k - 1 - index]; + arr[k - 1 + index] = !arr[k - 1 + index]; + + index++; + } else { + + arr[k - 1] = !arr[k - 1]; + break; + + } + + } + + } + } // 학생입력끝 + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < N; i++) { + if (arr[i]) { + sb.append(1).append(" "); + } else { + sb.append(0).append(" "); + } + if (i % 20 == 19) { + sb.append("\n"); + } + } + System.out.println(sb.toString()); + + } + +} diff --git a/minsung/Week3_IM/BOJ_13300.java b/minsung/Week3_IM/BOJ_13300.java new file mode 100644 index 0000000..6bb0617 --- /dev/null +++ b/minsung/Week3_IM/BOJ_13300.java @@ -0,0 +1,41 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class BOJ_13300 { + + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + int[][] arr = new int[6][2]; + for (int i = 0; i < N; i++) { + StringTokenizer st2 = new StringTokenizer(br.readLine()); + int S = Integer.parseInt(st2.nextToken()); + int Y = Integer.parseInt(st2.nextToken()); + arr[Y - 1][S]++; + } + int ans = 0; + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 2; j++) { + while (arr[i][j] != 0) { + if (arr[i][j] >= K) { + arr[i][j] -= K; + ans++; + } else if (arr[i][j] < K) { + arr[i][j] = 0; + ans++; + } + } + } + } + + System.out.println(ans); + br.close(); + + } + +} diff --git a/minsung/Week3_IM/BOJ_14696.java b/minsung/Week3_IM/BOJ_14696.java new file mode 100644 index 0000000..5412899 --- /dev/null +++ b/minsung/Week3_IM/BOJ_14696.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + + public static void main(String[] args) throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + for (int t = 1; t <= N; t++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int[] arrA = new int[4]; + int A = Integer.parseInt(st.nextToken()); + for (int a = 0; a < A; a++) { + arrA[Integer.parseInt(st.nextToken()) - 1]++; + + } + StringTokenizer st2 = new StringTokenizer(br.readLine()); + int[] arrB = new int[4]; + int B = Integer.parseInt(st2.nextToken()); + for (int b = 0; b < B; b++) { + arrB[Integer.parseInt(st2.nextToken()) - 1]++; + + } + boolean drawFlag = true; + for (int i = 3; i >= 0; i--) { + if (arrA[i] == arrB[i]) { + continue; + } else if (arrA[i] > arrB[i]) { + System.out.println("A"); + drawFlag = false; + break; + } else { + System.out.println("B"); + drawFlag = false; + break; + } + + } + if (drawFlag) { + System.out.println("D"); + } + } + + } +} diff --git a/minsung/Week3_IM/BOJ_2309.java b/minsung/Week3_IM/BOJ_2309.java new file mode 100644 index 0000000..add0514 --- /dev/null +++ b/minsung/Week3_IM/BOJ_2309.java @@ -0,0 +1,34 @@ +import java.util.Arrays; +import java.util.Scanner; + +public class BOJ_2309 { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int[] arr = new int[9]; + int sum = 0; + for (int i = 0; i < 9; i++) { + arr[i] = sc.nextInt(); + sum += arr[i]; + } + int index1; + int index2; + + out: for (int i = 0; i < 9; i++) { + for (int j = i + 1; j < 9; j++) { + if (sum - arr[i] - arr[j] == 100) { + arr[i] = 0; + arr[j] = 0; + break out; + } + } + } + + Arrays.sort(arr); + for (int i = 2; i < 9; i++) { + System.out.println(arr[i]); + } + } + +} diff --git a/minsung/Week3_IM/BOJ_2491.java b/minsung/Week3_IM/BOJ_2491.java new file mode 100644 index 0000000..0e6783f --- /dev/null +++ b/minsung/Week3_IM/BOJ_2491.java @@ -0,0 +1,45 @@ +import java.util.Scanner; + +public class BOJ_2491 { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + + int[] arr = new int[N]; + for (int i = 0; i < N; i++) { + arr[i] = sc.nextInt(); + } + + int max = 1; + int cnt = 1; + for (int i = 0; i < N - 1; i++) { + if (arr[i] <= arr[i + 1]) { + cnt++; + if (cnt > max) { + max = cnt; + + } + } else { + cnt = 1; + } + } + cnt = 1; + for (int i = 0; i < N - 1; i++) { + if (arr[i] >= arr[i + 1]) { + cnt++; + if (cnt > max) { + max = cnt; + + } + } else { + cnt = 1; + } + } + + System.out.println(max); + + } + +} diff --git a/minsung/Week3_IM/BOJ_2559.java b/minsung/Week3_IM/BOJ_2559.java new file mode 100644 index 0000000..a37b796 --- /dev/null +++ b/minsung/Week3_IM/BOJ_2559.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +public class BOJ_2559 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int K = sc.nextInt(); + + int[] tarr = new int[N]; + int[] sum = new int[N - K + 1]; + + for (int i = 0; i < N; i++) { + tarr[i] = sc.nextInt(); + } + int max = Integer.MIN_VALUE; + for (int i = 0; i < N - K + 1; i++) { + for (int j = 0; j < K; j++) { + sum[i] += tarr[i + j]; + } + if (max < sum[i]) + max = sum[i]; + } + System.out.println(max); + } + +} diff --git a/minsung/Week3_IM/BOJ_2563.java b/minsung/Week3_IM/BOJ_2563.java new file mode 100644 index 0000000..57cde43 --- /dev/null +++ b/minsung/Week3_IM/BOJ_2563.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +public class BOJ_2563 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + + int[][] map = new int[100][100]; + for (int t = 0; t < N; t++) { + int r = sc.nextInt(); + int c = sc.nextInt(); + + for (int i = r; i < r + 10; i++) { + for (int j = c; j < c + 10; j++) { + map[i][j] = 1; + } + } + } + + int ans = 0; + + for (int i = 0; i < 100; i++) { + for (int j = 0; j < 100; j++) { + if (map[i][j] == 1) + ans++; + } + } + + System.out.println(ans); + + } + +} diff --git a/minsung/Week3_IM/BOJ_2578.java b/minsung/Week3_IM/BOJ_2578.java new file mode 100644 index 0000000..a23b63a --- /dev/null +++ b/minsung/Week3_IM/BOJ_2578.java @@ -0,0 +1,78 @@ +import java.util.Scanner; + +public class BOJ_2578 { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int[][] map = new int[5][5]; + + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + map[i][j] = sc.nextInt(); + } + } + for (int i = 0; i < 25; i++) { + int k = sc.nextInt(); + out: for (int r = 0; r < 5; r++) { + for (int c = 0; c < 5; c++) { + if (map[r][c] == k) { + map[r][c] = 0; + break out; + } + } + + } + if (bingocnt(map) >= 3) { + System.out.println(i + 1); + return; + } + } + sc.close(); + + } + + static int bingocnt(int[][] arr) { + int result = 0; + + for (int i = 0; i < 5; i++) { + int cnt = 0; + for (int j = 0; j < 5; j++) { + if (arr[i][j] == 0) + cnt++; + + } + if (cnt == 5) + result++; + } + + for (int i = 0; i < 5; i++) { + int cnt = 0; + for (int j = 0; j < 5; j++) { + if (arr[j][i] == 0) + cnt++; + + } + if (cnt == 5) + result++; + } + int cnt = 0; + for (int i = 0; i < 5; i++) { + if (arr[i][i] == 0) + cnt++; + + if (cnt == 5) + result++; + } + cnt = 0; + for (int i = 0; i < 5; i++) { + if (arr[i][4 - i] == 0) + cnt++; + + if (cnt == 5) + result++; + } + return result; + } + +} diff --git a/minsung/Week3_IM/BOJ_2605.java b/minsung/Week3_IM/BOJ_2605.java new file mode 100644 index 0000000..019c53e --- /dev/null +++ b/minsung/Week3_IM/BOJ_2605.java @@ -0,0 +1,35 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +public class BOJ_2605 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + + int[] arr = new int[N]; + for (int i = 0; i < N; i++) { + arr[i] = sc.nextInt(); + } + List students = new ArrayList<>(); + + for (int i = 0; i < N; i++) { + + if (students.isEmpty()) { + students.add(i + 1); + continue; + } + students.add(students.size() - arr[i], i + 1); + + } + + for (int i = 0; i < N; i++) { + System.out.print(students.get(i)+" "); + + } + + } + +} diff --git a/minsung/Week3_IM/BOJ_2628.java b/minsung/Week3_IM/BOJ_2628.java new file mode 100644 index 0000000..90f2a9e --- /dev/null +++ b/minsung/Week3_IM/BOJ_2628.java @@ -0,0 +1,46 @@ +import java.util.LinkedList; +import java.util.List; +import java.util.Scanner; + +public class BOJ_2628 { + public static void main(String[] args) { + List colcut = new LinkedList<>(); + List rowcut = new LinkedList<>(); + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int M = sc.nextInt(); + int t = sc.nextInt(); + for (int i = 0; i < t; i++) { + if (sc.nextInt() == 0) { + rowcut.add(sc.nextInt()); + } else { + colcut.add(sc.nextInt()); + } + } + rowcut.add(0); + rowcut.add(M); + colcut.add(0); + colcut.add(N); + rowcut.sort(null); + colcut.sort(null); + + int rowmax = -1; + + for (int i = 0; i < rowcut.size() - 1; i++) { + int tmp = rowcut.get(i + 1) - rowcut.get(i); + if (tmp > rowmax) { + rowmax = tmp; + } + } + int colmax = -1; + for (int i = 0; i < colcut.size() - 1; i++) { + int tmp = colcut.get(i + 1) - colcut.get(i); + if (tmp > colmax) { + colmax = tmp; + } + } + + System.out.println(rowmax * colmax); + + } +} \ No newline at end of file diff --git a/minsung/Week3_IM/BOJ_2635.java b/minsung/Week3_IM/BOJ_2635.java new file mode 100644 index 0000000..e3b0ab4 --- /dev/null +++ b/minsung/Week3_IM/BOJ_2635.java @@ -0,0 +1,60 @@ +import java.util.Scanner; + +public class BOJ_2635 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int maxLength = 0; // 최대 수열 길이 + int bestA = 0; + + + for (int i = 0; i <= N; i++) { + int cnt = 3; // 초기 수열 길이는 N, a, b로 3 + int a = i; + int b = N - a; + StringBuilder sb = new StringBuilder(); + sb.append(N).append(" ").append(a).append(" ").append(b).append(" "); + + // 수열 생성 + while (true) { + int c = a - b; + if (c < 0) { + break; + } + cnt++; + sb.append(c).append(" "); + a = b; + b = c; + } + + // 현재 수열 길이가 최대 길이보다 길면 갱신 + if (cnt > maxLength) { + maxLength = cnt; + bestA = i; + } + } + + + System.out.println(maxLength); + + + int a = bestA; + int b = N - a; + StringBuilder sb = new StringBuilder(); + sb.append(N).append(" ").append(a).append(" ").append(b).append(" "); + + while (true) { + int c = a - b; + if (c < 0) { + break; + } + sb.append(c).append(" "); + a = b; + b = c; + } + + + System.out.println(sb.toString().trim()); + } +} \ No newline at end of file diff --git a/minsung/Week3_IM/BOJ_2669.java b/minsung/Week3_IM/BOJ_2669.java new file mode 100644 index 0000000..d330150 --- /dev/null +++ b/minsung/Week3_IM/BOJ_2669.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class BOJ_2669 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int[][] map = new int[101][101]; + for (int t = 1; t <= 4; t++) { + int a = sc.nextInt(); + int b = sc.nextInt(); + int c = sc.nextInt(); + int d = sc.nextInt(); + + for (int i = a; i < c; i++) { + for (int j = b; j < d; j++) { + map[i][j] = 1; + + } + } + } + int ans = 0; + for (int i = 0; i <= 100; i++) { + for (int j = 0; j <= 100; j++) { + if (map[i][j] == 1) + ans++; + } + } + System.out.println(ans); + } + +} diff --git a/minsung/Week4_Recursion/BOJ_1992_QuadTree.java b/minsung/Week4_Recursion/BOJ_1992_QuadTree.java new file mode 100644 index 0000000..8a1dd32 --- /dev/null +++ b/minsung/Week4_Recursion/BOJ_1992_QuadTree.java @@ -0,0 +1,52 @@ +import java.util.Scanner; + +public class BOJ_1992_QuadTree { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + + int[][] map = new int[N][N]; + + for (int i = 0; i < N; i++) { + String str = sc.next(); + for (int j = 0; j < N; j++) { + map[i][j] = str.charAt(j) - '0'; + } + } + Paper(map, 0, 0, N); + + } + + static void Paper(int[][] map, int r, int c, int N) { + int check = map[r][c]; + boolean flag = false; + out: for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (map[r + i][c + j] != check) { + flag = true; + break out; + } + } + } + + if (flag) { + System.out.print("("); + Paper(map, r, c, N / 2); + Paper(map, r, c + N / 2, N / 2); + Paper(map, r + N / 2, c, N / 2); + Paper(map, r + N / 2, c + N / 2, N / 2); + System.out.print(")"); + } else { + + if (check == 1) { + System.out.print(1); + } else { + System.out.print(0); + + } + } + + } +} \ No newline at end of file diff --git a/minsung/Week4_Recursion/BOJ_2630_Paper.java b/minsung/Week4_Recursion/BOJ_2630_Paper.java new file mode 100644 index 0000000..41eba11 --- /dev/null +++ b/minsung/Week4_Recursion/BOJ_2630_Paper.java @@ -0,0 +1,52 @@ +import java.util.Scanner; + +public class BOJ_2630_Paper { + static int blue = 0; + static int white = 0; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + + int[][] map = new int[N][N]; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + map[i][j] = sc.nextInt(); + } + } + Paper(map, 0, 0, N); + System.out.println(white); + System.out.println(blue); + + } + + static void Paper(int[][] map, int r, int c, int N) { + int check = map[r][c]; + boolean flag = false; + out: for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (map[r + i][c + j] != check) { + flag = true; + break out; + } + } + } + + if (flag) { + Paper(map, r, c, N / 2); + Paper(map, r + N / 2, c, N / 2); + Paper(map, r, c + N / 2, N / 2); + Paper(map, r + N / 2, c + N / 2, N / 2); + } else { + + if (check == 1) + blue++; + else + white++; + } + + } + +} diff --git a/minsung/Week4_Recursion/BOJ_4779_Kantor.java b/minsung/Week4_Recursion/BOJ_4779_Kantor.java new file mode 100644 index 0000000..6578d26 --- /dev/null +++ b/minsung/Week4_Recursion/BOJ_4779_Kantor.java @@ -0,0 +1,38 @@ +import java.util.NoSuchElementException; +import java.util.Scanner; + +public class BOJ_4779_Kantor { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + while (true) { + + try { + int N = sc.nextInt(); + System.out.println(Kantor(N)); + + } catch (NoSuchElementException e) { + break; + } + + } + + } + + static String Kantor(int N) { + + if (N == 0) { + return "-"; + } + + StringBuilder sb = new StringBuilder(); + sb.append(Kantor(N - 1)); + for (int i = 0; i < Math.pow(3, N - 1); i++) { + sb.append(" "); + } + sb.append(Kantor(N - 1)); + + return sb.toString(); + } + +} diff --git a/minsung/Week5_BFS/BOJ_10026_RedGreen.java b/minsung/Week5_BFS/BOJ_10026_RedGreen.java new file mode 100644 index 0000000..bdb8533 --- /dev/null +++ b/minsung/Week5_BFS/BOJ_10026_RedGreen.java @@ -0,0 +1,146 @@ +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_10026_RedGreen { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int[][] map = new int[N][N]; + // R : 1 G: 2 B :3 + for (int i = 0; i < N; i++) { + String str = sc.next(); + for (int j = 0; j < N; j++) { + if (str.charAt(j) == 'R') + map[i][j] = 1; + else if (str.charAt(j) == 'G') + map[i][j] = 2; + else if (str.charAt(j) == 'B') + map[i][j] = 3; + } + } + + // 일반인 BFS + Queue queue = new ArrayDeque<>(); + boolean[][] visitedNormal = new boolean[N][N]; + + int[] dr = { 0, 1, 0, -1 }; + int[] dc = { 1, 0, -1, 0 }; + + int normalcnt = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!visitedNormal[i][j]) { + normalcnt++; + // 0 : row 1 :col + int[] tmp = new int[2]; + tmp[0] = i; + tmp[1] = j; + + visitedNormal[i][j] = true; + + queue.offer(tmp); + + while (!queue.isEmpty()) { + int[] nowP = new int[2]; + + nowP = queue.poll(); + + int r = nowP[0]; + int c = nowP[1]; + int color = map[r][c]; + + for (int k = 0; k < 4; k++) { + int nr = r + dr[k]; + int nc = c + dc[k]; + // 인덱스체크 && visited 체크 && 내 색상인지 체크 + if (nr >= 0 && nr < N && nc >= 0 && nc < N && !visitedNormal[nr][nc] + && map[nr][nc] == color) { + + visitedNormal[nr][nc] = true; + int[] next = new int[2]; + + next[0] = nr; + next[1] = nc; + + queue.offer(next); + + } + } + + } + } + } + } + + // 적록인 BFS + Queue queue2 = new ArrayDeque<>(); + boolean[][] visitedRedGreen = new boolean[N][N]; + int redGreenCnt = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!visitedRedGreen[i][j]) { + redGreenCnt++; + // 0 : row 1 :col + int[] tmp = new int[2]; + tmp[0] = i; + tmp[1] = j; + + visitedRedGreen[i][j] = true; + + queue2.offer(tmp); + + while (!queue2.isEmpty()) { + int[] nowP = new int[2]; + + nowP = queue2.poll(); + + int r = nowP[0]; + int c = nowP[1]; + int color = map[r][c]; + + for (int k = 0; k < 4; k++) { + int nr = r + dr[k]; + int nc = c + dc[k]; + // 인덱스체크 && visited 체크 && 내 색상인지 체크 + if (nr >= 0 && nr < N && nc >= 0 && nc < N && !visitedRedGreen[nr][nc]) { + + if (color == 1 || color == 2) { + if (map[nr][nc] == 1 || map[nr][nc] == 2) { + visitedRedGreen[nr][nc] = true; + int[] next = new int[2]; + + next[0] = nr; + next[1] = nc; + + queue2.offer(next); + } + } else if (color == 3) { + if (map[nr][nc] == 3) { + visitedRedGreen[nr][nc] = true; + int[] next = new int[2]; + + next[0] = nr; + next[1] = nc; + + queue2.offer(next); + } + } + + } + } + + } + } + } + } + System.out.println(normalcnt + " " + redGreenCnt); + + sc.close(); + + } + +} diff --git a/minsung/Week5_BFS/BOJ_1926_Grim.java b/minsung/Week5_BFS/BOJ_1926_Grim.java new file mode 100644 index 0000000..8b02e52 --- /dev/null +++ b/minsung/Week5_BFS/BOJ_1926_Grim.java @@ -0,0 +1,81 @@ +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_1926_Grim { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int M = sc.nextInt(); + + boolean[][] map = new boolean[N][M]; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (sc.nextInt() == 1) + map[i][j] = true; + + } + } + + boolean[][] visited = new boolean[N][M]; + + int[] dr = { 1, 0, -1, 0 }; + int[] dc = { 0, -1, 0, 1 }; + + Queue queue = new ArrayDeque<>(); + + int numGrim = 0; + int maxGrim = 0; + + // 모든 요소 순회 + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + // 그림 && 방문 안한 지점에서 BFS + if (map[i][j] && !visited[i][j]) { + numGrim++; + int surface = 1; + int[] info = new int[2]; + info[0] = i; + info[1] = j; + visited[i][j] = true; + + queue.offer(info); + + while (!queue.isEmpty()) { + + int[] tmp = new int[2]; + tmp = queue.poll(); + int r = tmp[0]; + int c = tmp[1]; + + for (int k = 0; k < 4; k++) { + int nr = r + dr[k]; + int nc = c + dc[k]; + + if (nr >= 0 && nr < N && nc >= 0 && nc < M && !visited[nr][nc] && map[nr][nc]) { + int[] newInfo = new int[3]; + newInfo[0] = nr; + newInfo[1] = nc; + visited[nr][nc] = true; + + queue.offer(newInfo); + surface++; + } + } + } + maxGrim = Math.max(maxGrim, surface); + + } + } + } + + System.out.println(numGrim); + System.out.println(maxGrim); + + } + +} diff --git a/minsung/Week5_BFS/BOJ_2178_Miro.java b/minsung/Week5_BFS/BOJ_2178_Miro.java new file mode 100644 index 0000000..9d05faa --- /dev/null +++ b/minsung/Week5_BFS/BOJ_2178_Miro.java @@ -0,0 +1,69 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_2178_Miro { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int M = sc.nextInt(); + + int[][] map = new int[N][M]; + boolean[][] visited = new boolean[N][M]; + + for (int i = 0; i < N; i++) { + String str = sc.next(); + for (int j = 0; j < M; j++) { + map[i][j] = str.charAt(j) - '0'; + } + } + + // 6시 9시 12시 3시 + int[] dr = { 1, 0, -1, 0 }; + int[] dc = { 0, -1, 0, 1 }; + // queue 에 현재 방문 지점의 row, col , 이동 횟수를 넣음 + Queue queue = new LinkedList<>(); + + int[] info = new int[3]; + info[0] = 0; + info[1] = 0; + visited[0][0] = true; + info[2] = 1; + + queue.add(info); + + while (!queue.isEmpty()) { + int[] tmp = queue.poll(); + + int r = tmp[0]; + int c = tmp[1]; + int cnt = tmp[2]; + + for (int i = 0; i < 4; i++) { + int nr = r + dr[i]; + int nc = c + dc[i]; + + if (nr >= 0 && nr < N && nc >= 0 && nc < M && !visited[nr][nc] && map[nr][nc] == 1) { + int[] newInfo = new int[3]; + newInfo[0] = nr; + newInfo[1] = nc; + newInfo[2] = cnt + 1; + visited[nr][nc] = true; + + if (nr == N - 1 && nc == M - 1) { + System.out.println(cnt + 1); + return; + } + + queue.offer(newInfo); + } + + } + + } + + } + +} diff --git a/minsung/Week5_BFS/BOJ_25418_MakeAK.java b/minsung/Week5_BFS/BOJ_25418_MakeAK.java new file mode 100644 index 0000000..1bca2fa --- /dev/null +++ b/minsung/Week5_BFS/BOJ_25418_MakeAK.java @@ -0,0 +1,42 @@ +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_25418_MakeAK { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); + int K = sc.nextInt(); + //bfs 탐색을 위한 queue 와 boolean 배열 + Queue queue = new ArrayDeque<>(); + boolean[] visited = new boolean[K + 1]; + + int cnt = 0; + int[] arr = { N, cnt }; + queue.offer(arr); + visited[N] = true; + + while (!queue.isEmpty()) { + int[] tmp = queue.poll(); + + if (tmp[0] == K) { + System.out.println(tmp[1]); + return; + } + if (tmp[0] * 2 <= K && !visited[tmp[0] * 2]) { + visited[tmp[0] * 2] = true; + queue.offer(new int[] { tmp[0] * 2, tmp[1] + 1 }); + } + if (tmp[0] + 1 <= K && !visited[tmp[0] + 1]) { + visited[tmp[0] + 1] = true; + queue.offer(new int[] { tmp[0] + 1, tmp[1] + 1 }); + } + + } + + } + +} diff --git a/minsung/Week5_BFS/BOJ_2583_Area.java b/minsung/Week5_BFS/BOJ_2583_Area.java new file mode 100644 index 0000000..95e8179 --- /dev/null +++ b/minsung/Week5_BFS/BOJ_2583_Area.java @@ -0,0 +1,86 @@ +import java.util.ArrayDeque; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_2583_Area { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int M = sc.nextInt(); + int N = sc.nextInt(); + int K = sc.nextInt(); + + boolean[][] map = new boolean[N][M]; + for (int i = 0; i < K; i++) { + + int r1 = sc.nextInt(); + int c1 = sc.nextInt(); + int r2 = sc.nextInt(); + int c2 = sc.nextInt(); + + for (int r = r1; r < r2; r++) { + for (int c = c1; c < c2; c++) { + + map[r][c] = true; + } + } + } + + int[] info = new int[2]; + Queue queue = new ArrayDeque<>(); + + int[] dx = { 0, 1, 0, -1 }; + int[] dy = { 1, 0, -1, 0 }; + + List list = new LinkedList<>(); + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + + if (map[i][j] == true) + continue; + + info[0] = i; + info[1] = j; + queue.offer(info); + + map[i][j] = true; + int area = 1; + while (!queue.isEmpty()) { + int[] tmp = queue.poll(); + + int nowX = tmp[0]; + int nowY = tmp[1]; + + for (int m = 0; m < 4; m++) { + int nx = nowX + dx[m]; + int ny = nowY + dy[m]; + + if (nx >= 0 && nx < N && ny >= 0 && ny < M && !map[nx][ny]) { + area++; + map[nx][ny] = true; + int[] newInfo = { nx, ny }; + queue.offer(newInfo); + + } + } + + } + if (area != 0) + list.add(area); + + } + } + + System.out.println(list.size()); + list.sort(null); + + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + " "); + } + } + +} diff --git a/minsung/Week5_BFS/BOJ_7562_KnightMove.java b/minsung/Week5_BFS/BOJ_7562_KnightMove.java new file mode 100644 index 0000000..96527ef --- /dev/null +++ b/minsung/Week5_BFS/BOJ_7562_KnightMove.java @@ -0,0 +1,60 @@ +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_7562_KnightMove { + + static int N; + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + int T = sc.nextInt(); + + for (int t = 1; t <= T; t++) { + // 0 : row 1: col 2: cnt + int[] info = new int[3]; + Queue queue = new ArrayDeque<>(); + + N = sc.nextInt(); + boolean[][] visited = new boolean[N][N]; + info[0] = sc.nextInt(); + info[1] = sc.nextInt(); + + int destR = sc.nextInt(); + int destC = sc.nextInt(); + queue.offer(info); + + int[] dr = { 2, 2, 1, 1, -2, -2, -1, -1 }; + int[] dc = { 1, -1, 2, -2, -1, 1, 2, -2 }; + + while (!queue.isEmpty()) { + int[] tmp = queue.poll(); + int nowR = tmp[0]; + int nowC = tmp[1]; + int cnt = tmp[2]; + + if (destR == nowR && destC == nowC) { + System.out.println(cnt); + break; + } + + for (int i = 0; i < 8; i++) { + if (canMove(nowR + dr[i], nowC + dc[i]) && !visited[nowR + dr[i]][nowC + dc[i]]) { + visited[nowR + dr[i]][nowC + dc[i]] = true; + queue.offer(new int[] { nowR + dr[i], nowC + dc[i], cnt + 1 }); + } + } + + } + + } + } + + static boolean canMove(int r, int c) { + + return r < N && r >= 0 && c < N && c >= 0; + } + +} diff --git a/minsung/Week5_BFS/BOJ_7569_tomato.java b/minsung/Week5_BFS/BOJ_7569_tomato.java new file mode 100644 index 0000000..572a43b --- /dev/null +++ b/minsung/Week5_BFS/BOJ_7569_tomato.java @@ -0,0 +1,97 @@ +import java.util.ArrayDeque; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_7569_tomato { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + // N행 M열 처리 + int M = sc.nextInt(); + int N = sc.nextInt(); + int H = sc.nextInt(); + + int[][][] map = new int[N][M][H]; + boolean[][][] visited = new boolean[N][M][H]; + Queue queue = new ArrayDeque<>(); + + int unripe = 0; + + for (int k = 0; k < H; k++) { + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + map[i][j][k] = sc.nextInt(); + // 이미 익은 토마토를 저장하고, 큐에 넣어야 함 + if (map[i][j][k] == 1) { + // 익은토마토에 또 방문할 일은 없으므로, + visited[i][j][k] = true; + + // r,c,z, 좌표와 day 정보 + int[] tmp = new int[4]; + tmp[0] = i; + tmp[1] = j; + tmp[2] = k; + tmp[3] = 0; + + queue.offer(tmp); + + } else if (map[i][j][k] == 0) + unripe++; + } + } + } + + int[] dr = { 0, 1, 0, -1, 0, 0 }; + int[] dc = { 1, 0, -1, 0, 0, 0 }; + int[] dz = { 0, 0, 0, 0, 1, -1 }; + // queue를 list로 초기화 + int lastDay = 0; + while (!queue.isEmpty()) { + + int[] tmp = queue.poll(); + int r = tmp[0]; + int c = tmp[1]; + int z = tmp[2]; + int day = tmp[3]; + + for (int i = 0; i < 6; i++) { + int nr = r + dr[i]; + int nc = c + dc[i]; + int nz = z + dz[i]; + + // 인덱스 체크 && 안익은 토마토인지 빈 공간인지 체크 && 방문을 했었는지 + if (nr >= 0 && nr < N && nc >= 0 && nc < M && nz >= 0 && nz < H && map[nr][nc][nz] == 0 + && !visited[nr][nc][nz]) { + + // if 문에 들어왔다 -> 안익은 토마토다 + map[nr][nc][nz] = 1; // 익힘 + visited[nr][nc][nz] = true; // 방문 + unripe--; // 안익은거 -1 + + int[] newTomato = new int[4]; + newTomato[0] = nr; + newTomato[1] = nc; + newTomato[2] = nz; + newTomato[3] = day + 1; + + if (lastDay < day + 1) + lastDay = day + 1; + + queue.offer(newTomato); + + } + } + + } + + if (unripe == 0) { + System.out.println(lastDay); + } else + System.out.println(-1); + + } + +} diff --git a/minsung/Week6_DFS/BOJ_1260_DFSBFS.java b/minsung/Week6_DFS/BOJ_1260_DFSBFS.java new file mode 100644 index 0000000..e816e2a --- /dev/null +++ b/minsung/Week6_DFS/BOJ_1260_DFSBFS.java @@ -0,0 +1,71 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; + +public class BOJ_1260_DFSBFS { + static int N, V, start; + static int[][] adjArr; + static boolean[] visited; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + N = sc.nextInt(); + V = sc.nextInt(); + start = sc.nextInt(); + + adjArr = new int[N + 1][N + 1]; + + for (int i = 0; i < V; i++) { + int A = sc.nextInt(); + int B = sc.nextInt(); + adjArr[A][B] = adjArr[B][A] = 1; + } + + visited = new boolean[N + 1]; + dfs(start, 0); + System.out.println(); + + visited = new boolean[N + 1]; + bfs(start); + sc.close(); + } + + static void dfs(int idx, int cnt) { + + visited[idx] = true; + System.out.print(idx + " "); + if (cnt == N) + return; + + for (int i = 1; i <= N; i++) { + if (adjArr[idx][i] == 1 && !visited[i]) { + dfs(i, cnt + 1); + } + } + } + + static void bfs(int idx) { + StringBuilder sb = new StringBuilder(); + visited[idx] = true; + + Queue queue = new LinkedList<>(); + queue.offer(idx); + + while (!queue.isEmpty()) { + int curr = queue.poll(); + sb.append(curr).append(" "); + + for (int i = 1; i <= N; i++) { + if (adjArr[curr][i] == 1 && !visited[i]) { + visited[i] = true; + queue.offer(i); + } + } + + } + System.out.println(sb.toString()); + + } + +} diff --git a/minsung/Week6_DFS/BOJ_1987_Alphabet.java b/minsung/Week6_DFS/BOJ_1987_Alphabet.java new file mode 100644 index 0000000..0a43e8d --- /dev/null +++ b/minsung/Week6_DFS/BOJ_1987_Alphabet.java @@ -0,0 +1,47 @@ +import java.util.*; + +public class BOJ_1987_Alphabet { + private static final int[] dx = { -1, 1, 0, 0 }; // 상하좌우 이동 + private static final int[] dy = { 0, 0, -1, 1 }; // 상하좌우 이동 + private static int R, C; + private static char[][] board; + private static boolean[] visited; + private static int maxCount = 0; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + R = scanner.nextInt(); + C = scanner.nextInt(); + board = new char[R][C]; + scanner.nextLine(); + + for (int i = 0; i < R; i++) { + board[i] = scanner.nextLine().toCharArray(); + } + + visited = new boolean[26]; // A-Z + + visited[board[0][0] - 'A'] = true; + dfs(0, 0, 1); + + System.out.println(maxCount); + } + + private static void dfs(int x, int y, int count) { + maxCount = Math.max(maxCount, count); + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (nx >= 0 && nx < R && ny >= 0 && ny < C) { + char nextChar = board[nx][ny]; + if (!visited[nextChar - 'A']) { + visited[nextChar - 'A'] = true; + dfs(nx, ny, count + 1); + visited[nextChar - 'A'] = false; + } + } + } + } +} diff --git a/minsung/Week6_DFS/BOJ_2606_virus.java b/minsung/Week6_DFS/BOJ_2606_virus.java new file mode 100644 index 0000000..389ef60 --- /dev/null +++ b/minsung/Week6_DFS/BOJ_2606_virus.java @@ -0,0 +1,41 @@ +import java.util.Scanner; + +public class BOJ_2606_virus { + static int N, V, start; + static int[][] adjArr; + static boolean[] visited; + static int cnt; + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + N = sc.nextInt(); + V = sc.nextInt(); + + adjArr = new int[N + 1][N + 1]; + visited = new boolean[N + 1]; + for (int i = 0; i < V; i++) { + int A = sc.nextInt(); + int B = sc.nextInt(); + adjArr[A][B] = adjArr[B][A] = 1; + } + cnt = 0; + dfs(1); + + System.out.println(cnt - 1); + + } + + static void dfs(int idx) { + cnt++; + visited[idx] = true; + + for (int i = 1; i <= N; i++) { + if (adjArr[idx][i] == 1 && !visited[i]) { + dfs(i); + } + } + + } +} diff --git a/minsung/temp.java b/minsung/temp.java deleted file mode 100644 index 1c1d9e5..0000000 --- a/minsung/temp.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.ssafy.ws.step3; - -public class temp { - public static void main(String[] args) { - System.out.println("test"); - } -}