From 2fab08fb4e2eedeb525b9bd1cd6e31abf568fbac Mon Sep 17 00:00:00 2001 From: Heize Date: Thu, 24 Mar 2022 17:20:14 +0900 Subject: [PATCH 001/111] [Baekjoon] 9612: Maximum Word Frequency --- sangryeol/Week_1/Maximum Word Frequency.java | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 sangryeol/Week_1/Maximum Word Frequency.java diff --git a/sangryeol/Week_1/Maximum Word Frequency.java b/sangryeol/Week_1/Maximum Word Frequency.java new file mode 100644 index 0000000..e934549 --- /dev/null +++ b/sangryeol/Week_1/Maximum Word Frequency.java @@ -0,0 +1,31 @@ +import java.io.*; +import java.util.*; + +public class BOJ_9612 { + static int N; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + HashMap hashMap = new HashMap<>(); + N = Integer.parseInt(br.readLine()); + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + String in = st.nextToken(); + if (hashMap.containsKey(in)) { + int value = hashMap.get(in); + hashMap.put(in, value + 1); + } + else { + hashMap.put(in, 1); + } + } + List> entryList = new ArrayList>(hashMap.entrySet()); + Collections.sort(entryList, new Comparator>() { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + if (o1.getValue() != o2.getValue()) return o2.getValue().compareTo(o1.getValue()); + return o2.getKey().compareTo(o1.getKey()); + } + }); + System.out.println(entryList.get(0).getKey() + " " + entryList.get(0).getValue()); + } +} From c07e87c37aa1c828c7556c6acceb54385d024134 Mon Sep 17 00:00:00 2001 From: Heize Date: Thu, 24 Mar 2022 17:26:05 +0900 Subject: [PATCH 002/111] =?UTF-8?q?[Baekjoon]=201654:=20=EB=9E=9C=EC=84=A0?= =?UTF-8?q?=20=EC=9E=90=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\236\220\353\245\264\352\270\260.java" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "sangryeol/Week_1/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.java" diff --git "a/sangryeol/Week_1/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.java" "b/sangryeol/Week_1/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.java" new file mode 100644 index 0000000..9522065 --- /dev/null +++ "b/sangryeol/Week_1/\353\236\234\354\204\240 \354\236\220\353\245\264\352\270\260.java" @@ -0,0 +1,31 @@ +import java.io.*; +import java.util.*; + +public class BOJ_1654 { + static int K, N; + static int[] lines; + public static long f() { + long lo = 0, hi = 1L << 31; + while (lo + 1 < hi) { + long mid = (lo + hi) / 2; + int count = 0; + for (int i = 0; i < K; i++) { + count += lines[i] / mid; + } + if (count >= N) lo = mid; + else hi = mid; + } + return lo; + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + K = Integer.parseInt(st.nextToken()); + N = Integer.parseInt(st.nextToken()); + lines = new int[N]; + for (int i = 0; i < K; i++) { + lines[i] = Integer.parseInt(br.readLine()); + } + System.out.println(f()); + } +} From 6a9068670180f0e978d52b265c4d445879384811 Mon Sep 17 00:00:00 2001 From: Heize Date: Thu, 24 Mar 2022 17:26:37 +0900 Subject: [PATCH 003/111] =?UTF-8?q?[Baekjoon]=2017390:=20=EC=9D=B4?= =?UTF-8?q?=EA=B1=B4=20=EA=BC=AD=20=ED=92=80=EC=96=B4=EC=95=BC=20=ED=95=B4?= =?UTF-8?q?!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\226\264\354\225\274 \355\225\264!.java" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "sangryeol/Week_1/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.java" diff --git "a/sangryeol/Week_1/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.java" "b/sangryeol/Week_1/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.java" new file mode 100644 index 0000000..4cb26a4 --- /dev/null +++ "b/sangryeol/Week_1/\354\235\264\352\261\264 \352\274\255 \355\222\200\354\226\264\354\225\274 \355\225\264!.java" @@ -0,0 +1,32 @@ +import java.io.*; +import java.util.*; + +public class BOJ_17390 { + static int N, Q; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + Q = Integer.parseInt(st.nextToken()); + ArrayList arrayList = new ArrayList<>(); + int[] pSum = new int[N + 1]; + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + int in = Integer.parseInt(st.nextToken()); + arrayList.add(in); + } + Collections.sort(arrayList); + for (int i = 1; i <= N; i++) { + pSum[i] = pSum[i-1] + arrayList.get(i-1); + } + for (int i = 1, l, r; i <= Q; i++) { + st = new StringTokenizer(br.readLine()); + l = Integer.parseInt(st.nextToken()); + r = Integer.parseInt(st.nextToken()); + bw.write(pSum[r] - pSum[l - 1] + "\n"); + } + bw.flush(); + bw.close(); + } +} From 5e358ff7c3488f8adf245494de2c25eac7e21430 Mon Sep 17 00:00:00 2001 From: Heize Date: Thu, 24 Mar 2022 17:27:54 +0900 Subject: [PATCH 004/111] =?UTF-8?q?[Baekjoon]=202644:=20=EC=B4=8C=EC=88=98?= =?UTF-8?q?=EA=B3=84=EC=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\210\230\352\263\204\354\202\260.java" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "sangryeol/Week_1/\354\264\214\354\210\230\352\263\204\354\202\260.java" diff --git "a/sangryeol/Week_1/\354\264\214\354\210\230\352\263\204\354\202\260.java" "b/sangryeol/Week_1/\354\264\214\354\210\230\352\263\204\354\202\260.java" new file mode 100644 index 0000000..5c1eaa2 --- /dev/null +++ "b/sangryeol/Week_1/\354\264\214\354\210\230\352\263\204\354\202\260.java" @@ -0,0 +1,48 @@ +import java.util.*; +import java.io.*; + +public class BOJ_2644 { + public static int N, M; + public static int start, end; + public static ArrayList> adj = new ArrayList<>(); + public static int bfs() { + Queue queue = new LinkedList<>(); + boolean[] visited = new boolean[N]; + queue.add(start); + visited[start] = true; + for (int d = 0; !queue.isEmpty(); d++) { + int qSize = queue.size(); + for (int s = 0; s < qSize; s++) { + int curr = queue.poll(); + if (curr == end) return d; + for (int next : adj.get(curr)) { + if (visited[next]) continue; + queue.add(next); + visited[next] = true; + } + } + } + return -1; + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + for (int i = 0; i < N; i++) { + adj.add(new ArrayList()); + } + StringTokenizer st = new StringTokenizer(br.readLine()); + start = Integer.parseInt(st.nextToken()); + end = Integer.parseInt(st.nextToken()); + start--; end--; + M = Integer.parseInt(br.readLine()); + for (int i = 0, u, v; i < M; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + u--; v--; + adj.get(u).add(v); + adj.get(v).add(u); + } + System.out.println(bfs()); + } +} From 7dae33b54f8a7d3f44106cd04f43dd02142a8e79 Mon Sep 17 00:00:00 2001 From: heize <33370107+sonsy0929@users.noreply.github.com> Date: Wed, 30 Mar 2022 00:21:45 +0900 Subject: [PATCH 005/111] docs: Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 542ee06..7cdcff5 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,10 @@

- + + + + From b68c2f77931bbc3213bf28f98b53650c423eaa21 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 3 Apr 2022 11:52:02 +0900 Subject: [PATCH 006/111] =?UTF-8?q?[Baekjoon]=201717:=20=EC=A7=91=ED=95=A9?= =?UTF-8?q?=EC=9D=98=20=ED=91=9C=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_2/Boj1717.java | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 daejik/Week_2/Boj1717.java diff --git a/daejik/Week_2/Boj1717.java b/daejik/Week_2/Boj1717.java new file mode 100644 index 0000000..c5b1d6f --- /dev/null +++ b/daejik/Week_2/Boj1717.java @@ -0,0 +1,68 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.StringTokenizer; + + +public class Boj1717 { + + static public int parent[]; + + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + parent = new int[N + 1]; + init(N); + + int type, a, b; + + for(int i=0; i Date: Sun, 3 Apr 2022 11:52:22 +0900 Subject: [PATCH 007/111] =?UTF-8?q?[Baekjoon]=206198:=20=EC=98=A5=EC=83=81?= =?UTF-8?q?=20=EC=A0=95=EC=9B=90=20=EA=BE=B8=EB=AF=B8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_2/Boj6198.java | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 daejik/Week_2/Boj6198.java diff --git a/daejik/Week_2/Boj6198.java b/daejik/Week_2/Boj6198.java new file mode 100644 index 0000000..fe9ed23 --- /dev/null +++ b/daejik/Week_2/Boj6198.java @@ -0,0 +1,40 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.IOException; +import java.util.Stack; + +public class Boj6198 { + + public static Stack stack = new Stack<>(); + public static long ans; + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + int N = Integer.parseInt(br.readLine()); + + for(int i=0; i Date: Sun, 3 Apr 2022 11:52:40 +0900 Subject: [PATCH 008/111] =?UTF-8?q?[Baekjoon]=209081:=EB=8B=A8=EC=96=B4=20?= =?UTF-8?q?=EB=A7=9E=EC=B6=94=EA=B8=B0=5F=EC=8B=A4=ED=8C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Boj9081_\354\213\244\355\214\250.java" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "daejik/Week_2/Boj9081_\354\213\244\355\214\250.java" diff --git "a/daejik/Week_2/Boj9081_\354\213\244\355\214\250.java" "b/daejik/Week_2/Boj9081_\354\213\244\355\214\250.java" new file mode 100644 index 0000000..adf96cc --- /dev/null +++ "b/daejik/Week_2/Boj9081_\354\213\244\355\214\250.java" @@ -0,0 +1,58 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +public class Boj9081 { + + public static String arr; + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + + int T = Integer.parseInt(br.readLine()); + + for(int t=0; t 0 && (arr.charAt(i - 1) >= arr.charAt(i))) i--; + //() ° ܾ + if(i <= 0) { + bw.write(arr + "\n"); + continue; + } + + //(2) [i-1] < j j ã + while(arr.charAt(i - 1) > arr.charAt(j)) j--; + + //(3) [i-1] , [j] swap + arr = swap(arr, i - 1, j); + + //(4) i ~ N-1 swap + j = arr.length() - 1; + while(i < j) { + arr = swap(arr, i, j); + i++; + j--; + } + + bw.write(arr + "\n"); + } + bw.close(); + } + public static String swap(String data, int i, int j) { + char[] ret = data.toCharArray(); + char temp = ret[i]; + ret[i] = ret[j]; + ret[j] = temp; + + return new String(ret); + + } +} From d2b32cce079f1f5e644cd0b2800ca91807fd9c41 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 3 Apr 2022 13:51:51 +0900 Subject: [PATCH 009/111] =?UTF-8?q?[Baekjoon]=203079:=20=EC=9E=85=EA=B5=AD?= =?UTF-8?q?=EC=8B=AC=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\265\255\354\213\254\354\202\254.java" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "sangryeol/Week_2/\354\236\205\352\265\255\354\213\254\354\202\254.java" diff --git "a/sangryeol/Week_2/\354\236\205\352\265\255\354\213\254\354\202\254.java" "b/sangryeol/Week_2/\354\236\205\352\265\255\354\213\254\354\202\254.java" new file mode 100644 index 0000000..7ae2653 --- /dev/null +++ "b/sangryeol/Week_2/\354\236\205\352\265\255\354\213\254\354\202\254.java" @@ -0,0 +1,33 @@ +import java.io.*; +import java.util.*; + +public class BOJ_3079 { + static long N, M; + static ArrayList times = new ArrayList<>(); + static boolean f(long param) { + long cnt = 0; + for (long time : times) { + cnt += param / time; + } + return cnt >= M; + } + 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()); + for (int i = 0; i < N; i++) { + long time = Integer.parseInt(br.readLine()); + times.add(time); + } + Collections.sort(times); + int last = times.size() - 1; + long lo = 0, hi = times.get(last) * M; + while (lo + 1 < hi) { + long mid = (lo + hi) / 2; + if (f(mid)) hi = mid; + else lo = mid; + } + System.out.println(hi); + } +} From 700474b4d812ceb62b97a38a02237094500b43d3 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 3 Apr 2022 13:51:56 +0900 Subject: [PATCH 010/111] =?UTF-8?q?[Baekjoon]=209081:=20=EB=8B=A8=EC=96=B4?= =?UTF-8?q?=20=EB=A7=9E=EC=B6=94=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\247\236\354\266\224\352\270\260.java" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "sangryeol/Week_2/\353\213\250\354\226\264 \353\247\236\354\266\224\352\270\260.java" diff --git "a/sangryeol/Week_2/\353\213\250\354\226\264 \353\247\236\354\266\224\352\270\260.java" "b/sangryeol/Week_2/\353\213\250\354\226\264 \353\247\236\354\266\224\352\270\260.java" new file mode 100644 index 0000000..edb360d --- /dev/null +++ "b/sangryeol/Week_2/\353\213\250\354\226\264 \353\247\236\354\266\224\352\270\260.java" @@ -0,0 +1,29 @@ +import java.util.*; +import java.io.*; + +public class BOJ_9081 { + public static String getNextWord(String word) { + int u = -1, v = 0; + char[] tWord = word.toCharArray(); + int idx = word.length() - 1; + while (idx > 0 && tWord[idx - 1] >= tWord[idx]) idx--; + u = idx - 1; + if (u == -1) return word; + idx = word.length() - 1; + while (idx >= u + 1 && tWord[u] >= tWord[idx]) idx--; + v = idx; + char t = tWord[u]; + tWord[u] = tWord[v]; + tWord[v] = t; + Arrays.sort(tWord, u + 1, word.length()); + return new String(tWord); + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + for (int i = 0; i < T; i++) { + String word = br.readLine(); + System.out.println(getNextWord(word)); + } + } +} From b47dbd391a862aa94d6497166ffeb17a87bc7369 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 3 Apr 2022 13:52:02 +0900 Subject: [PATCH 011/111] =?UTF-8?q?[Baekjoon]=206198:=20=EC=98=A5=EC=83=81?= =?UTF-8?q?=20=EC=A0=95=EC=9B=90=20=EA=BE=B8=EB=AF=B8=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\276\270\353\257\270\352\270\260.java" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "sangryeol/Week_2/\354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.java" diff --git "a/sangryeol/Week_2/\354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.java" "b/sangryeol/Week_2/\354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.java" new file mode 100644 index 0000000..b565880 --- /dev/null +++ "b/sangryeol/Week_2/\354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.java" @@ -0,0 +1,23 @@ +import java.io.*; +import java.util.*; + +public class BOJ_6198 { + public static int N; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + int[] heights = new int[N]; + for (int i = 0; i < N; i++) { + heights[i] = Integer.parseInt(br.readLine()); + } + Stack stk = new Stack<>(); + long answer = 0; + for (int i = 0; i < N; i++) { + int height = heights[i]; + while (!stk.isEmpty() && stk.peek() <= height) stk.pop(); + stk.push(height); + answer += 0L + stk.size() - 1; + } + System.out.println(answer); + } +} From d384d0223426fa9ef02cefef8aaeec2b15c42754 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 3 Apr 2022 20:30:00 +0900 Subject: [PATCH 012/111] =?UTF-8?q?[Baekjoon]=209081:=20=EB=8B=A8=EC=96=B4?= =?UTF-8?q?=20=EB=A7=9E=EC=B6=94=EA=B8=B0=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Boj9081_\354\204\261\352\263\265.java" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "daejik/Week_2/Boj9081_\354\204\261\352\263\265.java" diff --git "a/daejik/Week_2/Boj9081_\354\204\261\352\263\265.java" "b/daejik/Week_2/Boj9081_\354\204\261\352\263\265.java" new file mode 100644 index 0000000..79f876e --- /dev/null +++ "b/daejik/Week_2/Boj9081_\354\204\261\352\263\265.java" @@ -0,0 +1,60 @@ +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.Arrays; + +public class Boj9081 { + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int T = Integer.parseInt(br.readLine()); + + for(int t=0; t 0 && (arr[i - 1] >= arr[i])) i--; + + //(2) i <= 0 ̸ . + if(i <= 0) { + System.out.println(arr); + continue; + } + + //(3) arr[i-1] ¹ٲ arr[i-1] ū arr[j] ã + int j = arr.length - 1; + while(arr[j] <= arr[i-1]) j--; + + swap(arr, i-1, j); + + //(4)i ϱ + /* + j = arr.length - 1; + while(j > i) { + swap(arr, i, j); + i++; + j--; + } + */ + + + Arrays.sort(arr, i, arr.length); + + System.out.println(arr); + + //bw.write((arr+"\n").toCharArray()); + } + //bw.close(); + } + public static void swap(char[] arr, int i, int j) { + char tmp = arr[i]; + arr[i] = arr[j]; + arr[j] = tmp; + } +} From a5d2a9a04a593332757db722e0542dbcbe551762 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 3 Apr 2022 20:30:43 +0900 Subject: [PATCH 013/111] =?UTF-8?q?[Baekjoon]=203079:=20=EC=9E=85=EA=B5=AD?= =?UTF-8?q?=EC=8B=AC=EC=82=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_2/Boj3079.java | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 daejik/Week_2/Boj3079.java diff --git a/daejik/Week_2/Boj3079.java b/daejik/Week_2/Boj3079.java new file mode 100644 index 0000000..e463d35 --- /dev/null +++ b/daejik/Week_2/Boj3079.java @@ -0,0 +1,54 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Boj3079 { + + public static int N, M; + public static int[] arr; + public static long ans, maxTime = 0; + + 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()); + arr = new int[N]; + ans = Long.MAX_VALUE; + + for(int i=0; i Date: Sat, 16 Apr 2022 21:14:31 +0900 Subject: [PATCH 014/111] =?UTF-8?q?[Baekjoon]21939=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=EC=B6=94=EC=B2=A8=EC=8B=9C=EC=8A=A4=ED=85=9CVersion1(comparabl?= =?UTF-8?q?e/comparator)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_3/Boj21939_comparable.java | 88 ++++++++++++++++++++ daejik/Week_3/Boj21939_comparator.java | 109 +++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 daejik/Week_3/Boj21939_comparable.java create mode 100644 daejik/Week_3/Boj21939_comparator.java diff --git a/daejik/Week_3/Boj21939_comparable.java b/daejik/Week_3/Boj21939_comparable.java new file mode 100644 index 0000000..832bac6 --- /dev/null +++ b/daejik/Week_3/Boj21939_comparable.java @@ -0,0 +1,88 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeSet; + +public class Boj21939_comparable { + + public static int N, M; + + public static class Pair implements Comparable{ + int P, L; + + Pair(int p, int l){ + this.P = p; + this.L = l; + } + + //Comparable + @Override + public int compareTo(Pair p) { + + if(this.L == p.L) { + return this.P - p.P; + } + else { + return this.L - p.L; + } + } + } + + public static void main(String[] args) throws IOException{ + TreeSet asc = new TreeSet(); + Map map = new HashMap<>(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + + for(int i=0; i asc = new TreeSet(new AscendingComparator()); + public static Map map = new HashMap<>(); + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + + for(int i=0; i{ + + @Override + public int compare(Pair a, Pair b) { + if(a.L < b.L) return -1; + else if(a.L == b.L) { + // + // ȣ + if(a.P < b.P) return -1; + else if(a.P == b.P) return 0; + else return 1; + } + else return 1; + } +} +/* +class DescendingComparator implements Comparator{ + + @Override + public int compare(Pair a, Pair b) { + if(a.L < b.L) return 1; + else if(a.L == b.L) { + // + // ȣ + if(a.P < b.P) return 1; + else if(a.P == b.P) return 0; + else return -1; + } + else return -1; + } +} +*/ +class Pair{ + int P, L; + + Pair(int p, int l){ + this.P = p; + this.L = l; + } +} \ No newline at end of file From 297edbaf92d0928653de8954ed58c3def8899295 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sat, 16 Apr 2022 21:15:00 +0900 Subject: [PATCH 015/111] =?UTF-8?q?[Baekjoon]2302=20=EA=B7=B9=EC=9E=A5?= =?UTF-8?q?=EC=A2=8C=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_3/Boj2302.java | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 daejik/Week_3/Boj2302.java diff --git a/daejik/Week_3/Boj2302.java b/daejik/Week_3/Boj2302.java new file mode 100644 index 0000000..0d7029f --- /dev/null +++ b/daejik/Week_3/Boj2302.java @@ -0,0 +1,45 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Boj2302 { + + public static int N, M; + public static int[] fibo; + public static int befIdx, idx, num; + public static int ans; + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + M = Integer.parseInt(br.readLine()); + fibo = new int[N + 1]; + befIdx = 1; + ans = 1; + + + for(int i=0; i Date: Sat, 16 Apr 2022 21:15:32 +0900 Subject: [PATCH 016/111] =?UTF-8?q?[Baekjoon]20166=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=20=EC=A7=80=EC=98=A5=EC=97=90=20=EB=B9=A0=EC=A7=84=20?= =?UTF-8?q?=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_3/Boj20166.java | 81 +++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 daejik/Week_3/Boj20166.java diff --git a/daejik/Week_3/Boj20166.java b/daejik/Week_3/Boj20166.java new file mode 100644 index 0000000..9fce930 --- /dev/null +++ b/daejik/Week_3/Boj20166.java @@ -0,0 +1,81 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; + +public class Boj20166 { + public static int N, M, K, ans; + public static char[][] board; + public static int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1}; + public static int[] dc = {-1, 0, 1, -1, 1, -1, 0, 1}; + public static Map map = new HashMap<>(); + + 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()); + K = Integer.parseInt(st.nextToken()); + board = new char[N][M]; + + for(int i=0; i= N) arr[0] = 0; + if(arr[1] >= M) arr[1] = 0; + } +} \ No newline at end of file From 06a7e5d2bd9829a3f1f722c3a351d7b095a73fa0 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sat, 16 Apr 2022 21:15:55 +0900 Subject: [PATCH 017/111] =?UTF-8?q?[Baekjoon]15681=20=ED=8A=B8=EB=A6=AC?= =?UTF-8?q?=EC=99=80=20=EC=BF=BC=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_3/Boj15681.java | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 daejik/Week_3/Boj15681.java diff --git a/daejik/Week_3/Boj15681.java b/daejik/Week_3/Boj15681.java new file mode 100644 index 0000000..1640db4 --- /dev/null +++ b/daejik/Week_3/Boj15681.java @@ -0,0 +1,56 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class Boj15681 { + + public static int N, R, Q; + public static int[] size; + public static ArrayList[] arrayList; + + 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()); + R = Integer.parseInt(st.nextToken()); + Q = Integer.parseInt(st.nextToken()); + + size = new int[N + 1]; + arrayList = new ArrayList[N + 1]; + for(int i=1; i<=N; i++) { + arrayList[i] = new ArrayList(); + } + + for(int i=1; i Date: Sun, 17 Apr 2022 07:06:39 +0900 Subject: [PATCH 018/111] =?UTF-8?q?[Baekjoon]=2015681:=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=99=80=20=EC=BF=BC=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_3/BOJ_15681.java | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sangryeol/Week_3/BOJ_15681.java diff --git a/sangryeol/Week_3/BOJ_15681.java b/sangryeol/Week_3/BOJ_15681.java new file mode 100644 index 0000000..7e0c051 --- /dev/null +++ b/sangryeol/Week_3/BOJ_15681.java @@ -0,0 +1,41 @@ +import java.io.*; +import java.util.*; + +public class BOJ_15681 { + static int N, Q, R; + static final int SZ = 100_010; + static int[] dp = new int[SZ]; + static ArrayList[] adj = new ArrayList[SZ]; + public static int f(int curr, int parent) { + if (dp[curr] != -1) return dp[curr]; + dp[curr] = 1; + for (int next : adj[curr]) { + if (next == parent) continue; + dp[curr] += f(next, curr); + } + return dp[curr]; + } + 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()); + R = Integer.parseInt(st.nextToken()); + Q = Integer.parseInt(st.nextToken()); + for (int i = 1; i <= N; i++) { + adj[i] = new ArrayList<>(); + } + for (int i = 1, u, v; i <= N - 1; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + adj[u].add(v); + adj[v].add(u); + } + Arrays.fill(dp, -1); + f(R, 0); + for (int i = 1, q; i <= Q; i++) { + q = Integer.parseInt(br.readLine()); + System.out.println(dp[q]); + } + } +} From 24eb004595765c8606ec02a105830e7ab83eaa6a Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 17 Apr 2022 07:06:51 +0900 Subject: [PATCH 019/111] =?UTF-8?q?[Baekjoon]=2020166:=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EC=97=B4=20=EC=A7=80=EC=98=A5=EC=97=90=20=EB=B9=A0?= =?UTF-8?q?=EC=A7=84=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_3/BOJ_20166.java | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sangryeol/Week_3/BOJ_20166.java diff --git a/sangryeol/Week_3/BOJ_20166.java b/sangryeol/Week_3/BOJ_20166.java new file mode 100644 index 0000000..a7f23ba --- /dev/null +++ b/sangryeol/Week_3/BOJ_20166.java @@ -0,0 +1,41 @@ +import java.io.*; +import java.util.*; + +public class BOJ_20166 { + static int N, M, K; + static char[][] maze; + static HashMap hashMap; + static final int[] roff = {-1, 1, 0, 0, 1, 1, -1, -1}; + static final int[] coff = {0, 0, 1, -1, 1, -1, 1, -1}; + 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()); + K = Integer.parseInt(st.nextToken()); + maze = new char[N][M]; + for (int i = 0; i < N; i++) { + maze[i] = br.readLine().toCharArray(); + } + hashMap = new HashMap<>(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + dfs(i, j, "" + maze[i][j]); + } + } + for (int i = 0; i < K; i++) { + String destination = br.readLine(); + System.out.println(hashMap.getOrDefault(destination, 0)); + } + } + public static void dfs(int r, int c, String s) { + if (s.length() > 5) return; + int count = hashMap.getOrDefault(s, 0); + hashMap.put(s, count + 1); + for (int d = 0; d < 8; d++) { + int nr = (N + r + roff[d]) % N; + int nc = (M + c + coff[d]) % M; + dfs(nr, nc, s + maze[nr][nc]); + } + } +} From cf6d3a47f15d267b2e437aecfcf2c5dccc0ec97a Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 17 Apr 2022 07:07:04 +0900 Subject: [PATCH 020/111] =?UTF-8?q?[Baekjoon]=2021939:=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=B6=94=EC=B2=9C=20=EC=8B=9C=EC=8A=A4=ED=85=9C=20?= =?UTF-8?q?Version=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_3/BOJ_21939.java | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sangryeol/Week_3/BOJ_21939.java diff --git a/sangryeol/Week_3/BOJ_21939.java b/sangryeol/Week_3/BOJ_21939.java new file mode 100644 index 0000000..0c55858 --- /dev/null +++ b/sangryeol/Week_3/BOJ_21939.java @@ -0,0 +1,59 @@ +import java.io.*; +import java.util.*; +class Problem implements Comparable { + int p, l; + Problem(int p, int l) { + this.p = p; + this.l = l; + } + @Override + public int compareTo(Problem o) { + if (this.l != o.l) return this.l - o.l; + return this.p - o.p; + } +} +public class BOJ_21939 { + public static void main(String[] args) throws IOException { + int N, M, P, L; + int[] solved = new int[100010]; + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + PriorityQueue hardPQ = new PriorityQueue<>(); + PriorityQueue easyPQ = new PriorityQueue<>(); + for (int i = 1; i <= N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + P = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + hardPQ.add(new Problem(-P, -L)); + easyPQ.add(new Problem(P, L)); + solved[P] = L; + } + M = Integer.parseInt(br.readLine()); + for (int i = 1; i <= M; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + String command = st.nextToken(); + if (command.equals("add")) { + P = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + hardPQ.add(new Problem(-P, -L)); + easyPQ.add(new Problem(P, L)); + solved[P] = L; + } + else if (command.equals("recommend")) { + int type = Integer.parseInt(st.nextToken()); + if (type == 1) { + while (!hardPQ.isEmpty() && solved[-hardPQ.peek().p] != -hardPQ.peek().l) hardPQ.poll(); + System.out.println(-hardPQ.peek().p); + } + else { + while (!easyPQ.isEmpty() && solved[easyPQ.peek().p] != easyPQ.peek().l) easyPQ.poll(); + System.out.println(easyPQ.peek().p); + } + } + else if (command.equals("solved")) { + P = Integer.parseInt(st.nextToken()); + solved[P] = 0; + } + } + } +} \ No newline at end of file From 7865191843da5c9edaa531a613d64ca5bff1de23 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 17 Apr 2022 07:07:13 +0900 Subject: [PATCH 021/111] =?UTF-8?q?[Baekjoon]=202302:=20=EA=B7=B9=EC=9E=A5?= =?UTF-8?q?=20=EC=A2=8C=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_3/BOJ_2302.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sangryeol/Week_3/BOJ_2302.java diff --git a/sangryeol/Week_3/BOJ_2302.java b/sangryeol/Week_3/BOJ_2302.java new file mode 100644 index 0000000..3c3bdba --- /dev/null +++ b/sangryeol/Week_3/BOJ_2302.java @@ -0,0 +1,24 @@ +import java.io.*; +import java.util.*; + +public class BOJ_2302 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + int M = Integer.parseInt(br.readLine()); + int[] dp = new int[N + 1]; + dp[0] = dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= N; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + int answer = 1, s = 1; + for (int i = 1; i <= M; i++) { + int e = Integer.parseInt(br.readLine()); + answer *= dp[e - s]; + s = e + 1; + } + answer *= dp[N + 1 - s]; + System.out.println(answer); + } +} \ No newline at end of file From cb5f0225331681a5786a4f405133bd3b6de8e937 Mon Sep 17 00:00:00 2001 From: leedjik Date: Wed, 20 Apr 2022 17:06:52 +0900 Subject: [PATCH 022/111] =?UTF-8?q?[Baekjoon]=2020208:=20=EC=A7=84?= =?UTF-8?q?=EC=9A=B0=EC=9D=98=20=EB=AF=BC=ED=8A=B8=EC=B4=88=EC=BD=94?= =?UTF-8?q?=EC=9A=B0=EC=9C=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_4/Boj20208.java | 84 +++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 daejik/Week_4/Boj20208.java diff --git a/daejik/Week_4/Boj20208.java b/daejik/Week_4/Boj20208.java new file mode 100644 index 0000000..17ae8c1 --- /dev/null +++ b/daejik/Week_4/Boj20208.java @@ -0,0 +1,84 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class Boj20208 { + + public static int map[][]; + public static boolean visited[]; + public static int dr[]= {-1, 1, 0, 0}; + public static int dc[]= {0, 0, -1, 1}; + public static int ans, N, M, H, milkCnt; + public static int homeR, homeC; + public static ArrayList arr = new ArrayList<>(); + + 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()); + H = Integer.parseInt(st.nextToken()); + + map = new int[N][N]; + + // map Է ޱ + for(int i=0; i= distToHome) { + dfs(new Pair(homeR, homeC), eatMilk, hp - distToHome, true); + } + + // + for(int i=0; i= distToMilk) { + visited[i] = true; + dfs(arr.get(i), eatMilk + 1, hp - distToMilk + H, false); + visited[i] = false; + } + } + } + public static int distFromTo(int nowR, int nowC, int milkR, int milkC) { + //ź Ÿ + return Math.abs(nowR - milkR) + Math.abs(nowC - milkC); + } +} +class Pair{ + int r, c; + + Pair(int r, int c){ + this.r = r; + this.c = c; + } +} From 481c192f0e187da3a77ddeabad1509e013a8bd6d Mon Sep 17 00:00:00 2001 From: leedjik Date: Sat, 30 Apr 2022 22:29:57 +0900 Subject: [PATCH 023/111] =?UTF-8?q?[Baekjoon]19591:=20=EB=8F=85=ED=8A=B9?= =?UTF-8?q?=ED=95=9C=20=EA=B3=84=EC=82=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_4/Boj19591.java | 134 ++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 daejik/Week_4/Boj19591.java diff --git a/daejik/Week_4/Boj19591.java b/daejik/Week_4/Boj19591.java new file mode 100644 index 0000000..34592bd --- /dev/null +++ b/daejik/Week_4/Boj19591.java @@ -0,0 +1,134 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; + + +public class Boj19591 { + + static Deque dequeOp = new ArrayDeque<>(); + static Deque dequeNum = new ArrayDeque<>(); + static HashMap hashMap = new HashMap<>(); + static boolean flag = false; + + public static void main(String[] args) throws IOException{ + + // 켱 ֱ + hashMap.put("+", 1); + hashMap.put("-", 1); + hashMap.put("*", 2); + hashMap.put("/", 2); + + // Է ޱ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String input = br.readLine(); + + // , + makeDeque(input); + + // + while(dequeOp.size() > 1) { + + // + long frontFirstNum = dequeNum.pollFirst(); + long frontSecondNum = dequeNum.pollFirst(); + String frontOp = dequeOp.pollFirst(); + + // + long lastSecondNum = dequeNum.pollLast(); + long lastFirstNum = 0; + if(dequeNum.isEmpty()) flag = true; // 3 + 2 - 1 , 2 + if(flag == true) lastFirstNum = frontSecondNum; + else lastFirstNum = dequeNum.pollLast(); + String lastOp = dequeOp.pollLast(); + + // 켱 + int priorityNumfrontOp = hashMap.get(frontOp); + int priorityNumLastOp = hashMap.get(lastOp); + + if(priorityNumfrontOp > priorityNumLastOp) { + // 켱 + long calValue = calculate(frontFirstNum, frontSecondNum, frontOp); + dequeNum.addFirst(calValue); + + // ڴ ״ ٽ ֱ + dequeOp.addLast(lastOp); + if(flag == false) dequeNum.addLast(lastFirstNum); + dequeNum.addLast(lastSecondNum); + } + else if(priorityNumfrontOp < priorityNumLastOp) { + // ڰ 켱 + long calValue = calculate(lastFirstNum, lastSecondNum, lastOp); + dequeNum.addLast(calValue); + + // ״ ٽ ֱ + dequeOp.addFirst(frontOp); + if(flag == false) dequeNum.addFirst(frontSecondNum); + dequeNum.addFirst(frontFirstNum); + } + else { + // 켱 + + // غ + long frontRet = calculate(frontFirstNum, frontSecondNum, frontOp); + // غ + long lastRet = calculate(lastFirstNum, lastSecondNum, lastOp); + + if(frontRet < lastRet) { + // ڰ ŭ + dequeNum.addLast(lastRet); + + // ״ ٽ ֱ + dequeOp.addFirst(frontOp); + if(flag == false) dequeNum.addFirst(frontSecondNum); + dequeNum.addFirst(frontFirstNum); + } + else { + // ũų, յ + dequeNum.addFirst(frontRet); + + // ڴ ״ ٽ ֱ + dequeOp.addLast(lastOp); + if(flag == false) dequeNum.addLast(lastFirstNum); + dequeNum.addLast(lastSecondNum); + } + } + } + // 1 ׳ ϱ + long firstNum = dequeNum.pollFirst(); + if(dequeNum.isEmpty()) { + // Է 1 + System.out.println(firstNum); + return; + } + long secondNum = dequeNum.pollFirst(); + String op = dequeOp.pollFirst(); + + System.out.println(calculate(firstNum, secondNum, op)); + } + public static long calculate(long num1, long num2, String op) { + if(op.equals("+".toString())) return num1 + num2; + else if(op.equals("-".toString())) return num1 - num2; + else if(op.equals("/".toString())) return num1 / num2; + else return num1 * num2; + } + public static void makeDeque(String input) { + int befIdx = 0; + + for(int i=0; i Date: Sat, 30 Apr 2022 22:33:09 +0900 Subject: [PATCH 024/111] [Baekjoon]12018: Yonsei TOTO --- daejik/Week_4/Boj12018.java | 101 ++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 daejik/Week_4/Boj12018.java diff --git a/daejik/Week_4/Boj12018.java b/daejik/Week_4/Boj12018.java new file mode 100644 index 0000000..40bd2a3 --- /dev/null +++ b/daejik/Week_4/Boj12018.java @@ -0,0 +1,101 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.StringTokenizer; + +public class Boj12018 { + + static ArrayList inputArray = new ArrayList<>(); + static ArrayList ascendingSortedList = new ArrayList<>(); + static int N, M, P, L, ans; + + 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()); + + // (1) 񸶴 -> L° ٸ List ֱ + while(N-- > 0) { + st = new StringTokenizer(br.readLine()); + P = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + + inputArray.clear(); + st = new StringTokenizer(br.readLine()); + + // ٸ л ϸ Է + while(P-- > 0) { + int tmp = Integer.parseInt(st.nextToken()); + inputArray.add(tmp); + } + + // inputArray + Collections.sort(inputArray, Collections.reverseOrder()); + + // L° ascendingSortList ֱ + if(inputArray.size() >= L) { + ascendingSortedList.add(inputArray.get(L - 1)); + } + else { + // ش ûο L ۾Ƽ, ϸ 1 ־ ȴ. + ascendingSortedList.add(1); + } + } + + + // (2) list + Collections.sort(ascendingSortedList); + + // (3) Mŭ ư鼭 ++ + for(int num : ascendingSortedList) { + M -= num; + if(M < 0) break; + + ans++; + } + System.out.println(ans); + } + + /* + * ascendingSortedList ϼŰ Լ + */ + + public static void inputFunc() throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + while(N-- > 0) { + + System.out.println("N:"+N); + st = new StringTokenizer(br.readLine()); + P = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + + System.out.println("P:"+P+", L:"+L); + + inputArray.clear(); + st = new StringTokenizer(br.readLine()); + + // ٸ л ϸ Է + while(P-- > 0) { + int tmp = Integer.parseInt(st.nextToken()); + inputArray.add(tmp); + } + + // inputArray + Collections.sort(inputArray, Collections.reverseOrder()); + + // L° ascendingSortList ֱ + if(inputArray.size() >= L) { + ascendingSortedList.add(inputArray.get(L - 1)); + } + else { + // ش ûο L ۾Ƽ, ϸ 1 ־ ȴ. + ascendingSortedList.add(1); + } + } + } +} From 5735e62fb67c7b15c8ab6b525508639e2a2c6b06 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sat, 30 Apr 2022 22:34:26 +0900 Subject: [PATCH 025/111] =?UTF-8?q?[Baekjoon]6118:=20=EC=88=A8=EB=B0=94?= =?UTF-8?q?=EA=BC=AD=EC=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_4/Boj6118.java | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 daejik/Week_4/Boj6118.java diff --git a/daejik/Week_4/Boj6118.java b/daejik/Week_4/Boj6118.java new file mode 100644 index 0000000..8f14a5b --- /dev/null +++ b/daejik/Week_4/Boj6118.java @@ -0,0 +1,82 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Boj6118 { + + static ArrayList> graph = new ArrayList<>(); + static Queue q = new LinkedList<>(); + static int[] dist; + static boolean[] visited; + static int N, M; + + 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()); + visited = new boolean[N + 1]; + dist = new int[N + 1]; + + for(int i=0; i<=N; i++) { + graph.add(new ArrayList()); + } + + for(int m=0; m maxDist) + maxDist = dist[next]; + } + } + + int numHide = 0; + int numSameDist = 0; + + for(int i=1; i<=N; i++) { + if(dist[i] == maxDist) { + if(numHide == 0) numHide = i; + + numSameDist++; + } + } + System.out.println(numHide + " " + maxDist + " " + numSameDist); + } + private static class Pair{ + int num, dist; + + Pair(int num, int dist){ + this.num = num; + this.dist = dist; + } + } +} From 47610a3e1c8d38e52272e4f858e78eee62bf02f9 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 1 May 2022 11:33:40 +0900 Subject: [PATCH 026/111] [Baekjoon] 12018: Yonsei TOTO --- sangryeol/Week_4/BOJ_12018.java | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sangryeol/Week_4/BOJ_12018.java diff --git a/sangryeol/Week_4/BOJ_12018.java b/sangryeol/Week_4/BOJ_12018.java new file mode 100644 index 0000000..2ae90e9 --- /dev/null +++ b/sangryeol/Week_4/BOJ_12018.java @@ -0,0 +1,35 @@ +import java.io.*; +import java.util.*; + +public class BOJ_12018 { + public static void main(String[] args) throws IOException { + int N, M; + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + PriorityQueue scores = new PriorityQueue<>(); + for (int i = 1, P, L; i <= N; i++) { + st = new StringTokenizer(br.readLine()); + P = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + ArrayList mileages = new ArrayList<>(); + st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= P; j++) { + int mileage = Integer.parseInt(st.nextToken()); + mileages.add(mileage); + } + Collections.sort(mileages, Collections.reverseOrder()); + if (mileages.size() >= L - 1) scores.add(mileages.get(L - 1)); + else scores.add(1); + } + int cnt = 0, sum = 0; + while (!scores.isEmpty()) { + int score = scores.poll(); + if (sum + score > M) break; + sum += score; + cnt++; + } + System.out.println(cnt); + } +} From 85dac9de4d15998d4345d2eda7eee5baaec095a9 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 1 May 2022 11:33:52 +0900 Subject: [PATCH 027/111] =?UTF-8?q?[Baekjoon]=2020208:=20=EC=A7=84?= =?UTF-8?q?=EC=9A=B0=EC=9D=98=20=EB=AF=BC=ED=8A=B8=EC=B4=88=EC=BD=94?= =?UTF-8?q?=EC=9A=B0=EC=9C=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_4/BOJ_20208.java | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sangryeol/Week_4/BOJ_20208.java diff --git a/sangryeol/Week_4/BOJ_20208.java b/sangryeol/Week_4/BOJ_20208.java new file mode 100644 index 0000000..aeca29b --- /dev/null +++ b/sangryeol/Week_4/BOJ_20208.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + +public class BOJ_20208 { + static int N, M, H, sr, sc, mintCount, ans; + static int[][] maze; + static int[] indexs; + static ArrayList mintChocos = new ArrayList<>(); + static boolean isPossible(int count) { + int row = sr, col = sc, destRow = 0, destCol = 0; + int health = M; + for (int i = 0; i < count; i++) { + destRow = mintChocos.get(indexs[i]) / N; + destCol = mintChocos.get(indexs[i]) % N; + int dist = Math.abs(row - destRow) + Math.abs(col - destCol); + if (dist > health) return false; + row = destRow; col = destCol; + health -= dist; health += H; + } + int returnDist = Math.abs(destRow - sr) + Math.abs(destCol - sc); + if (returnDist > health) return false; + return true; + } + static void permutation(int pos, int selected) { + if (pos > mintCount) return; + if (isPossible(pos)) ans = Math.max(ans, pos); + for (int i = 0; i < mintCount; i++) { + if ((selected & 1 << i) != 0) continue; + indexs[pos] = i; + permutation(pos + 1, selected | (1 << i)); + } + } + 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()); + H = Integer.parseInt(st.nextToken()); + mintCount = 0; + maze = new int[N][N]; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + maze[i][j] = Integer.parseInt(st.nextToken()); + if (maze[i][j] == 1) { + sr = i; sc = j; + } + if (maze[i][j] == 2) { + mintCount++; + mintChocos.add(i * N + j); + } + } + } + indexs = new int[mintCount]; + permutation(0, 0); + System.out.println(ans); + } +} From b6b3184b6b3bec0712a5555026c1ba2423f3ab3d Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 1 May 2022 11:34:00 +0900 Subject: [PATCH 028/111] =?UTF-8?q?[Baekjoon]=206118:=20=EC=88=A8=EB=B0=94?= =?UTF-8?q?=EA=BC=AD=EC=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_4/BOJ_6118.java | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 sangryeol/Week_4/BOJ_6118.java diff --git a/sangryeol/Week_4/BOJ_6118.java b/sangryeol/Week_4/BOJ_6118.java new file mode 100644 index 0000000..dd6a246 --- /dev/null +++ b/sangryeol/Week_4/BOJ_6118.java @@ -0,0 +1,63 @@ +import java.io.*; +import java.util.*; + +class Answer { + int number, dist, count; + Answer(int n, int d, int c) { + number = n; + dist = d; + count = c; + } +} + +public class BOJ_6118 { + static int N, M; + static ArrayList[] adj; + static Answer bfs(int start) { + Queue queue = new LinkedList<>(); + int[] dist = new int[N + 1]; + Arrays.fill(dist, -1); + queue.add(start); + dist[start] = 0; + while (!queue.isEmpty()) { + int curr = queue.poll(); + for (int next : adj[curr]) { + if (dist[next] != -1) continue; + dist[next] = dist[curr] + 1; + queue.add(next); + } + } + Answer answer = new Answer(0, 0, 0); + for (int i = 1; i <= N; i++) { + int d = dist[i]; + if (d > answer.dist) { + answer.dist = d; + answer.count = 1; + answer.number = i; + } + else if (d == answer.dist) { + answer.count++; + } + } + return answer; + } + 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()); + adj = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) { + adj[i] = new ArrayList<>(); + } + for (int i = 1, u, v; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + adj[u].add(v); + adj[v].add(u); + } + Answer answer = bfs(1); + System.out.println(answer.number + " " + answer.dist + " " + answer.count); + } +} \ No newline at end of file From da24324c0f842e90430c402bbf9889a811958027 Mon Sep 17 00:00:00 2001 From: Heize Date: Sat, 14 May 2022 17:43:56 +0900 Subject: [PATCH 029/111] =?UTF-8?q?[Baekjoon]=201283:=20=EB=8B=A8=EC=B6=95?= =?UTF-8?q?=ED=82=A4=20=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\202\244 \354\247\200\354\240\225.java" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "sangryeol/Week_5/\353\213\250\354\266\225\355\202\244 \354\247\200\354\240\225.java" diff --git "a/sangryeol/Week_5/\353\213\250\354\266\225\355\202\244 \354\247\200\354\240\225.java" "b/sangryeol/Week_5/\353\213\250\354\266\225\355\202\244 \354\247\200\354\240\225.java" new file mode 100644 index 0000000..c474cda --- /dev/null +++ "b/sangryeol/Week_5/\353\213\250\354\266\225\355\202\244 \354\247\200\354\240\225.java" @@ -0,0 +1,89 @@ +import java.io.*; +import java.util.*; + +class Options { + ArrayList options; + ArrayList outputs; + boolean[] isUsedAlphabet; + Options() { + options = new ArrayList<>(); + outputs = new ArrayList<>(); + isUsedAlphabet = new boolean[60]; + } + void addOption(String option) { + options.add(option); + } + char getFirstAlphabet(String option) { + return option.charAt(0); + } + boolean isSmall(char c) { + return c >= 'a' && c <= 'z'; + } + int getAlphabetIndex(char c) { + if (isSmall(c)) return c - 'a'; + return c - 'A'; + } + boolean processFirstNotUsed(String[] splitOption) { + boolean flag = false; + StringBuilder tOption = new StringBuilder(); + for (String option : splitOption) { + char first = getFirstAlphabet(option); + int idx = getAlphabetIndex(first); + if (!flag && !isUsedAlphabet[idx]) { + isUsedAlphabet[idx] = true; + String suffix = option.substring(1); + tOption.append("[" + first + "]" + suffix + " "); + flag = true; + } + else tOption.append(option + " "); + } + if (flag) outputs.add(tOption.toString().trim()); + return flag; + } + void processNotUsed(String originOption) { + boolean flag = false; + StringBuilder tOption = new StringBuilder(); + for (char c : originOption.toCharArray()) { + if (c == ' ') { + tOption.append(c); + continue; + } + int idx = getAlphabetIndex(c); + if (!flag && !isUsedAlphabet[idx]) { + isUsedAlphabet[idx] = true; + tOption.append("[" + c + "]"); + flag = true; + } + else tOption.append(c); + } + outputs.add(tOption.toString()); + } + void process() { + for (String option : options) { + String[] splitOption = option.split(" "); + boolean ret = processFirstNotUsed(splitOption); + if (ret) continue; + processNotUsed(option); + } + } + void print() { + for (String output : outputs) { + System.out.println(output); + } + } +} + +public class BOJ_1283 { + static int N; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + Options options = new Options(); + for (int i = 1; i <= N; i++) { + String option = br.readLine(); + options.addOption(option); + } + options.process(); + options.print(); + } +} \ No newline at end of file From 355bfe3f9213b435cdf8ba837f820b9e2ac40f38 Mon Sep 17 00:00:00 2001 From: Heize Date: Sat, 14 May 2022 17:44:17 +0900 Subject: [PATCH 030/111] [Baekjoon] 11954: NIZOVI --- sangryeol/Week_5/NIZOVI.java | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sangryeol/Week_5/NIZOVI.java diff --git a/sangryeol/Week_5/NIZOVI.java b/sangryeol/Week_5/NIZOVI.java new file mode 100644 index 0000000..e02b107 --- /dev/null +++ b/sangryeol/Week_5/NIZOVI.java @@ -0,0 +1,44 @@ +import java.io.*; +import java.util.*; + +public class BOJ_11954 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String input = br.readLine(); + LinkedList outputs = new LinkedList<>(); + int brace = 0; + StringBuilder word = new StringBuilder(); + for (char c : input.toCharArray()) { + String output; + if (c == '{') { + output = " ".repeat(brace++ * 2) + "{"; + outputs.add(output); + } + else if (c == '}') { + if (word.length() > 0) { + output = " ".repeat(brace * 2) + word; + word = new StringBuilder(); + outputs.add(output); + } + output = " ".repeat(--brace * 2) + "}"; + outputs.add(output); + } + else if (c == ',') { + if (word.length() == 0) { + output = outputs.pollLast(); + output += ","; + outputs.addLast(output); + } + else { + output = " ".repeat(brace * 2) + word + ","; + word = new StringBuilder(); + outputs.addLast(output); + } + } + else word.append(c); + } + for (String output : outputs) { + System.out.println(output); + } + } +} From d18c9f508e056addff3387a60aebf9b3bd8e71a0 Mon Sep 17 00:00:00 2001 From: Heize Date: Sat, 14 May 2022 17:44:24 +0900 Subject: [PATCH 031/111] =?UTF-8?q?[Baekjoon]=2024725:=20=EC=97=A0?= =?UTF-8?q?=EB=B9=84=ED=8B=B0=EC=95=84=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\213\260\354\225\204\354\235\264.java" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "sangryeol/Week_5/\354\227\240\353\271\204\355\213\260\354\225\204\354\235\264.java" diff --git "a/sangryeol/Week_5/\354\227\240\353\271\204\355\213\260\354\225\204\354\235\264.java" "b/sangryeol/Week_5/\354\227\240\353\271\204\355\213\260\354\225\204\354\235\264.java" new file mode 100644 index 0000000..10a2bfd --- /dev/null +++ "b/sangryeol/Week_5/\354\227\240\353\271\204\355\213\260\354\225\204\354\235\264.java" @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + +public class BOJ_24725 { + static int N, M; + static char[][] MBTI = { + {'E', 'I'}, + {'N', 'S'}, + {'F', 'T'}, + {'P', 'J'} + }; + static char[][] alphabets; + static int[] roff = {1, -1, 0, 0, 1, 1, -1, -1}; + static int[] coff = {0, 0, -1, 1, 1, -1, 1, -1}; + static boolean isIn(int r, int c) { + return r >= 0 && c >= 0 && r < N && c < M; + } + static int dfs(int depth, int r, int c, int d, String word) { + if (depth == 4) { + boolean mbtiFlag = true; + for (int i = 0; i < 4; i++) { + char alphabet = word.charAt(i); + boolean findFlag = false; + for (int j = 0; j < 2; j++) { + findFlag |= (alphabet == MBTI[i][j]); + } + mbtiFlag &= findFlag; + if (!mbtiFlag) return 0; + } + return 1; + } + int nr = r + roff[d], nc = c + coff[d]; + if (!isIn(r, c)) return 0; + return dfs(depth + 1, nr, nc, d, word + alphabets[r][c]); + } + 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()); + alphabets = new char[N + 1][M + 1]; + for (int i = 0; i < N; i++) { + char[] rows = br.readLine().toCharArray(); + for (int j = 0; j < M; j++) { + alphabets[i][j] = rows[j]; + } + } + int ans = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + for (int d = 0; d < 8; d++) { + ans += dfs(0, i, j, d, ""); + } + } + } + System.out.println(ans); + } +} From 2c42afa87567f8e506484c554fd8310ef233badf Mon Sep 17 00:00:00 2001 From: Heize Date: Sat, 14 May 2022 17:44:30 +0900 Subject: [PATCH 032/111] =?UTF-8?q?[Baekjoon]=2016562:=20=EC=B9=9C?= =?UTF-8?q?=EA=B5=AC=EB=B9=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\271\234\352\265\254\353\271\204.java" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "sangryeol/Week_5/\354\271\234\352\265\254\353\271\204.java" diff --git "a/sangryeol/Week_5/\354\271\234\352\265\254\353\271\204.java" "b/sangryeol/Week_5/\354\271\234\352\265\254\353\271\204.java" new file mode 100644 index 0000000..d8d5b8b --- /dev/null +++ "b/sangryeol/Week_5/\354\271\234\352\265\254\353\271\204.java" @@ -0,0 +1,46 @@ +import java.io.*; +import java.util.*; + +public class BOJ_16562 { + static int N, M, K; + static int[] costs, parents; + public static int find(int n) { + if (parents[n] < 0) return n; + return parents[n] = find(parents[n]); + } + public static void union(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return; + if (costs[u] > costs[v]) parents[u] = v; + else parents[v] = u; + } + 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()); + K = Integer.parseInt(st.nextToken()); + costs = new int[N + 1]; + parents = new int[N + 1]; + st = new StringTokenizer(br.readLine()); + Arrays.fill(parents, -1); + for (int i = 1; i <= N; i++) { + costs[i] = Integer.parseInt(st.nextToken()); + } + for (int i = 1, u, v; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + union(u, v); + } + int ans = 0; + for (int i = 1; i <= N; i++) { + if (parents[i] != -1) continue; + ans += costs[i]; + K -= costs[i]; + } + if (K >= 0) System.out.println(ans); + else System.out.println("Oh no"); + } +} From ccba3e18b93aa0bd444d72fc2d11237ead6545f8 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 15 May 2022 14:54:27 +0900 Subject: [PATCH 033/111] =?UTF-8?q?[Baekjoon]1283:=20=EB=8B=A8=EC=B6=95?= =?UTF-8?q?=ED=82=A4=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_5/Boj1283.java | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 daejik/Week_5/Boj1283.java diff --git a/daejik/Week_5/Boj1283.java b/daejik/Week_5/Boj1283.java new file mode 100644 index 0000000..3a8ad6f --- /dev/null +++ b/daejik/Week_5/Boj1283.java @@ -0,0 +1,92 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.StringTokenizer; + + +public class Boj1283 { + static int N; + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + Set s = new HashSet<>(); + + for(int i=0; i arr = new ArrayList<>(); + while(st.hasMoreTokens()) { + arr.add(st.nextToken()); + } + + String ans = null; + + // 1 + for(String now : arr) { + String firstStr = String.valueOf(now.charAt(0)).toLowerCase(); + + if(s.contains(firstStr) == false) { + ans = makeAns(arr, now, 0); + s.add(firstStr); + break; + } + } + + if(ans != null) { + System.out.println(ans); + continue; + } + + // 2 + for(String now : arr) { + for(int j=1; j arr, String now, int idx) { + // now ڿ idx° Ű + String ans = null; + + for(String str : arr) { + if(str == now) { + // idx° [] ó Ѵ. + // (0 ~ idx - 1) + [idx] + (idx + 1 ~) + str = (str.substring(0, idx)).concat("[" + str.substring(idx, idx + 1) + "]").concat(str.substring(idx + 1)); + } + + if(ans == null) { + ans = str; + } + else { + ans = ans + " " + str; + } + } + + return ans; + } +} From 267f534f5b20b5a8f080588b0210179ffc0f431c Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 15 May 2022 14:54:55 +0900 Subject: [PATCH 034/111] [Baekjoon]24725: MBTI --- daejik/Week_5/Boj24725.java | 84 +++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 daejik/Week_5/Boj24725.java diff --git a/daejik/Week_5/Boj24725.java b/daejik/Week_5/Boj24725.java new file mode 100644 index 0000000..1ee0f1c --- /dev/null +++ b/daejik/Week_5/Boj24725.java @@ -0,0 +1,84 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Boj24725 { + static char[][] board; + static boolean[][] visited; + static int N, M, ans; + static int[] dr = {-1, -1, -1, 0, 0, 1, 1, 1}; + static int[] dc = {-1, 0, 1, -1, 1, -1, 0, 1}; + + 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()); + + board = new char[N][M]; + visited = new boolean[N][M]; + + for(int i=0; i=N || c>=M) return false; + return true; + } +} From 142f80859b6c99fe285989deb33ba9cadb1374f5 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 22 May 2022 13:14:31 +0900 Subject: [PATCH 035/111] [LeetCode] 1302: Deepest Leaves Sum --- sangryeol/Week_6/Deepest Leaves Sum.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sangryeol/Week_6/Deepest Leaves Sum.java diff --git a/sangryeol/Week_6/Deepest Leaves Sum.java b/sangryeol/Week_6/Deepest Leaves Sum.java new file mode 100644 index 0000000..b5d4c1f --- /dev/null +++ b/sangryeol/Week_6/Deepest Leaves Sum.java @@ -0,0 +1,18 @@ +class Solution { + int maxDepth, sum; + public void dfs(TreeNode curr, int depth) { + if (depth > maxDepth) { + maxDepth = depth; + sum = 0; + } + if (depth == maxDepth) { + sum += curr.val; + } + if (curr.left != null) dfs(curr.left, depth + 1); + if (curr.right != null) dfs(curr.right, depth + 1); + } + public int deepestLeavesSum(TreeNode root) { + dfs(root, 0); + return sum; + } +} \ No newline at end of file From 5a5476bb508fa8424c5fde20f84e60194a19debc Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 22 May 2022 13:14:41 +0900 Subject: [PATCH 036/111] =?UTF-8?q?[Baekjoon]=2021276:=20=EA=B3=84?= =?UTF-8?q?=EB=B3=B4=20=EB=B3=B5=EC=9B=90=EA=B0=80=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...352\260\200 \355\230\270\354\204\235.java" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "sangryeol/Week_6/\352\263\204\353\263\264 \353\263\265\354\233\220\352\260\200 \355\230\270\354\204\235.java" diff --git "a/sangryeol/Week_6/\352\263\204\353\263\264 \353\263\265\354\233\220\352\260\200 \355\230\270\354\204\235.java" "b/sangryeol/Week_6/\352\263\204\353\263\264 \353\263\265\354\233\220\352\260\200 \355\230\270\354\204\235.java" new file mode 100644 index 0000000..0155319 --- /dev/null +++ "b/sangryeol/Week_6/\352\263\204\353\263\264 \353\263\265\354\233\220\352\260\200 \355\230\270\354\204\235.java" @@ -0,0 +1,72 @@ +import java.io.*; +import java.util.*; + +public class BOJ_21276 { + static int N, M; + static TreeMap map = new TreeMap<>(); + static String[] names; + static ArrayList[] adj; + static ArrayList[] tree; + static int[] indegrees; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + indegrees = new int[N + 1]; + adj = new ArrayList[N + 1]; + tree = new ArrayList[N + 1]; + names = new String[N + 1]; + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + String name = st.nextToken(); + map.put(name, i); + names[i] = name; + adj[i] = new ArrayList<>(); + tree[i] = new ArrayList<>(); + } + M = Integer.parseInt(br.readLine()); + for (int i = 1; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + int child = map.get(st.nextToken()); + int parent = map.get(st.nextToken()); + adj[parent].add(child); + indegrees[child]++; + } + Queue queue = new LinkedList<>(); + ArrayList roots = new ArrayList<>(); + for (int i = 1; i <= N; i++) { + if (indegrees[i] == 0) { + queue.add(i); + roots.add(names[i]); + } + } + while (!queue.isEmpty()) { + int curr = queue.poll(); + for (int next : adj[curr]) { + if (--indegrees[next] == 0) { + queue.add(next); + tree[curr].add(next); + } + } + } + System.out.println(roots.size()); + Collections.sort(roots); + for (String name : roots) { + System.out.print(name + " "); + } + System.out.println(); + for (Map.Entry e : map.entrySet()) { + StringBuilder output = new StringBuilder(e.getKey()); + ArrayList childs = tree[e.getValue()]; + output.append(" " + childs.size() + " "); + ArrayList tNames = new ArrayList<>(); + for (int child : childs) { + tNames.add(names[child]); + } + Collections.sort(tNames); + for (String name : tNames) { + output.append(name + " ");; + } + System.out.println(output.toString()); + } + } +} From cc0576a4a051ba3768d02cba61ca1949f9324fbf Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 22 May 2022 13:15:08 +0900 Subject: [PATCH 037/111] =?UTF-8?q?[Baekjoon]=209694:=20=EB=AC=B4=EC=97=87?= =?UTF-8?q?=EC=9D=84=20=EC=95=84=EB=8A=90=EB=83=90=EA=B0=80=20=EC=95=84?= =?UTF-8?q?=EB=8B=88=EB=9D=BC=20=EB=88=84=EA=B5=AC=EB=A5=BC=20=EC=95=84?= =?UTF-8?q?=EB=8A=90=EB=83=90=EA=B0=80=20=EB=AC=B8=EC=A0=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\254\270\354\240\234\353\213\244.java" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "sangryeol/Week_6/\353\254\264\354\227\207\354\235\204 \354\225\204\353\212\220\353\203\220\352\260\200 \354\225\204\353\213\210\353\235\274 \353\210\204\352\265\254\353\245\274 \354\225\204\353\212\220\353\203\220\352\260\200 \353\254\270\354\240\234\353\213\244.java" diff --git "a/sangryeol/Week_6/\353\254\264\354\227\207\354\235\204 \354\225\204\353\212\220\353\203\220\352\260\200 \354\225\204\353\213\210\353\235\274 \353\210\204\352\265\254\353\245\274 \354\225\204\353\212\220\353\203\220\352\260\200 \353\254\270\354\240\234\353\213\244.java" "b/sangryeol/Week_6/\353\254\264\354\227\207\354\235\204 \354\225\204\353\212\220\353\203\220\352\260\200 \354\225\204\353\213\210\353\235\274 \353\210\204\352\265\254\353\245\274 \354\225\204\353\212\220\353\203\220\352\260\200 \353\254\270\354\240\234\353\213\244.java" new file mode 100644 index 0000000..ce41e5a --- /dev/null +++ "b/sangryeol/Week_6/\353\254\264\354\227\207\354\235\204 \354\225\204\353\212\220\353\203\220\352\260\200 \354\225\204\353\213\210\353\235\274 \353\210\204\352\265\254\353\245\274 \354\225\204\353\212\220\353\203\220\352\260\200 \353\254\270\354\240\234\353\213\244.java" @@ -0,0 +1,78 @@ +import java.io.*; +import java.util.*; + +class Relationship implements Comparable { + int to, cost; + Relationship(int to, int cost) { + this.to = to; + this.cost = cost; + } + + @Override + public int compareTo(Relationship o) { + return cost - o.cost; + } +} + +public class BOJ_9694 { + static int TC, N, M; + static ArrayList[] adj; + static int[] parent; + static final int INF = (int)1e9; + static void dijk() { + PriorityQueue pq = new PriorityQueue<>(); + int[] dist = new int[M]; + Arrays.fill(dist, INF); + Arrays.fill(parent, -1); + pq.add(new Relationship(0, 0)); + dist[0] = 0; + while (!pq.isEmpty()) { + Relationship curr = pq.poll(); + if (curr.cost > dist[curr.to]) continue; + for (Relationship next : adj[curr.to]) { + if (dist[next.to] > dist[curr.to] + next.cost) { + dist[next.to] = dist[curr.to] + next.cost; + parent[next.to] = curr.to; + pq.add(next); + } + } + } + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + TC = Integer.parseInt(br.readLine()); + for (int t = 1; t <= TC; t++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + adj = new ArrayList[M + 1]; + for (int i = 0; i <= M; i++) { + adj[i] = new ArrayList<>(); + } + parent = new int[M]; + for (int i = 0, u, v, w; i < N; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + w = Integer.parseInt(st.nextToken()); + adj[u].add(new Relationship(v, w)); + adj[v].add(new Relationship(u, w)); + } + dijk(); + if (parent[M - 1] != -1) { + int curr = M - 1; + Stack stk = new Stack<>(); + while (curr != -1) { + stk.add(curr); + curr = parent[curr]; + } + System.out.print("Case #" + t + ": "); + while (!stk.isEmpty()) { + System.out.print(stk.pop() + " "); + } + System.out.println(); + } + else System.out.println("Case #" + t + ": -1"); + } + } +} From 3834d0fa7a4b72242e0f7f11848fbd6cb07b273f Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 22 May 2022 15:14:00 +0900 Subject: [PATCH 038/111] =?UTF-8?q?[Baekjoon]21276:=20=EA=B3=84=EB=B3=B4?= =?UTF-8?q?=20=EB=B3=B5=EC=9B=90=EA=B0=80=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_6/Boj21276.java | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 daejik/Week_6/Boj21276.java diff --git a/daejik/Week_6/Boj21276.java b/daejik/Week_6/Boj21276.java new file mode 100644 index 0000000..69c8c11 --- /dev/null +++ b/daejik/Week_6/Boj21276.java @@ -0,0 +1,99 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + + +public class Boj21276 { + static int N, M; + static int arr[]; + static ArrayList> ans = new ArrayList<>(); + static ArrayList> list = new ArrayList<>(); + static ArrayList ancestorList = new ArrayList(); + static HashMap getIdxMap = new HashMap(); + static HashMap getNameMap = new HashMap<>(); + static Queue q = new LinkedList<>(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + arr = new int[N + 1]; + // ans: ڼյ Ʈ + // list: - ڼ 谡 Ʈ + for(int i=0; i()); + list.add(new ArrayList()); + } + + // N ̸ Է ޾ ̸ -> map ֱ + StringTokenizer st = new StringTokenizer(br.readLine()); + ArrayList inputName = new ArrayList(); + for(int i=0; i 0 Ǹ, ڼ ã + // 0̴ = nowIdx ڼ nextIdx̴. + while(!q.isEmpty()) { + int nowIdx = q.poll(); + + for(int nextIdx : list.get(nowIdx)) { + if(--arr[nextIdx] == 0) { + q.add(nextIdx); + ans.get(nowIdx).add(getNameMap.get(nextIdx)); + } + } + } + + // (3) + System.out.println(ancestorList.size()); + Collections.sort(ancestorList); + for(String ancestorName : ancestorList) { + System.out.print(ancestorName + " "); + } + System.out.println(); + for(int i=0; i Date: Sun, 22 May 2022 15:14:42 +0900 Subject: [PATCH 039/111] =?UTF-8?q?[Baekjoon]9694:=20=EB=AC=B4=EC=97=87?= =?UTF-8?q?=EC=9D=84=20=EC=95=84=EB=8A=90=EB=83=90=EA=B0=80=EC=95=84?= =?UTF-8?q?=EB=8B=88=EB=9D=BC=20=EB=88=84=EA=B5=AC=EB=A5=BC=20=EC=95=84?= =?UTF-8?q?=EB=8A=90=EB=83=90=EA=B0=80=20=EB=AC=B8=EC=A0=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_6/Boj9694.java | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 daejik/Week_6/Boj9694.java diff --git a/daejik/Week_6/Boj9694.java b/daejik/Week_6/Boj9694.java new file mode 100644 index 0000000..4874701 --- /dev/null +++ b/daejik/Week_6/Boj9694.java @@ -0,0 +1,100 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Stack; +import java.util.StringTokenizer; + + +public class Boj9694 { + static int T, N, M; + static int visited[] = new int[21]; + static ArrayList ans = new ArrayList(); + static ArrayList tmp = new ArrayList(); + //static ArrayList + static ArrayList> graph = new ArrayList<>(); + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + T = Integer.parseInt(br.readLine()); + + // graph + for(int i=1; i<=21; i++) { + graph.add(new ArrayList<>()); + } + + for(int t=1; t<=T; t++) { + // ʱȭ + ans.clear(); + tmp.clear(); + Arrays.fill(visited, Integer.MAX_VALUE); + + // N, M Է + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + // graph ʱȭ + for(int i=0; i<=M; i++) { + graph.get(i).clear(); + } + + // Է + for(int i=0, x, y, z; i Date: Sun, 22 May 2022 22:22:31 +0900 Subject: [PATCH 040/111] =?UTF-8?q?[Baekjoon]23629:=20=EB=84=88=EB=AC=B4?= =?UTF-8?q?=EB=81=94=EC=A7=81=ED=96=88=EB=8D=98=20=EC=88=98=EC=8B=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_6/Boj23629.java | 173 ++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 daejik/Week_6/Boj23629.java diff --git a/daejik/Week_6/Boj23629.java b/daejik/Week_6/Boj23629.java new file mode 100644 index 0000000..6e0b7a2 --- /dev/null +++ b/daejik/Week_6/Boj23629.java @@ -0,0 +1,173 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class Boj23629 { + static String input, ansStr; + static ArrayList numberList = new ArrayList(); + static ArrayList opList = new ArrayList(); + static HashMap strToNumMap = new HashMap<>(); + static HashMap numToStrMap = new HashMap<>(); + static HashSet opSet = new HashSet<>(); + static final String err = "Madness!"; + static long ans; // int  . + + + public static void main(String[] args) throws IOException { + int nowIdx = 0, finalIdx = 0; + initMapSet(); + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + input = br.readLine(); + finalIdx = input.length(); + + while(nowIdx < finalIdx) { + // 1. i ~ j ڿ ڷ ȯ + // 0~9 شϴ ܾ ƴҶ ݺ => list + // ̿ܿ Madness! + int numEndIdx = findNumEndIdx(input, nowIdx, finalIdx); + int opIdx = numEndIdx + 1; + + // ڸ ãҰų, ε ų, ܾ ̿ ڰ ִ + if(numEndIdx == -1 || !(opIdx < finalIdx || nowIdx <= numEndIdx) || (opIdx < nowIdx + 3)) { + System.out.println(err); + return; + } + + int num = convToNum(input, nowIdx, numEndIdx + 1); + Character op = input.charAt(opIdx); + + // ȯ ڿ ̻ϰų ڰ ̻ + if(num == -1 || !opSet.contains(op)) { + System.out.println(err); + return; + } + numberList.add(num); + opList.add(op); + + nowIdx = opIdx + 1; + } + // 2. -> ݺ ϸ鼭 print() + ans = numberList.get(0); + + for(int i=0; i 0) { + if(opList.get(i - 1) == '+') ans += (long) numberList.get(i); + else if(opList.get(i - 1) == '-') ans -= (long)numberList.get(i); + else if(opList.get(i - 1) == 'x') ans *= (long)numberList.get(i); + else if(opList.get(i - 1) == '/') ans /= (long)numberList.get(i); + } + } + System.out.println(); + // 3. ٽ ȯ + if(ans < 0) { + System.out.print('-'); + ans *= -1; + } + while(ans > 0) { + int tmp = (int) (ans % 10); + if(ansStr == null) ansStr = numToStrMap.get(tmp); + else ansStr = numToStrMap.get(tmp) + ansStr; + + ans /= 10; + } + + if(ansStr == null) ansStr = numToStrMap.get(0); + System.out.println(ansStr); + } + public static int findNumEndIdx(String str, int startIdx, int strLength) { + // ڰ ϴ idx ã + int idx = strLength; + + int tmpIdx = str.indexOf("+", startIdx); + if(tmpIdx > -1 && tmpIdx < idx) + idx = tmpIdx; + tmpIdx = str.indexOf("-", startIdx); + if(tmpIdx > -1 && tmpIdx < idx) + idx = tmpIdx; + tmpIdx = str.indexOf("x", startIdx); + if(tmpIdx > -1 && tmpIdx < idx) + idx = tmpIdx; + tmpIdx = str.indexOf("/", startIdx); + if(tmpIdx > -1 && tmpIdx < idx) + idx = tmpIdx; + tmpIdx = str.indexOf("=", startIdx); + if(tmpIdx > -1 && tmpIdx < idx) + idx = tmpIdx; + + if(idx >= strLength) return -1; + return idx - 1; + } + public static int convToNum(String str, int startIdx, int endIdx) { + int ret = 0; + + while(startIdx < endIdx) { + String strLen3 = null; + String strLen4 = null; + String strLen5 = null; + int num3 = 0, num4 = 0, num5 = 0; + int len = 0, num = 0; +//ONETWOTHREE +//SEVENTWO + if(startIdx + 3 <= endIdx) { + strLen3 = str.substring(startIdx, startIdx + 3); + //SEV + num3 = strToNumMap.getOrDefault(strLen3, -1); + } + if(startIdx + 4 <= endIdx) { + strLen4 = str.substring(startIdx, startIdx + 4); + num4 = strToNumMap.getOrDefault(strLen4, -1); + } + if(startIdx + 5 <= endIdx) { + strLen5 = str.substring(startIdx, startIdx + 5); + num5 = strToNumMap.getOrDefault(strLen5, -1); + } + + if(strLen3 != null && num3 != -1) { + // ONE, TWO, SIX + len = 3; + num = num3; + } else if(strLen4 != null && num4 != -1) { + // ZERO, FOUR, FIVE, NINE + len = 4; + num = num4; + } else if(strLen5 != null && num5 != -1) { + // THREE, SEVEN, EIGHT + len = 5; + num = num5; + } + + if(len == 0) { + return -1; + } + startIdx += len; + ret += num; + + if(startIdx < endIdx) ret *= 10; + } + + return ret; + } + public static void initMapSet() { + // strToNumMapʱȭ + strToNumMap.put("ZERO", 0); strToNumMap.put("ONE", 1); strToNumMap.put("TWO", 2); + strToNumMap.put("THREE", 3); strToNumMap.put("FOUR", 4); strToNumMap.put("FIVE", 5); + strToNumMap.put("SIX", 6); strToNumMap.put("SEVEN", 7); strToNumMap.put("EIGHT", 8); + strToNumMap.put("NINE", 9); + + // numToStrMap ʱȭ + numToStrMap.put(0, "ZERO"); numToStrMap.put(1, "ONE"); numToStrMap.put(2, "TWO"); + numToStrMap.put(3, "THREE"); numToStrMap.put(4, "FOUR"); numToStrMap.put(5, "FIVE"); + numToStrMap.put(6, "SIX"); numToStrMap.put(7, "SEVEN"); numToStrMap.put(8, "EIGHT"); + numToStrMap.put(9, "NINE"); + + // opSet ʱȭ + opSet.add('+'); opSet.add('-'); opSet.add('x'); opSet.add('/'); opSet.add('='); + } +} From 70342f40900aad930e35c177f29c24780c9d6075 Mon Sep 17 00:00:00 2001 From: leedjik Date: Mon, 23 May 2022 22:26:42 +0900 Subject: [PATCH 041/111] =?UTF-8?q?[Baekjoon]16472:=20=EA=B3=A0=EB=83=A5?= =?UTF-8?q?=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_7/Boj16472.java | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 daejik/Week_7/Boj16472.java diff --git a/daejik/Week_7/Boj16472.java b/daejik/Week_7/Boj16472.java new file mode 100644 index 0000000..53b7a95 --- /dev/null +++ b/daejik/Week_7/Boj16472.java @@ -0,0 +1,62 @@ +package Week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Boj16472 { + static int N; + static int recogNum, ans; + static int check[] = new int['z'-'a'+1]; + static String input; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + input = br.readLine(); + + int startIdx = 0, endIdx = 0; + int len = 1, recogNum = 1; + check[input.charAt(0) - 'a']++; + + + while(endIdx < input.length()) { + // ִ + if(len > ans) ans = len; + + int nextIdx = endIdx + 1; + if(nextIdx >= input.length()) break; + + // ĺ üũ + char nextAlpha = input.charAt(nextIdx); + + // ̹ ν ִ ĺ + if(check[nextAlpha - 'a'] > 0) { + len++; + endIdx++; + check[nextAlpha- 'a']++; + } + // ν ĺ + else { + // ϳ ν + if(recogNum < N) { + recogNum++; + check[nextAlpha - 'a']++; + endIdx++; + len++; + } + // ν Ұ + // startIdx ĭ ̸鼭, νߴ ĺ üũ īƮ + else { + char startAlpha = input.charAt(startIdx); + check[startAlpha - 'a']--; + if(check[startAlpha - 'a'] == 0) recogNum--; + startIdx++; + len--; + } + } + } + System.out.println(ans); + } + +} From 16acd17981947c6cabb6e61568ca9746009ac8d7 Mon Sep 17 00:00:00 2001 From: Heize Date: Mon, 6 Jun 2022 09:53:40 +0900 Subject: [PATCH 042/111] =?UTF-8?q?[Baekjoon]=2022251:=20=EB=B9=8C?= =?UTF-8?q?=EB=9F=B0=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_7/BOJ_22251.java | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sangryeol/Week_7/BOJ_22251.java diff --git a/sangryeol/Week_7/BOJ_22251.java b/sangryeol/Week_7/BOJ_22251.java new file mode 100644 index 0000000..b1c2a61 --- /dev/null +++ b/sangryeol/Week_7/BOJ_22251.java @@ -0,0 +1,47 @@ +import java.io.*; +import java.util.*; + +public class BOJ_22251 { + static int N, K, P, X; + static int[] sevenSegment = new int[] { + 0b1110111, // 0 + 0b0100100, // 1 + 0b1011110, // 2 + 0b1101110, // 3 + 0b0101101, // 4 + 0b1101011, // 5 + 0b1111011, // 6 + 0b0100110, // 7 + 0b1111111, // 8 + 0b1101111 // 9 + }; + static int[][] count = new int[10][10]; + static void init() { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + count[i][j] = Integer.bitCount(sevenSegment[i] ^ sevenSegment[j]); + } + } + } + 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()); + K = Integer.parseInt(st.nextToken()); + P = Integer.parseInt(st.nextToken()); + X = Integer.parseInt(st.nextToken()); + init(); + int ans = 0; + for (int i = 1; i <= N; i++) { + int from = X, to = i; + int cnt = 0; + for (int j = 0; j < K; j++) { + cnt += count[from % 10][to % 10]; + from /= 10; + to /= 10; + } + if (cnt > 0 && cnt <= P) ans++; + } + System.out.println(ans); + } +} From 632ea509da8e096aa9f5bec3355242db94f0883d Mon Sep 17 00:00:00 2001 From: Heize Date: Mon, 6 Jun 2022 09:53:52 +0900 Subject: [PATCH 043/111] [Baekjoon] 13244: Tree --- sangryeol/Week_7/BOJ_13244.java | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sangryeol/Week_7/BOJ_13244.java diff --git a/sangryeol/Week_7/BOJ_13244.java b/sangryeol/Week_7/BOJ_13244.java new file mode 100644 index 0000000..f72fa20 --- /dev/null +++ b/sangryeol/Week_7/BOJ_13244.java @@ -0,0 +1,42 @@ +import java.io.*; +import java.util.*; + +public class BOJ_13244 { + static int tc, N, M; + static int[] parent; + static void init(int N) { + parent = new int[N + 1]; + } + static int find(int n) { + if (parent[n] <= 0) return n; + return parent[n] = find(parent[n]); + } + static boolean union(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return false; + parent[u] = v; + return true; + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + tc = Integer.parseInt(br.readLine()); + for (int i = 0; i < tc; i++) { + N = Integer.parseInt(br.readLine()); + M = Integer.parseInt(br.readLine()); + init(N); + boolean isTree = true; + for (int j = 0, u, v; j < M; j++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + isTree &= union(u, v); + } + int root = find(1); + for (int j = 2; j <= N; j++) { + isTree &= (root == find(j)); + } + System.out.println(isTree ? "tree" : "graph"); + } + } +} \ No newline at end of file From a5762d29fa3b46d579c4d8784eb2636f72b1ad7c Mon Sep 17 00:00:00 2001 From: Heize Date: Mon, 6 Jun 2022 09:54:18 +0900 Subject: [PATCH 044/111] =?UTF-8?q?[Baekjoon]=2016472:=20=EA=B3=A0?= =?UTF-8?q?=EB=83=A5=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_7/BOJ_16472.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sangryeol/Week_7/BOJ_16472.java diff --git a/sangryeol/Week_7/BOJ_16472.java b/sangryeol/Week_7/BOJ_16472.java new file mode 100644 index 0000000..7af41d5 --- /dev/null +++ b/sangryeol/Week_7/BOJ_16472.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; + +public class BOJ_16472 { + static int N; + static int[] alphabet = new int[26]; + static int getIdx(char c) { + return c - 'a'; + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + char[] meow = br.readLine().toCharArray(); + int bound = meow.length; + int s = 0, e = 0, idx; + int cnt = 0, ans = 0; + while (e < bound) { + idx = getIdx(meow[e++]); + if (++alphabet[idx] == 1) cnt++; + while (cnt > N) { + idx = getIdx(meow[s++]); + if (--alphabet[idx] == 0) cnt--; + } + ans = Math.max(ans, e - s); + } + System.out.println(ans); + } +} \ No newline at end of file From 2461e8052ac4195fec6c29079e415f1927e3474f Mon Sep 17 00:00:00 2001 From: leedjik Date: Mon, 6 Jun 2022 18:38:47 +0900 Subject: [PATCH 045/111] [Baekjoon]13244: Tree --- daejik/Week_7/Boj13244.java | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 daejik/Week_7/Boj13244.java diff --git a/daejik/Week_7/Boj13244.java b/daejik/Week_7/Boj13244.java new file mode 100644 index 0000000..a3dc150 --- /dev/null +++ b/daejik/Week_7/Boj13244.java @@ -0,0 +1,83 @@ +package Week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +class Pair{ + int a, b; + + Pair(int a, int b) { + this.a = a; + this.b = b; + } +} +public class Boj13244 { + private static int T, N, M; + private static int parent[] = new int[1001]; + private static final String tree = "tree"; + private static final String graph = "graph"; + + public static void main(String[] args) throws IOException { + // TODO Auto-generated method stub + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + T = Integer.parseInt(br.readLine()); + + while(T-- > 0) { + N = Integer.parseInt(br.readLine()); + M = Integer.parseInt(br.readLine()); + Arrays.fill(parent, 0); + for(int i=1; i<=N; i++) parent[i] = i; + boolean treeFlag = false; + + while(M-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + treeFlag = union(a, b); + + if(treeFlag == false) break; + } + + if(treeFlag == false) { + System.out.println(graph); + continue; + } + + int parentNum = find(1); + + for(int i=2; i<=N; i++) { + if(parentNum != find(i)) { + treeFlag = false; + break; + } + } + + if(treeFlag == true) { + System.out.println(tree); + } + else { + System.out.println(graph); + } + } + } + public static boolean union(int a, int b) { + a = find(a); + b = find(b); + + if(a == b) { + return false; + } + else { + parent[a] = b; + return true; + } + } + public static int find(int n) { + if(parent[n] == n) return n; + return parent[n] = find(parent[n]); + } +} From e42c923edb81e328a33a7e05f06b565726c55e95 Mon Sep 17 00:00:00 2001 From: leedjik Date: Mon, 6 Jun 2022 19:37:22 +0900 Subject: [PATCH 046/111] =?UTF-8?q?[Baekjoon]22251:=20=EB=B9=8C=EB=9F=B0?= =?UTF-8?q?=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_7/Boj22251.java | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 daejik/Week_7/Boj22251.java diff --git a/daejik/Week_7/Boj22251.java b/daejik/Week_7/Boj22251.java new file mode 100644 index 0000000..a7f10d2 --- /dev/null +++ b/daejik/Week_7/Boj22251.java @@ -0,0 +1,83 @@ +package Week7; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Boj22251 { + static boolean led[][] = + { + {true, true, true, true, true, true, false}, // 0 + {false, true, true, false, false, false, false}, // 1 + {true, true, false, true, true, false, true}, // 2 + {true, true, true, true, false, false, true}, // 3 + {false, true, true, false, false, true, true}, // 4 + {true, false, true, true, false, true, true}, // 5 + {true, false, true, true, true, true, true}, // 6 + {true, true, true, false, false, false, false}, // 7 + {true, true, true, true, true, true, true}, // 8 + {true, true, true, true, false, true, true} // 9 + }; + static int[] nowFloor; + static int convCnt[][] = new int[10][10]; + static boolean check[] = new boolean[1000000]; + static int N, K, P, X, ans; + + public static void init() { + for(int i=0; i<10; i++) { + for(int j=0; j<10; j++) { + if(i==j) continue; + // i -> j ٲٴµ ʿ toggle Ƚ + int cnt = 0; + for(int k=0; k<7; k++) { + if(led[i][k] != led[j][k]) cnt++; + } + convCnt[i][j] = cnt; + } + } + } + public static void main(String[] args) throws IOException { + init(); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + P = Integer.parseInt(st.nextToken()); + X = Integer.parseInt(st.nextToken()); + + nowFloor = intToArr(X); + check[X] = true; + for(int i=1; i<=N; i++) { + if(i == X) continue; + + int[] arr = intToArr(i); + if(canConvert(arr) == true) ans++; + } + + System.out.println(ans); + } + public static boolean canConvert(int[] arr) { + int toggleCnt = 0; + + for(int idx=0; idx= 1) return true; + return false; + } + public static int[] intToArr(int n) { + int[] arr = new int[K]; + for(int idx = K - 1; idx >= 0 && n > 0; idx--) { + arr[idx] = n % 10; + n /= 10; + } + return arr; + } + +} From ecf46374c6e626ae3940a7e18949ae7dadefdbbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 19 Jun 2022 10:40:37 +0900 Subject: [PATCH 047/111] =?UTF-8?q?[Baekjoon]=2021924:=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EA=B1=B4=EC=84=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_8/BOJ_21924.java | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sangryeol/Week_8/BOJ_21924.java diff --git a/sangryeol/Week_8/BOJ_21924.java b/sangryeol/Week_8/BOJ_21924.java new file mode 100644 index 0000000..f6ecef7 --- /dev/null +++ b/sangryeol/Week_8/BOJ_21924.java @@ -0,0 +1,61 @@ +import java.util.*; +import java.io.*; + +class Node implements Comparable { + int u, v, w; + Node(int u, int v, int w) { + this.u = u; + this.v = v; + this.w = w; + } + @Override + public int compareTo(Node o) { + return w - o.w; + } +} + +public class BOJ_21924 { + static int N, M; + static int[] parent; + + static int find(int n) { + if (parent[n] <= 0) return n; + return parent[n] = find(parent[n]); + } + + static boolean union(int u, int v) { + u = find(u); + v = find(v); + if (u == v) return false; + parent[u] = v; + return true; + } + + 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()); + ArrayList graphs = new ArrayList<>(); + parent = new int[N + 1]; + long ans = 0; + for (int i = 1, u, v, w; i <= M; i++) { + st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + w = Integer.parseInt(st.nextToken()); + graphs.add(new Node(u, v, w)); + ans += w; + } + Collections.sort(graphs); + int cnt = 0; + for (int i = 0; i < M && cnt < N - 1; i++) { + Node node = graphs.get(i); + if (union(node.u, node.v)) { + ans -= node.w; + cnt++; + } + } + System.out.println(cnt != N - 1 ? -1 : ans); + } +} From ec52193472fd269e1f1552cc17e1c8ce177607c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 19 Jun 2022 10:41:57 +0900 Subject: [PATCH 048/111] =?UTF-8?q?[Baekjoon]=2022252:=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=83=81=EC=9D=B8=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_8/BOJ_22252.java | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sangryeol/Week_8/BOJ_22252.java diff --git a/sangryeol/Week_8/BOJ_22252.java b/sangryeol/Week_8/BOJ_22252.java new file mode 100644 index 0000000..45112dc --- /dev/null +++ b/sangryeol/Week_8/BOJ_22252.java @@ -0,0 +1,35 @@ +import java.util.*; +import java.io.*; + +public class BOJ_22252 { + static int Q; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + Q = Integer.parseInt(br.readLine()); + HashMap> information = new HashMap<>(); + long ans = 0; + for (int i = 1, q, k, c; i <= Q; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + q = Integer.parseInt(st.nextToken()); + String name = st.nextToken(); + k = Integer.parseInt(st.nextToken()); + PriorityQueue pq = information.getOrDefault(name, null); + if (q == 1) { + if (pq == null) pq = new PriorityQueue<>(Comparator.reverseOrder()); + for (int j = 0; j < k; j++) { + int value = Integer.parseInt(st.nextToken()); + pq.add(value); + } + information.put(name, pq); + } + else { + if (pq == null) continue; + int b = Math.min(k, pq.size()); + for (int j = 0; j < b; j++) { + ans += pq.poll(); + } + } + } + System.out.println(ans); + } +} From 662593303a6610ffdc466a7c3130be6223f52241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 19 Jun 2022 10:42:17 +0900 Subject: [PATCH 049/111] =?UTF-8?q?[Baekjoon]=203584:=20=EA=B0=80=EC=9E=A5?= =?UTF-8?q?=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20=EA=B3=B5=ED=86=B5=20=EC=A1=B0?= =?UTF-8?q?=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_8/BOJ_3584.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 sangryeol/Week_8/BOJ_3584.java diff --git a/sangryeol/Week_8/BOJ_3584.java b/sangryeol/Week_8/BOJ_3584.java new file mode 100644 index 0000000..e0b9ee3 --- /dev/null +++ b/sangryeol/Week_8/BOJ_3584.java @@ -0,0 +1,31 @@ +import java.io.*; +import java.util.*; + +public class BOJ_3584 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int tc = Integer.parseInt(br.readLine()); + while (tc-- > 0) { + int N = Integer.parseInt(br.readLine()); + int[] parent = new int[N + 1]; + boolean[] visited = new boolean[N + 1]; + for (int i = 1, u, v; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + u = Integer.parseInt(st.nextToken()); + v = Integer.parseInt(st.nextToken()); + parent[v] = u; + } + StringTokenizer st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + while (u != 0) { + visited[u] = true; + u = parent[u]; + } + while (!visited[v]) { + v = parent[v]; + } + System.out.println(v); + } + } +} From b07225d9e83a6d47fd0af37e48f19ccaec8454c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 19 Jun 2022 10:42:40 +0900 Subject: [PATCH 050/111] =?UTF-8?q?[Baekjoon]=2022254:=20=EA=B3=B5?= =?UTF-8?q?=EC=9E=A5=20=EC=BB=A8=EC=84=A4=ED=84=B4=ED=8A=B8=20=ED=98=B8?= =?UTF-8?q?=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_8/BOJ_22254.java | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 sangryeol/Week_8/BOJ_22254.java diff --git a/sangryeol/Week_8/BOJ_22254.java b/sangryeol/Week_8/BOJ_22254.java new file mode 100644 index 0000000..cc9578a --- /dev/null +++ b/sangryeol/Week_8/BOJ_22254.java @@ -0,0 +1,38 @@ +import java.io.*; +import java.util.*; + +public class BOJ_22254 { + static int N, X; + static int[] times; + static boolean isPossible(int m) { + PriorityQueue pq = new PriorityQueue<>(); + int bound = Math.min(N, m); + for (int i = 1; i <= bound; i++) { + pq.add(times[i]); + } + for (int i = bound + 1; i <= N; i++) { + int time = pq.poll(); + if (time + times[i] > X) return false; + pq.add(time + times[i]); + } + return pq.poll() <= X; + } + 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()); + X = Integer.parseInt(st.nextToken()); + times = new int[N + 1]; + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + times[i] = Integer.parseInt(st.nextToken()); + } + int lo = 0, hi = 100010; + while (lo + 1 < hi) { + int mid = (lo + hi) / 2; + if (isPossible(mid)) hi = mid; + else lo = mid; + } + System.out.println(hi); + } +} From 86592d5ddb3a2d23d9e9d61d7c33cbc997b74aef Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 13:54:12 +0900 Subject: [PATCH 051/111] =?UTF-8?q?[Baekjoon]=203584:=20=EA=B0=80=EC=9E=A5?= =?UTF-8?q?=20=EA=B0=80=EA=B9=8C=EC=9A=B4=20=EA=B3=B5=ED=86=B5=20=EC=A1=B0?= =?UTF-8?q?=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" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "sunguk/src/Week_8/Boj_3584_\352\260\200\354\236\245_\352\260\200\352\271\214\354\232\264_\352\263\265\355\206\265_\354\241\260\354\203\201.java" diff --git "a/sunguk/src/Week_8/Boj_3584_\352\260\200\354\236\245_\352\260\200\352\271\214\354\232\264_\352\263\265\355\206\265_\354\241\260\354\203\201.java" "b/sunguk/src/Week_8/Boj_3584_\352\260\200\354\236\245_\352\260\200\352\271\214\354\232\264_\352\263\265\355\206\265_\354\241\260\354\203\201.java" new file mode 100644 index 0000000..94b0c81 --- /dev/null +++ "b/sunguk/src/Week_8/Boj_3584_\352\260\200\354\236\245_\352\260\200\352\271\214\354\232\264_\352\263\265\355\206\265_\354\241\260\354\203\201.java" @@ -0,0 +1,105 @@ +package Week_8; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +/** + * @link https://www.acmicpc.net/problem/3584 + */ +class Boj_3584_가장_가까운_공통_조상 { + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int caseCount = Integer.parseInt(br.readLine()); + while (caseCount-- > 0) { + Map nodeMap = new HashMap<>(); + int nodeCount = Integer.parseInt(br.readLine()); + while (nodeCount-- > 1) { // N-1 번 노드 정보를 읽음 + String line = br.readLine(); + StringTokenizer st = new StringTokenizer(line, " "); + int parentNum = Integer.parseInt(st.nextToken()); + int childNum = Integer.parseInt(st.nextToken()); + + Node parentNode = nodeMap.getOrDefault(parentNum, new Node(parentNum)); + Node childNode = nodeMap.getOrDefault(childNum, new Node(childNum)); + + parentNode.addChild(childNode); + childNode.setParent(parentNode); + nodeMap.put(parentNum, parentNode); + nodeMap.put(childNum, childNode); + + } + String line = br.readLine(); + StringTokenizer st = new StringTokenizer(line, " "); + int firstNodeNum = Integer.parseInt(st.nextToken()); + int secondNodeNum = Integer.parseInt(st.nextToken()); + + Node firstNode = nodeMap.get(firstNodeNum); + Set parentsSet = getParentsSet(firstNode); + Node currentNode = nodeMap.get(secondNodeNum); + while (!parentsSet.contains(currentNode)) { + currentNode = currentNode.getParent(); + if (currentNode.isRoot()) { + break; + } + } + System.out.println(currentNode.getId()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private Set getParentsSet(Node node) { + Node currentNode = node; + Set parentsSet = new HashSet<>(); + parentsSet.add(currentNode); + while (!currentNode.isRoot()) { + parentsSet.add(currentNode.getParent()); + currentNode = currentNode.getParent(); + } + return parentsSet; + } + + +} + +class Node { + public int getId() { + return id; + } + + public Node getParent() { + return parent; + } + + public List getChildren() { + return children; + } + + private final int id; + private Node parent; + private final List children = new ArrayList<>(); + + public Node(int id) { + this.id = id; + } + + void addChild(Node node) { + if (node != null) { + children.add(node); + } + } + + void setParent(Node node) { + if (node != null) { + parent = node; + } + } + + boolean isRoot() { + return parent == null; + } +} From 03ba732f3935104de161418e9cb354277149e791 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 13:54:28 +0900 Subject: [PATCH 052/111] =?UTF-8?q?[Baekjoon]=2022252:=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=83=81=EC=9D=B8=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...354\235\270_\355\230\270\354\204\235.java" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "sunguk/src/Week_8/Boj_22252_\354\240\225\353\263\264_\354\203\201\354\235\270_\355\230\270\354\204\235.java" diff --git "a/sunguk/src/Week_8/Boj_22252_\354\240\225\353\263\264_\354\203\201\354\235\270_\355\230\270\354\204\235.java" "b/sunguk/src/Week_8/Boj_22252_\354\240\225\353\263\264_\354\203\201\354\235\270_\355\230\270\354\204\235.java" new file mode 100644 index 0000000..bd77685 --- /dev/null +++ "b/sunguk/src/Week_8/Boj_22252_\354\240\225\353\263\264_\354\203\201\354\235\270_\355\230\270\354\204\235.java" @@ -0,0 +1,52 @@ +package Week_8; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +/** + * @link https://www.acmicpc.net/problem/22252 + */ +public class Boj_22252_정보_상인_호석 { + + final int EARNING = 1; + final int BUYING = 2; + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int lineCounts = Integer.parseInt(br.readLine()); + Map> gorillaInfoMap = new HashMap<>(); + long total = 0; + while (lineCounts-- > 0) { + String line = br.readLine(); + StringTokenizer st = new StringTokenizer(line, " "); + int command = Integer.parseInt(st.nextToken()); + String gorillaName = st.nextToken(); + if (command == BUYING) { + int buyingCount = Integer.parseInt(st.nextToken()); + PriorityQueue items = gorillaInfoMap.get(gorillaName); + if (items == null) continue; + while (buyingCount-- > 0) { + if (items.isEmpty()) { + break; + } else { + total += items.poll(); + } + } + } else if (command == EARNING) { + PriorityQueue items = gorillaInfoMap.getOrDefault(gorillaName, new PriorityQueue(Comparator.reverseOrder())); + int itemCount = Integer.parseInt(st.nextToken()); + while (itemCount-- > 0) { + items.add(Integer.parseInt(st.nextToken())); + } + gorillaInfoMap.put(gorillaName, items); + } + } + System.out.println(total); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} From a90e8d9472c13e8ff797c6af410944477448c318 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 14:10:20 +0900 Subject: [PATCH 053/111] =?UTF-8?q?(WIP)=20[Baekjoon]=2021924:=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EA=B1=B4=EC=84=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...354\213\234_\352\261\264\354\204\244.java" | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 "sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" diff --git "a/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" "b/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" new file mode 100644 index 0000000..d0ff8f2 --- /dev/null +++ "b/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" @@ -0,0 +1,178 @@ +package Week_8; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +public class Boj_21924_도시_건설 { + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int buildingCount = Integer.parseInt(st.nextToken()); // N, 3~10^5 3~100000 + int roadCount = Integer.parseInt(st.nextToken()); // M, 2~ + + + long totalPrice = 0; + long minPrice = 0; + List roads = new ArrayList<>(); + + while (roadCount-- > 0) { + st = new StringTokenizer(br.readLine()); + int firstBuildingNumber = Integer.parseInt(st.nextToken()); + int secondBuildingNumber = Integer.parseInt(st.nextToken()); + int price = Integer.parseInt(st.nextToken()); + totalPrice += price; + Road road = new Road(firstBuildingNumber, secondBuildingNumber, price); + roads.add(road); + } + + roads.sort(Comparator.naturalOrder()); + + Map buildingMap = new HashMap<>(); + int selectedRoadCounts = 0; + for (Road road : roads) { + if (selectedRoadCounts == buildingCount - 1) break; + + int smallerBuildingNumber = road.getSmallerBuildingNumber(); + int biggerBuildingNumber = road.getBiggerBuildingNumber(); + + Building firstBuilding = buildingMap.getOrDefault(smallerBuildingNumber, new Building(smallerBuildingNumber)); + Building secondBuilding = buildingMap.getOrDefault(biggerBuildingNumber, new Building(biggerBuildingNumber)); + + int firstBuildingParentId = getParentBuildingOf(firstBuilding).getId(); + int secondBuildingParentId = getParentBuildingOf(secondBuilding).getId(); + + if (firstBuildingParentId != secondBuildingParentId) { + /** + * TODO: 22. 6. 19. building 번호가 낮은 것을 기준으로 부모로 설정하니 동기화시 문제가 됨 + * + * 7 6 + * 1 2 1 + * 3 4 1 + * 5 6 1 + * 1 7 2 + * 7 3 3 + * 5 7 3 + * + * 7 + * / | \ + * 1 3 5 + * | | | + * 2 4 6 + * + * 1, 3, 5 가 각각이 자신이 최상위 부모로 인지함. 동기화 필요 + */ + + syncParent(firstBuilding, secondBuilding); + buildingMap.put(smallerBuildingNumber, firstBuilding); + buildingMap.put(biggerBuildingNumber, secondBuilding); + minPrice += road.getPrice(); + selectedRoadCounts++; + } + } + + boolean hasFoundAnswer = true; + List buildings = new ArrayList<>(buildingMap.values()); + for (int i = 1; i < buildings.size(); i++) { + Building firstParent = updateParentBuildingOf(buildings.get(i - 1)); + Building secondParent = updateParentBuildingOf(buildings.get(i)); + if (firstParent.getId() != secondParent.getId()) { + hasFoundAnswer = false; + break; + } + } + + if (hasFoundAnswer) { + System.out.println(totalPrice - minPrice); + } else { + System.out.println(-1); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private Building updateParentBuildingOf(Building building) { + if (building.hasParent()) { + Building parentBuilding = getParentBuildingOf(building.getParentBuilding()); + building.setParentBuilding(parentBuilding); + return parentBuilding; + } + return building.getParentBuilding(); + } + + + private Building getParentBuildingOf(Building building) { + if (building.hasParent()) { + return getParentBuildingOf(building.getParentBuilding()); + } + return building.getParentBuilding(); + } + + private void syncParent(Building firstBuilding, Building secondBuilding) { + Building firstBuildingParent = getParentBuildingOf(firstBuilding); + Building secondBuildingParent = getParentBuildingOf(secondBuilding); + if (firstBuildingParent.getId() < secondBuildingParent.getId()) { + firstBuilding.setParentBuilding(secondBuildingParent); + } else { + secondBuilding.setParentBuilding(firstBuildingParent); + } + } +} + +class Road implements Comparable { + private final int smallerBuildingNumber; + private final int biggerBuildingNumber; + private final int price; + + public int getSmallerBuildingNumber() { + return smallerBuildingNumber; + } + + public int getBiggerBuildingNumber() { + return biggerBuildingNumber; + } + + public int getPrice() { + return price; + } + + public Road(int firstBuildingNumber, int secondBuildingNumber, int price) { + this.smallerBuildingNumber = Math.min(firstBuildingNumber, secondBuildingNumber); + this.biggerBuildingNumber = Math.max(firstBuildingNumber, secondBuildingNumber); + this.price = price; + } + + @Override + public int compareTo(Road o) { // ascending sort 오름차순 + return price - o.price; + } +} + + +class Building { + private final int id; + private Building parentBuilding = this; + + public Building(int id) { + this.id = id; + } + + public Building getParentBuilding() { + return parentBuilding; + } + + public int getId() { + return id; + } + + boolean hasParent() { + return parentBuilding.getId() != id; + } + + public void setParentBuilding(Building building) { + parentBuilding = building; + } +} \ No newline at end of file From 6ea903b7745e275abfa79db723e56eb60aed2987 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 16:03:12 +0900 Subject: [PATCH 054/111] =?UTF-8?q?(WIP)=20[Baekjoon]=2022254:=20=EA=B3=B5?= =?UTF-8?q?=EC=A0=95=20=EC=BB=A8=EC=84=A4=ED=84=B4=ED=8A=B8=20=ED=98=B8?= =?UTF-8?q?=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\212\270_\355\230\270\354\204\235.java" | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 "sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" diff --git "a/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" "b/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" new file mode 100644 index 0000000..15e92ef --- /dev/null +++ "b/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" @@ -0,0 +1,76 @@ +package Week_8; + + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +/** + * @link https://www.acmicpc.net/problem/22254 + */ +public class Boj_22254_공정_컨설턴트_호석 { + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String line = br.readLine(); + StringTokenizer st = new StringTokenizer(line, " "); + int orderCount = Integer.parseInt(st.nextToken()); + int leftTime = Integer.parseInt(st.nextToken()); + + List orders = new ArrayList(orderCount); + st = new StringTokenizer(br.readLine(), " "); + for (int i = 0; i < orderCount; i++) { + orders.add(new Order(Integer.parseInt(st.nextToken()))); + } + orders.sort((o1, o2) -> o2.getTime() - o1.getTime()); + int finalAnswer = orderCount; + int minFactoryCount = 1; + int maxFactoryCount = orderCount; + int mid = (minFactoryCount + maxFactoryCount) / 2; + + PriorityQueue pq = new PriorityQueue<>(orderCount); + while (minFactoryCount <= maxFactoryCount) { + boolean isPossible = true; + pq.clear(); + for (int i = 0; i < mid; i++) { + pq.offer(0); + } + for (Order order : orders) { + Integer factoryCapacity = pq.poll(); + if (factoryCapacity + order.getTime() > leftTime) { + isPossible = false; + break; + } + pq.offer(factoryCapacity + order.getTime()); + } + if (isPossible) { + finalAnswer = mid; + maxFactoryCount = mid - 1; + } else { + minFactoryCount = mid + 1; + } + mid = (minFactoryCount + maxFactoryCount) / 2; + } + System.out.println(finalAnswer); + } catch (Exception e) { + e.printStackTrace(); + } + } +} + +class Order { + + private final int time; + + public Order(int time) { + this.time = time; + } + + public int getTime() { + return time; + } +} \ No newline at end of file From e0161bba520ab8a44d6d54111516be7ec069600f Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 19 Jun 2022 16:03:49 +0900 Subject: [PATCH 055/111] =?UTF-8?q?[Baekjoon]21924:=20=EB=8F=84=EC=8B=9C?= =?UTF-8?q?=EA=B1=B4=EC=84=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_8/Boj21924.java | 128 ++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 daejik/Week_8/Boj21924.java diff --git a/daejik/Week_8/Boj21924.java b/daejik/Week_8/Boj21924.java new file mode 100644 index 0000000..d93c842 --- /dev/null +++ b/daejik/Week_8/Boj21924.java @@ -0,0 +1,128 @@ +package Week8; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Boj21924 { + private static int[] parent; + private static boolean[] visitedNode; + private static ArrayList> graph; + private static ArrayList vector; + private static Queue q; + private static int N, M; + private static long totalCost, ans; + + 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()); + + parent = new int[N + 1]; + visitedNode = new boolean[N + 1]; + vector = new ArrayList<>(); + q = new LinkedList(); + graph = new ArrayList<>(); + + for(int i=0; i<=N; i++) { + graph.add(new ArrayList()); + } + + for(int m=0; m{ + int a, b, cost; + + Struct(int a, int b, int cost){ + this.a = a; + this.b = b; + this.cost = cost; + } + + @Override + public int compareTo(Struct o) { + if(o.cost > cost) return -1; + else if(o.cost < cost) return 1; + else return 0; + } + + @Override + public String toString() { + return "(" + this.a + "," + this.b + "):"+ this.cost; + } +} From 38df7dbb8ef61dc23d64588078aa4a718e61ec93 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 19 Jun 2022 16:04:18 +0900 Subject: [PATCH 056/111] =?UTF-8?q?[Baekjoon]22252:=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=20=EC=83=81=EC=9D=B8=20=ED=98=B8=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_8/Boj22252.java | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 daejik/Week_8/Boj22252.java diff --git a/daejik/Week_8/Boj22252.java b/daejik/Week_8/Boj22252.java new file mode 100644 index 0000000..6e086c6 --- /dev/null +++ b/daejik/Week_8/Boj22252.java @@ -0,0 +1,71 @@ +package Week8; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.StringTokenizer; + +public class Boj22252 { + private static HashMap hashMap = new HashMap<>(); + private static ArrayList> arr = new ArrayList<>(); + private static int Q, gorilCnt; + private static long ans; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + Q = Integer.parseInt(br.readLine()); + + while(Q-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + int type = Integer.parseInt(st.nextToken()); + String name = st.nextToken(); + + String nameValue = hashMap.get(name); + int arrIdx = 0; + + // name - arrε + if(nameValue == null) { + arr.add(new ArrayList()); + arrIdx = gorilCnt; + hashMap.put(name, String.valueOf(arrIdx)); + gorilCnt++; + } + else { + arrIdx = Integer.parseInt(nameValue); + } + + // + if(type == 1) { + inputData(st, arrIdx); + } + // 󿡼 + else { + ans += (long)getData(st, arrIdx); + } + } + System.out.println(ans); + } + public static void inputData(StringTokenizer st, int idx) { + int inputNum = Integer.parseInt(st.nextToken()); + + for(int i=0; i 0 && !arr.get(idx).isEmpty()) { + ret += (int)arr.get(idx).get(0); + arr.get(idx).remove(0); + } + return ret; + } + +} From 5db4d4a378a94afdfea3ae4e12b4b620a566c5a7 Mon Sep 17 00:00:00 2001 From: leedjik Date: Sun, 19 Jun 2022 16:04:50 +0900 Subject: [PATCH 057/111] =?UTF-8?q?[Baekjoon]3584:=20=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B0=80=EA=B9=8C=EC=9A=B4=20=EA=B3=B5=ED=86=B5=EC=A1=B0?= =?UTF-8?q?=EC=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_8/Boj3584.java | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 daejik/Week_8/Boj3584.java diff --git a/daejik/Week_8/Boj3584.java b/daejik/Week_8/Boj3584.java new file mode 100644 index 0000000..4775705 --- /dev/null +++ b/daejik/Week_8/Boj3584.java @@ -0,0 +1,62 @@ +package Week8; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Boj3584 { + public static Queue q = new LinkedList(); + public static int T, N; + public static int[] visited = new int[100001]; + public static int[] parent = new int[100001]; + + public static void main(String[] args) throws IOException{ + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = null; + T = Integer.parseInt(br.readLine()); + + while(T-- > 0) { + Arrays.fill(visited, 0); + Arrays.fill(parent, 0); + while(!q.isEmpty()) q.poll(); + + N = Integer.parseInt(br.readLine()); + + for(int n=1; n= 1) { + System.out.println(parentNode); + break; + } + else if(visited[parentNode] == 0){ + visited[parentNode]++; + q.add(parentNode); + } + } + } + } +} From 16db8118ef8d02c4d1af6cbfdd5f7c7b56f9536c Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 17:12:32 +0900 Subject: [PATCH 058/111] =?UTF-8?q?[Baekjoon]=2022254:=20=EA=B3=B5?= =?UTF-8?q?=EC=A0=95=20=EC=BB=A8=EC=84=A4=ED=84=B4=ED=8A=B8=20=ED=98=B8?= =?UTF-8?q?=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" "b/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" index 15e92ef..dcccd31 100644 --- "a/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" +++ "b/sunguk/src/Week_8/Boj_22254_\352\263\265\354\240\225_\354\273\250\354\204\244\355\204\264\355\212\270_\355\230\270\354\204\235.java" @@ -26,8 +26,8 @@ public void run() { for (int i = 0; i < orderCount; i++) { orders.add(new Order(Integer.parseInt(st.nextToken()))); } - orders.sort((o1, o2) -> o2.getTime() - o1.getTime()); int finalAnswer = orderCount; + int minFactoryCount = 1; int maxFactoryCount = orderCount; int mid = (minFactoryCount + maxFactoryCount) / 2; From 62b63fd27c679f5ff935b368623f1c2c4300d172 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 19 Jun 2022 21:11:25 +0900 Subject: [PATCH 059/111] =?UTF-8?q?[Baekjoon]=2021924:=20=EB=8F=84?= =?UTF-8?q?=EC=8B=9C=20=EA=B1=B4=EC=84=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...354\213\234_\352\261\264\354\204\244.java" | 138 ++++++------------ 1 file changed, 43 insertions(+), 95 deletions(-) diff --git "a/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" "b/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" index d0ff8f2..bd01491 100644 --- "a/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" +++ "b/sunguk/src/Week_8/Boj_21924_\353\217\204\354\213\234_\352\261\264\354\204\244.java" @@ -2,10 +2,15 @@ import java.io.BufferedReader; import java.io.InputStreamReader; -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.StringTokenizer; public class Boj_21924_도시_건설 { + private int[] parent; + public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -20,65 +25,44 @@ public void run() { while (roadCount-- > 0) { st = new StringTokenizer(br.readLine()); - int firstBuildingNumber = Integer.parseInt(st.nextToken()); - int secondBuildingNumber = Integer.parseInt(st.nextToken()); + int leftNumber = Integer.parseInt(st.nextToken()); + int rightNumber = Integer.parseInt(st.nextToken()); int price = Integer.parseInt(st.nextToken()); totalPrice += price; - Road road = new Road(firstBuildingNumber, secondBuildingNumber, price); + Road road = new Road(leftNumber, rightNumber, price); roads.add(road); } roads.sort(Comparator.naturalOrder()); - Map buildingMap = new HashMap<>(); + parent = new int[buildingCount + 1]; + for (int i = 0; i < parent.length; i++) { + parent[i] = i; + } + int selectedRoadCounts = 0; + for (Road road : roads) { if (selectedRoadCounts == buildingCount - 1) break; - int smallerBuildingNumber = road.getSmallerBuildingNumber(); - int biggerBuildingNumber = road.getBiggerBuildingNumber(); - - Building firstBuilding = buildingMap.getOrDefault(smallerBuildingNumber, new Building(smallerBuildingNumber)); - Building secondBuilding = buildingMap.getOrDefault(biggerBuildingNumber, new Building(biggerBuildingNumber)); + int leftBuildingNumber = road.getLeftNumber(); + int rightBuildingNumber = road.getRightNumber(); - int firstBuildingParentId = getParentBuildingOf(firstBuilding).getId(); - int secondBuildingParentId = getParentBuildingOf(secondBuilding).getId(); + int firstBuildingParentId = findParent(leftBuildingNumber); + int secondBuildingParentId = findParent(rightBuildingNumber); if (firstBuildingParentId != secondBuildingParentId) { - /** - * TODO: 22. 6. 19. building 번호가 낮은 것을 기준으로 부모로 설정하니 동기화시 문제가 됨 - * - * 7 6 - * 1 2 1 - * 3 4 1 - * 5 6 1 - * 1 7 2 - * 7 3 3 - * 5 7 3 - * - * 7 - * / | \ - * 1 3 5 - * | | | - * 2 4 6 - * - * 1, 3, 5 가 각각이 자신이 최상위 부모로 인지함. 동기화 필요 - */ - - syncParent(firstBuilding, secondBuilding); - buildingMap.put(smallerBuildingNumber, firstBuilding); - buildingMap.put(biggerBuildingNumber, secondBuilding); + syncParentsAsSmallerNumber(leftBuildingNumber, rightBuildingNumber); minPrice += road.getPrice(); selectedRoadCounts++; } } boolean hasFoundAnswer = true; - List buildings = new ArrayList<>(buildingMap.values()); - for (int i = 1; i < buildings.size(); i++) { - Building firstParent = updateParentBuildingOf(buildings.get(i - 1)); - Building secondParent = updateParentBuildingOf(buildings.get(i)); - if (firstParent.getId() != secondParent.getId()) { + for (int i = 1; i < buildingCount; i++) { + int firstParent = findParent(i); + int secondParent = findParent(i + 1); + if (firstParent != secondParent) { hasFoundAnswer = false; break; } @@ -94,54 +78,44 @@ public void run() { } } - private Building updateParentBuildingOf(Building building) { - if (building.hasParent()) { - Building parentBuilding = getParentBuildingOf(building.getParentBuilding()); - building.setParentBuilding(parentBuilding); - return parentBuilding; + int findParent(int buildingNumber) { + if (parent[buildingNumber] != buildingNumber) { + parent[buildingNumber] = findParent(parent[buildingNumber]); } - return building.getParentBuilding(); + return parent[buildingNumber]; } - - private Building getParentBuildingOf(Building building) { - if (building.hasParent()) { - return getParentBuildingOf(building.getParentBuilding()); - } - return building.getParentBuilding(); - } - - private void syncParent(Building firstBuilding, Building secondBuilding) { - Building firstBuildingParent = getParentBuildingOf(firstBuilding); - Building secondBuildingParent = getParentBuildingOf(secondBuilding); - if (firstBuildingParent.getId() < secondBuildingParent.getId()) { - firstBuilding.setParentBuilding(secondBuildingParent); + void syncParentsAsSmallerNumber(int firstBuildingNumber, int secondBuildingNumber) { + int firstParent = findParent(firstBuildingNumber); + int secondParent = findParent(secondBuildingNumber); + if (firstParent < secondParent) { + parent[secondParent] = firstParent; } else { - secondBuilding.setParentBuilding(firstBuildingParent); + parent[firstParent] = secondParent; } } } class Road implements Comparable { - private final int smallerBuildingNumber; - private final int biggerBuildingNumber; + private final int leftNumber; + private final int rightNumber; private final int price; - public int getSmallerBuildingNumber() { - return smallerBuildingNumber; + public int getLeftNumber() { + return leftNumber; } - public int getBiggerBuildingNumber() { - return biggerBuildingNumber; + public int getRightNumber() { + return rightNumber; } public int getPrice() { return price; } - public Road(int firstBuildingNumber, int secondBuildingNumber, int price) { - this.smallerBuildingNumber = Math.min(firstBuildingNumber, secondBuildingNumber); - this.biggerBuildingNumber = Math.max(firstBuildingNumber, secondBuildingNumber); + public Road(int leftBuildingNumber, int rightBuildingNumber, int price) { + this.leftNumber = leftBuildingNumber; + this.rightNumber = rightBuildingNumber; this.price = price; } @@ -149,30 +123,4 @@ public Road(int firstBuildingNumber, int secondBuildingNumber, int price) { public int compareTo(Road o) { // ascending sort 오름차순 return price - o.price; } -} - - -class Building { - private final int id; - private Building parentBuilding = this; - - public Building(int id) { - this.id = id; - } - - public Building getParentBuilding() { - return parentBuilding; - } - - public int getId() { - return id; - } - - boolean hasParent() { - return parentBuilding.getId() != id; - } - - public void setParentBuilding(Building building) { - parentBuilding = building; - } } \ No newline at end of file From 61ab5ecf35a2ab6617c47a2cea1fdba2750b5a93 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sat, 25 Jun 2022 18:13:17 +0900 Subject: [PATCH 060/111] =?UTF-8?q?[Baekjoon]=2010775:=20=EA=B3=B5?= =?UTF-8?q?=ED=95=AD=20-=20=EC=8B=9C=EA=B0=84=EC=B4=88=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Boj_10775_\352\263\265\355\225\255.java" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" diff --git "a/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" "b/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" new file mode 100644 index 0000000..bde26a0 --- /dev/null +++ "b/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" @@ -0,0 +1,46 @@ +package Week_9; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class Boj_10775_공항 { + + int[] highestNumberGates; + boolean[] isChecked; + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int numberOfGates = Integer.parseInt(br.readLine()); + int numberOfAirplane = Integer.parseInt(br.readLine()); + highestNumberGates = new int[numberOfGates + 1]; + isChecked = new boolean[numberOfGates + 1]; + for (int i = 0; i < highestNumberGates.length; i++) { + highestNumberGates[i] = i; + } + int answer = 0; + while (numberOfAirplane-- > 0) { + int airplaneDockableMaxGateNumber = Integer.parseInt(br.readLine()); + int highestGateNumber = findHighestNumberGate(airplaneDockableMaxGateNumber); + if (0 < highestGateNumber && highestGateNumber <= airplaneDockableMaxGateNumber) { + isChecked[highestGateNumber] = true; + answer++; + } else { + break; + } + } + System.out.println(answer); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private int findHighestNumberGate(int gateNumber) { + if (gateNumber == highestNumberGates[gateNumber] && !isChecked[gateNumber]) { + return gateNumber; + } else { + return highestNumberGates[gateNumber] = findHighestNumberGate(gateNumber - 1); + } + } + +} From 03bf5069eaad36baa8e218ea7ffe3cd8288743db Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sat, 25 Jun 2022 18:29:38 +0900 Subject: [PATCH 061/111] =?UTF-8?q?[Baekjoon]=2010775:=20=EA=B3=B5?= =?UTF-8?q?=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Boj_10775_\352\263\265\355\225\255.java" | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git "a/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" "b/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" index bde26a0..03d1c3b 100644 --- "a/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" +++ "b/sunguk/src/Week_9/Boj_10775_\352\263\265\355\225\255.java" @@ -2,28 +2,26 @@ import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.TreeSet; public class Boj_10775_공항 { - int[] highestNumberGates; - boolean[] isChecked; + TreeSet possibleGates = new TreeSet<>(); public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numberOfGates = Integer.parseInt(br.readLine()); int numberOfAirplane = Integer.parseInt(br.readLine()); - highestNumberGates = new int[numberOfGates + 1]; - isChecked = new boolean[numberOfGates + 1]; - for (int i = 0; i < highestNumberGates.length; i++) { - highestNumberGates[i] = i; + for (int i = 0; i < numberOfGates; i++) { + possibleGates.add(i + 1); } int answer = 0; while (numberOfAirplane-- > 0) { int airplaneDockableMaxGateNumber = Integer.parseInt(br.readLine()); - int highestGateNumber = findHighestNumberGate(airplaneDockableMaxGateNumber); - if (0 < highestGateNumber && highestGateNumber <= airplaneDockableMaxGateNumber) { - isChecked[highestGateNumber] = true; + Integer possibleMaxGateNumber = possibleGates.floor(airplaneDockableMaxGateNumber); + if (possibleMaxGateNumber != null) { + possibleGates.remove(possibleMaxGateNumber); answer++; } else { break; @@ -34,13 +32,4 @@ public void run() { e.printStackTrace(); } } - - private int findHighestNumberGate(int gateNumber) { - if (gateNumber == highestNumberGates[gateNumber] && !isChecked[gateNumber]) { - return gateNumber; - } else { - return highestNumberGates[gateNumber] = findHighestNumberGate(gateNumber - 1); - } - } - -} +} \ No newline at end of file From 69e6097b04b847a4b58c977331fbea749c0bc8f9 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 26 Jun 2022 00:13:32 +0900 Subject: [PATCH 062/111] =?UTF-8?q?[Baekjoon]=203691:=20=EC=BB=B4=ED=93=A8?= =?UTF-8?q?=ED=84=B0=20=EC=A1=B0=EB=A6=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...355\204\260_\354\241\260\353\246\275.java" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "sunguk/src/Week_9/Boj_3691_\354\273\264\355\223\250\355\204\260_\354\241\260\353\246\275.java" diff --git "a/sunguk/src/Week_9/Boj_3691_\354\273\264\355\223\250\355\204\260_\354\241\260\353\246\275.java" "b/sunguk/src/Week_9/Boj_3691_\354\273\264\355\223\250\355\204\260_\354\241\260\353\246\275.java" new file mode 100644 index 0000000..368e8a6 --- /dev/null +++ "b/sunguk/src/Week_9/Boj_3691_\354\273\264\355\223\250\355\204\260_\354\241\260\353\246\275.java" @@ -0,0 +1,102 @@ +package Week_9; + + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +/** + * @link https://www.acmicpc.net/problem/3691 + */ +public class Boj_3691_컴퓨터_조립 { + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int caseCount = Integer.parseInt(br.readLine()); + + while (caseCount-- > 0) { + String line = br.readLine(); + StringTokenizer st = new StringTokenizer(line, " "); + Map partsMap = new HashMap<>(); // key: type, value: parts of type + int numberOfParts = Integer.parseInt(st.nextToken()); + int budget = Integer.parseInt(st.nextToken()); + + while (numberOfParts-- > 0) { + st = new StringTokenizer(br.readLine(), " "); + String type = st.nextToken(); + st.nextToken(); + + int costOfThePart = Integer.parseInt(st.nextToken()); + int qualityOfThePart = Integer.parseInt(st.nextToken()); + PriorityQueue costAscendingPriorityQueue = partsMap.getOrDefault(type, new PriorityQueue(Comparator.comparingInt(Part::getCost))); + costAscendingPriorityQueue.offer(new Part(type, costOfThePart, qualityOfThePart)); + + partsMap.put(type, costAscendingPriorityQueue); + } + + long totalCost = 0L; + PriorityQueue qualityAscendingPriorityQueue = new PriorityQueue<>((o1, o2) -> o1.getQuality() - o2.getQuality()); + for (PriorityQueue pq : partsMap.values()) { // fill with all types of parts + Part cheapPartOfType = pq.poll(); + totalCost += cheapPartOfType.getCost(); + qualityAscendingPriorityQueue.offer(cheapPartOfType); + } + + List collectedQualities = new ArrayList<>(); // it's for figuring it out what the lowest quality of the collected parts is + while (!qualityAscendingPriorityQueue.isEmpty()) { + Part currentLowestQualityPart = qualityAscendingPriorityQueue.poll(); + String type = currentLowestQualityPart.getType(); + if (partsMap.get(type).isEmpty()) { // empty means there is any part of this type + collectedQualities.add(currentLowestQualityPart.getQuality()); // no option to choose + continue; + } + + Part nextCheapPartOfTheType = (Part) partsMap.get(type).poll(); // ready to search better part of this type + if (nextCheapPartOfTheType.getQuality() <= currentLowestQualityPart.getQuality()) { + qualityAscendingPriorityQueue.offer(currentLowestQualityPart); + continue; + } + + int costOfNextPart = nextCheapPartOfTheType.getCost(); + int costOfCurrentPart = currentLowestQualityPart.getCost(); + if (totalCost - costOfCurrentPart + costOfNextPart <= budget) { + totalCost = totalCost - costOfCurrentPart + costOfNextPart; + qualityAscendingPriorityQueue.offer(nextCheapPartOfTheType); // keep next part + } else { + qualityAscendingPriorityQueue.offer(currentLowestQualityPart); // keep current part + } + } + collectedQualities.sort(Comparator.naturalOrder()); + System.out.println(collectedQualities.get(0)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} + +class Part { + private String type; + private int cost; + private int quality; + + Part(String type, int cost, int quality) { + this.type = type; + this.cost = cost; + this.quality = quality; + } + + public String getType() { + return type; + } + + public int getCost() { + return cost; + } + + public int getQuality() { + return quality; + } +} \ No newline at end of file From c540491fa0fcf17277ab94cc35d17b0e2c4c3c03 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 26 Jun 2022 00:32:18 +0900 Subject: [PATCH 063/111] =?UTF-8?q?[Baekjoon]=2016719:=20ZOAC=20-=203%?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=ED=8B=80=EB=A6=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sunguk/src/Week_9/Boj_16719_ZOAC.java | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 sunguk/src/Week_9/Boj_16719_ZOAC.java diff --git a/sunguk/src/Week_9/Boj_16719_ZOAC.java b/sunguk/src/Week_9/Boj_16719_ZOAC.java new file mode 100644 index 0000000..9cb9f16 --- /dev/null +++ b/sunguk/src/Week_9/Boj_16719_ZOAC.java @@ -0,0 +1,85 @@ +package Week_9; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.PriorityQueue; + + +/** + * https://www.acmicpc.net/problem/16719 + */ +public class Boj_16719_ZOAC { + + ArrayList characters = new ArrayList<>(); + StringBuilder answer = new StringBuilder(); // 최종 답 출력을 위한 StringBuilder + StringBuilder lastOutputValue = new StringBuilder(); // 최근 값을 저장하는 StringBuilder + boolean[] isChecked; + ArrayList outputArray = new ArrayList(); // 새로운 값이 들어오면 원본순서로 정렬하기 위한 ArrayList + + public void run() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String inputString = br.readLine(); + for (int i = 0; i < inputString.length(); i++) { + characters.add(new PriorityCharacter(inputString.charAt(i), i)); + } + isChecked = new boolean[inputString.length()]; + findLowestAsciiFromCurrentPosition(0); + if (answer.length() > 0) { + System.out.println(answer.delete(answer.length() - 1, answer.length()).toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void findLowestAsciiFromCurrentPosition(int index) { + PriorityQueue pqFromIndex = new PriorityQueue<>((o1, o2) -> { + return o1.getAsciiCodeValue() - o2.getAsciiCodeValue(); + }); + for (int i = index; i < characters.size(); i++) { + pqFromIndex.offer(characters.get(i)); + } + while (!pqFromIndex.isEmpty()) { + PriorityCharacter lowestAscii = pqFromIndex.poll(); + int originalPosition = lowestAscii.getOriginalPriority(); + if (isChecked[originalPosition] == false) { + isChecked[originalPosition] = true; + storeCharacter(originalPosition); + findLowestAsciiFromCurrentPosition(originalPosition + 1); + } + } + } + + private void storeCharacter(int index) { + outputArray.add(characters.get(index)); + outputArray.sort((o1, o2) -> { + return o1.getOriginalPriority() - o2.getOriginalPriority(); + }); + lastOutputValue.delete(0, lastOutputValue.length()); + for (PriorityCharacter priorityCharacter : outputArray) { + lastOutputValue.append((char) priorityCharacter.getAsciiCodeValue()); + } + answer.append(lastOutputValue).append("\n"); + } +} + +class PriorityCharacter { + + private final int asciiCodeValue; + private final int originalPriority; + + public PriorityCharacter(int asciiCodeValue, int originalPriority) { + this.asciiCodeValue = asciiCodeValue; + this.originalPriority = originalPriority; + } + + public int getOriginalPriority() { + return originalPriority; + } + + public int getAsciiCodeValue() { + return asciiCodeValue; + } +} \ No newline at end of file From 5c1c0578de93d9ca068be0dc46c862e84f046790 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 26 Jun 2022 01:11:33 +0900 Subject: [PATCH 064/111] [Baekjoon] 16719: ZOAC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PriorityQueue 쓸 때, 다른 값들도 명시해줘야 정확한 포지션의 값을 가져올 수 있음. --- sunguk/src/Week_9/Boj_16719_ZOAC.java | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/sunguk/src/Week_9/Boj_16719_ZOAC.java b/sunguk/src/Week_9/Boj_16719_ZOAC.java index 9cb9f16..8589560 100644 --- a/sunguk/src/Week_9/Boj_16719_ZOAC.java +++ b/sunguk/src/Week_9/Boj_16719_ZOAC.java @@ -36,14 +36,20 @@ public void run() { private void findLowestAsciiFromCurrentPosition(int index) { PriorityQueue pqFromIndex = new PriorityQueue<>((o1, o2) -> { - return o1.getAsciiCodeValue() - o2.getAsciiCodeValue(); + if(o1.getAsciiCodeValue() == o2.getAsciiCodeValue()){ + return o1.getOriginalPosition() - o2.getOriginalPosition(); + }else { + return o1.getAsciiCodeValue() - o2.getAsciiCodeValue(); + } }); for (int i = index; i < characters.size(); i++) { - pqFromIndex.offer(characters.get(i)); + if (isChecked[i] == false) { + pqFromIndex.offer(characters.get(i)); + } } while (!pqFromIndex.isEmpty()) { PriorityCharacter lowestAscii = pqFromIndex.poll(); - int originalPosition = lowestAscii.getOriginalPriority(); + int originalPosition = lowestAscii.getOriginalPosition(); if (isChecked[originalPosition] == false) { isChecked[originalPosition] = true; storeCharacter(originalPosition); @@ -55,7 +61,7 @@ private void findLowestAsciiFromCurrentPosition(int index) { private void storeCharacter(int index) { outputArray.add(characters.get(index)); outputArray.sort((o1, o2) -> { - return o1.getOriginalPriority() - o2.getOriginalPriority(); + return o1.getOriginalPosition() - o2.getOriginalPosition(); }); lastOutputValue.delete(0, lastOutputValue.length()); for (PriorityCharacter priorityCharacter : outputArray) { @@ -68,15 +74,15 @@ private void storeCharacter(int index) { class PriorityCharacter { private final int asciiCodeValue; - private final int originalPriority; + private final int originalPosition; - public PriorityCharacter(int asciiCodeValue, int originalPriority) { + public PriorityCharacter(int asciiCodeValue, int originalPosition) { this.asciiCodeValue = asciiCodeValue; - this.originalPriority = originalPriority; + this.originalPosition = originalPosition; } - public int getOriginalPriority() { - return originalPriority; + public int getOriginalPosition() { + return originalPosition; } public int getAsciiCodeValue() { From cd7f24cadd18aca881021ad93c65a1b81db50113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 26 Jun 2022 01:56:11 +0900 Subject: [PATCH 065/111] =?UTF-8?q?[Baekjoon]=2010775:=20=EA=B3=B5?= =?UTF-8?q?=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_9/BOJ_10775.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sangryeol/Week_9/BOJ_10775.java diff --git a/sangryeol/Week_9/BOJ_10775.java b/sangryeol/Week_9/BOJ_10775.java new file mode 100644 index 0000000..4e3ca0b --- /dev/null +++ b/sangryeol/Week_9/BOJ_10775.java @@ -0,0 +1,23 @@ +import java.io.*; +import java.util.*; + +public class BOJ_10775 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + TreeSet gates = new TreeSet<>(); + int G = Integer.parseInt(br.readLine()); + int P = Integer.parseInt(br.readLine()); + for (int g = 1; g <= G; g++) { + gates.add(g); + } + int ans = 0; + for (int i = 0; i < P; i++) { + int want = Integer.parseInt(br.readLine()); + Integer found = gates.floor(want); + if (found == null) break; + gates.remove(found); + ans++; + } + System.out.println(ans); + } +} From 59f9a33e89d7375c338179921f4959f544a6bf5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 26 Jun 2022 01:56:50 +0900 Subject: [PATCH 066/111] [Baekjoon] 16719: ZOAC --- sangryeol/Week_9/BOJ_16719.java | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 sangryeol/Week_9/BOJ_16719.java diff --git a/sangryeol/Week_9/BOJ_16719.java b/sangryeol/Week_9/BOJ_16719.java new file mode 100644 index 0000000..45078af --- /dev/null +++ b/sangryeol/Week_9/BOJ_16719.java @@ -0,0 +1,42 @@ +import java.io.*; +import java.util.*; + +public class BOJ_16719 { + static boolean[] selected; + static ArrayList strings = new ArrayList<>(); + static int getMinIndex(char[] input, int left, int right) { + char minAlphabet = 'Z'; + int index = left; + for (int i = left; i < right; i++) { + if (minAlphabet > input[i]) { + minAlphabet = input[i]; + index = i; + } + } + return index; + } + static void splitString(char[] input, int left, int right) { + if (left >= right) return; + int index = getMinIndex(input, left, right); + selected[index] = true; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < input.length; i++) { + if (selected[i]) { + sb.append(input[i]); + } + } + strings.add(sb.toString()); + splitString(input, index + 1, right); + splitString(input, left, index); + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + char[] input = br.readLine().toCharArray(); + int inputLength = input.length; + selected = new boolean[inputLength]; + splitString(input, 0, inputLength); + for (String output : strings) { + System.out.println(output); + } + } +} From cd68ae39cefb217e3f2878d012d534d9391bc527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sun, 26 Jun 2022 01:57:19 +0900 Subject: [PATCH 067/111] =?UTF-8?q?[Baekjoon]=203691:=20=EC=BB=B4=ED=93=A8?= =?UTF-8?q?=ED=84=B0=20=EC=A1=B0=EB=A6=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sangryeol/Week_9/BOJ_3691.java | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sangryeol/Week_9/BOJ_3691.java diff --git a/sangryeol/Week_9/BOJ_3691.java b/sangryeol/Week_9/BOJ_3691.java new file mode 100644 index 0000000..66f00dd --- /dev/null +++ b/sangryeol/Week_9/BOJ_3691.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + +class Component { + int price, quality; + Component(int price, int quality) { + this.price = price; + this.quality = quality; + } +} + +public class BOJ_3691 { + static int N, B; + static final int INF = (int)1e9 + 10; + static HashMap> componentMap; + static boolean isPossible(int want) { + int sum = 0; + for (ArrayList components : componentMap.values()) { + int price = INF; + for (Component component : components) { + if (component.quality >= want) { + price = Math.min(price, component.price); + } + } + sum += price; + if (sum > B) return false; + } + return true; + } + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int tc = Integer.parseInt(br.readLine()); + while (tc-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + B = Integer.parseInt(st.nextToken()); + componentMap = new HashMap<>(); + for (int i = 0; i < N; i++) { + String type; + int price, quality; + st = new StringTokenizer(br.readLine()); + type = st.nextToken(); st.nextToken(); + price = Integer.parseInt(st.nextToken()); + quality = Integer.parseInt(st.nextToken()); + componentMap.putIfAbsent(type, new ArrayList<>()); + ArrayList components = componentMap.get(type); + components.add(new Component(price, quality)); + } + int lo = 0, hi = INF; + while (lo + 1 < hi) { + int mid = (lo + hi) / 2; + if (isPossible(mid)) lo = mid; + else hi = mid; + } + System.out.println(lo); + } + } +} From 6d6e32e07d190b6ace223102db51eeca952c36a5 Mon Sep 17 00:00:00 2001 From: SunGuk Date: Sun, 26 Jun 2022 02:41:22 +0900 Subject: [PATCH 068/111] [LeetCode] 1855: Maximum_Distance_Between_a_Pair_of_Values --- ...mum_Distance_Between_a_Pair_of_Values.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 sunguk/src/Week_9/Leet_1855_Maximum_Distance_Between_a_Pair_of_Values.java diff --git a/sunguk/src/Week_9/Leet_1855_Maximum_Distance_Between_a_Pair_of_Values.java b/sunguk/src/Week_9/Leet_1855_Maximum_Distance_Between_a_Pair_of_Values.java new file mode 100644 index 0000000..ad99796 --- /dev/null +++ b/sunguk/src/Week_9/Leet_1855_Maximum_Distance_Between_a_Pair_of_Values.java @@ -0,0 +1,33 @@ +package Week_9; + +public class Leet_1855_Maximum_Distance_Between_a_Pair_of_Values { + /** + * find possible distance using binary search + * + * @return 0 if there is not valid pair + */ + public int maxDistance(int[] nums1, int[] nums2) { + int answer = 0; + int min = 1; // searching for distance of 0 is useless + int max = nums2.length; + while (min <= max) { + int mid = (max + min) / 2; + boolean isPossible = false; + for (int i = 0; i < nums1.length; i++) { + if (i + mid < nums2.length) { + if (nums1[i] <= nums2[i + mid]) { + answer = mid; + isPossible = true; + break; + } + } + } + if (isPossible) { + min = mid + 1; + } else { + max = mid - 1; + } + } + return answer; + } +} \ No newline at end of file From 6bbab5fe1c22b5c2c831d36e6bb9664c1d2c3b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 26 Jun 2022 16:41:04 +0900 Subject: [PATCH 069/111] =?UTF-8?q?[Baekjoon]10775:=20=EA=B3=B5=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_9/Boj10775.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 daejik/Week_9/Boj10775.java diff --git a/daejik/Week_9/Boj10775.java b/daejik/Week_9/Boj10775.java new file mode 100644 index 0000000..abf8c58 --- /dev/null +++ b/daejik/Week_9/Boj10775.java @@ -0,0 +1,34 @@ +package Week9; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Boj10775 { + static int[] parent; + static int numGates, numPlanes, ans; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + numGates = Integer.parseInt(br.readLine()); + numPlanes = Integer.parseInt(br.readLine()); + parent = new int[numGates + 1]; + + for(int i=1; i<=numGates; i++) parent[i] = i; + + for(int i=0; i Date: Sun, 26 Jun 2022 16:42:09 +0900 Subject: [PATCH 070/111] [leetcode]1855: maximumDistance --- daejik/Week_9/leetcode1855.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 daejik/Week_9/leetcode1855.java diff --git a/daejik/Week_9/leetcode1855.java b/daejik/Week_9/leetcode1855.java new file mode 100644 index 0000000..be9eebb --- /dev/null +++ b/daejik/Week_9/leetcode1855.java @@ -0,0 +1,24 @@ +package Week9; + + +class Solution{ + + public int maxDistance(int[] nums1, int[] nums2) { + int maxDist = 0; + int nums1Length = nums1.length; + int nums2Length = nums2.length; + + for(int i=0; i maxDist) maxDist = tmpDist; + } + if(maxDist > 0) return maxDist - 1; + return 0; + } +} From 0cf0f36a534cfdc3164a96fbc5351a9e130e8ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 26 Jun 2022 16:43:43 +0900 Subject: [PATCH 071/111] =?UTF-8?q?[Baekjoon]3691:=20=EC=BB=B4=ED=93=A8?= =?UTF-8?q?=ED=84=B0=EC=A1=B0=EB=A6=BD(=EC=8B=A4=ED=8C=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_9/Boj3691.java | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 daejik/Week_9/Boj3691.java diff --git a/daejik/Week_9/Boj3691.java b/daejik/Week_9/Boj3691.java new file mode 100644 index 0000000..a8bff35 --- /dev/null +++ b/daejik/Week_9/Boj3691.java @@ -0,0 +1,93 @@ +package Week9; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.StringTokenizer; + +public class Boj3691 { + static int totalTestCase; + static int numOfIngredients, money; + static int resultOfSimulation; + static HashMap> ingredientsMap = new HashMap<>(); + static ArrayList[] ingredientArrayList; + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + totalTestCase = Integer.parseInt(br.readLine()); + + while(totalTestCase-- > 0) { + StringTokenizer st = new StringTokenizer(br.readLine()); + numOfIngredients = Integer.parseInt(st.nextToken()); + money = Integer.parseInt(st.nextToken()); + + inputIngredientsInfo(br, st); + moveFromMapToList(); + resultOfSimulation = 0; + simulation(0, money, Integer.MAX_VALUE); + System.out.println(resultOfSimulation); + } + } + public static void moveFromMapToList(){ + ingredientArrayList = new ArrayList[ingredientsMap.size()]; + int idx = 0; + for(String type : ingredientsMap.keySet()) { + ingredientArrayList[idx] = ingredientsMap.get(type); + idx++; + } + } + public static void simulation(int idx, int testMoney, int testQuality) { + if(idx >= ingredientArrayList.length) { + if(resultOfSimulation < testQuality) resultOfSimulation = testQuality; + return; + } + + for(int i=0; i < ingredientArrayList[idx].size(); i++) { + if(testMoney >= ingredientArrayList[idx].get(i).price) { + if(testQuality > ingredientArrayList[idx].get(i).quality) + simulation(idx + 1, testMoney - ingredientArrayList[idx].get(i).price, ingredientArrayList[idx].get(i).quality); + else + simulation(idx + 1, testMoney - ingredientArrayList[idx].get(i).price, testQuality); + } + } + } + public static void inputIngredientsInfo(BufferedReader br, StringTokenizer st) throws IOException { + ingredientsMap.clear(); + + while(numOfIngredients-- > 0) { + st = new StringTokenizer(br.readLine()); + + String type = st.nextToken(); + String name = st.nextToken(); + Integer price = Integer.parseInt(st.nextToken()); + Integer quality = Integer.parseInt(st.nextToken()); + + IngredientsClass ingredientsClass = new IngredientsClass(price, quality); + + ArrayList arrayListOfIngredients = ingredientsMap.get(type); + + if(arrayListOfIngredients == null) { + arrayListOfIngredients = new ArrayList<>(); + } + arrayListOfIngredients.add(ingredientsClass); + + ingredientsMap.put(type, arrayListOfIngredients); + } + } +} +class IngredientsClass{ + int price, quality; + + IngredientsClass(int price, int quality){ + this.price = price; + this.quality = quality; + } + + @Override + public String toString() { + return "price:"+this.price+", quality:"+this.quality; + } +} \ No newline at end of file From 04c601353d5ded6269ffe53cf04b18b22c5eba83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 10 Jul 2022 12:07:02 +0900 Subject: [PATCH 072/111] =?UTF-8?q?[Baekjoon]12886:=20=EB=8F=8C=EA=B7=B8?= =?UTF-8?q?=EB=A3=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- daejik/Week_10/Boj12886.java | 112 +++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 daejik/Week_10/Boj12886.java diff --git a/daejik/Week_10/Boj12886.java b/daejik/Week_10/Boj12886.java new file mode 100644 index 0000000..73a4447 --- /dev/null +++ b/daejik/Week_10/Boj12886.java @@ -0,0 +1,112 @@ +package Week10; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Boj12886 { + static Queue queue = new LinkedList(); + static boolean[][] visited = new boolean[3][1501]; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int cntFirstGroup = Integer.parseInt(st.nextToken()); + int cntSecondGroup = Integer.parseInt(st.nextToken()); + int cntThirdGroup = Integer.parseInt(st.nextToken()); + + visited[0][cntFirstGroup] = true; + visited[1][cntSecondGroup] = true; + visited[2][cntThirdGroup] = true; + + queue.add(new Stones(cntFirstGroup, cntSecondGroup, cntThirdGroup)); + + while(!queue.isEmpty()) { + Stones now = queue.poll(); + + if(sameNumAll(now)) { + System.out.println("1"); + return; + } + + + if(now.aGroup != now.bGroup) { + Stones next = now; + + if(now.aGroup > now.bGroup) { + next.aGroup -= next.bGroup; + next.bGroup += next.bGroup; + } else { + next.bGroup -= next.aGroup; + next.aGroup += next.aGroup; + } + + if(isNotNegative(next) == true && isVisited(next) == false) { + queue.add(next); + } + } + + if(now.aGroup != now.cGroup) { + Stones next = now; + + if(now.aGroup > now.cGroup) { + next.aGroup -= next.cGroup; + next.cGroup += next.cGroup; + } else { + next.cGroup -= next.aGroup; + next.aGroup += next.aGroup; + } + + if(isNotNegative(next) == true && isVisited(next) == false) { + queue.add(next); + } + } + + if(now.bGroup != now.cGroup) { + Stones next = now; + + if(now.bGroup > now.cGroup) { + next.bGroup -= next.cGroup; + next.cGroup += next.cGroup; + } else { + next.cGroup -= next.bGroup; + next.bGroup += next.bGroup; + } + + if(isNotNegative(next) == true && isVisited(next) == false) { + queue.add(next); + } + } + } + System.out.println("0"); + + } + public static boolean isNotNegative(Stones stones) { + if(stones.aGroup >= 0 && stones.bGroup >= 0 && stones.cGroup >= 0) return true; + return false; + } + public static boolean sameNumAll(Stones stones) { + if(stones.aGroup == stones.bGroup && stones.aGroup == stones.cGroup && stones.bGroup == stones.cGroup) return true; + return false; + } + public static boolean isVisited(Stones stones) { + if(visited[0][stones.aGroup] == true && visited[1][stones.bGroup] == true && visited[2][stones.cGroup] == true) return true; + visited[0][stones.aGroup] = true; + visited[1][stones.bGroup] = true; + visited[2][stones.cGroup] = true; + return false; + } + +} +class Stones{ + int aGroup, bGroup, cGroup; + + Stones(int aGroup, int bGroup, int cGroup){ + this.aGroup = aGroup; + this.bGroup = bGroup; + this.cGroup = cGroup; + } +} From 06e434782f53dcda38dcb9578dde0a2b848e247c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 10 Jul 2022 12:07:40 +0900 Subject: [PATCH 073/111] [Baekjoon]16929: Two Dots --- daejik/Week_10/Boj16929.java | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 daejik/Week_10/Boj16929.java diff --git a/daejik/Week_10/Boj16929.java b/daejik/Week_10/Boj16929.java new file mode 100644 index 0000000..415c795 --- /dev/null +++ b/daejik/Week_10/Boj16929.java @@ -0,0 +1,67 @@ +package Week10; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Boj16929 { + static char[][] map = new char[50][50]; + static boolean[][] visited = new boolean[50][50]; + static int dr[]= {1, 0, -1, 0}, dc[]= {0, 1, 0, -1}; + static int numRow, numCol; + static boolean flag; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + numRow = Integer.parseInt(st.nextToken()); + numCol = Integer.parseInt(st.nextToken()); + + for(int row = 0; row < numRow; row++) { + map[row] = br.readLine().toCharArray(); + } + + for(int row = 0; row < numRow; row++) { + for(int col = 0; col < numCol; col++) { + + visited[row][col] = true; + dfs(row, col, row, col, 1); + visited[row][col] = false; + + if(flag == true) { + System.out.println("Yes"); + return; + } + } + } + System.out.println("No"); + } + public static void dfs(int nowR, int nowC, int originR, int originC, int cnt) { + if(flag == true) return; + + for(int direction=0; direction<4; direction++) { + int nextR = nowR + dr[direction]; + int nextC = nowC + dc[direction]; + + if(!canNext(nextR, nextC)) continue; + if(visited[nextR][nextC]) { + if(nextR == originR && nextC == originC && cnt >= 3) { + flag = true; + return; + } + } else { + if(map[nextR][nextC] == map[originR][originC]) { + visited[nextR][nextC] = true; + dfs(nextR, nextC, originR, originC, cnt + 1); + visited[nextR][nextC] = false; + } + } + } + } + public static boolean canNext(int r, int c) { + if(r<0 || c<0 || r>=numRow || c>=numCol) return false; + return true; + } +} From 448c3ef172177bba2c2faba2ab06e90a57155e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 10 Jul 2022 12:08:08 +0900 Subject: [PATCH 074/111] [LeetCode]2078: Two Furthest Houses With Different Colors --- daejik/Week_10/Leet2078.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 daejik/Week_10/Leet2078.java diff --git a/daejik/Week_10/Leet2078.java b/daejik/Week_10/Leet2078.java new file mode 100644 index 0000000..35cfc1e --- /dev/null +++ b/daejik/Week_10/Leet2078.java @@ -0,0 +1,5 @@ +package Week10; + +public class Leet2078 { + +} From b7b14b19f7a4983a6cfc4d79b08460bd94a215e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sat, 30 Jul 2022 23:53:53 +0900 Subject: [PATCH 075/111] [LeetCode]797. All Paths From Source to Target --- daejik/Week_11/leetcode797.java | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 daejik/Week_11/leetcode797.java diff --git a/daejik/Week_11/leetcode797.java b/daejik/Week_11/leetcode797.java new file mode 100644 index 0000000..676892a --- /dev/null +++ b/daejik/Week_11/leetcode797.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +class Solution { + List visitInform = new ArrayList(); + List> ans = new ArrayList<>(); + int[][] graphInform; + + public List> allPathsSourceTarget(int[][] graph) { + visitInform.add(0); + graphInform = graph; + dfs(0); + + return ans; + } + + public void dfs(int idx) { + + if(idx == graphInform.length - 1){ + // , + List tmp = new ArrayList<>(); + tmp.addAll(visitInform); + ans.add(tmp); + + return; + } + + if(graphInform[idx].length > 0){ + // 湮 + for(int i=0; i Date: Sat, 30 Jul 2022 23:56:07 +0900 Subject: [PATCH 076/111] [LeetCode]797. All Paths From Source to Target --- daejik/Week_12/leetcode797.java | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 daejik/Week_12/leetcode797.java diff --git a/daejik/Week_12/leetcode797.java b/daejik/Week_12/leetcode797.java new file mode 100644 index 0000000..676892a --- /dev/null +++ b/daejik/Week_12/leetcode797.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +class Solution { + List visitInform = new ArrayList(); + List> ans = new ArrayList<>(); + int[][] graphInform; + + public List> allPathsSourceTarget(int[][] graph) { + visitInform.add(0); + graphInform = graph; + dfs(0); + + return ans; + } + + public void dfs(int idx) { + + if(idx == graphInform.length - 1){ + // , + List tmp = new ArrayList<>(); + tmp.addAll(visitInform); + ans.add(tmp); + + return; + } + + if(graphInform[idx].length > 0){ + // 湮 + for(int i=0; i Date: Sun, 14 Aug 2022 21:17:44 +0900 Subject: [PATCH 077/111] [LeetCode] 739: Daily Temperature --- sangryeol/Week_14/Daily Temperatures.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 sangryeol/Week_14/Daily Temperatures.cpp diff --git a/sangryeol/Week_14/Daily Temperatures.cpp b/sangryeol/Week_14/Daily Temperatures.cpp new file mode 100644 index 0000000..9f16d86 --- /dev/null +++ b/sangryeol/Week_14/Daily Temperatures.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + int size = temperatures.size(); + vector answer(size, 0); + stack stk; + vector& T = temperatures; + for (int i = size - 1; i >= 0; i--) { + while (!stk.empty() && T[stk.top()] <= T[i]) stk.pop(); + if (!stk.empty()) answer[i] = stk.top() - i; + stk.push(i); + } + return answer; + } +}; \ No newline at end of file From f8d6786a382f04379e50582b0065235641122c04 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 14 Aug 2022 21:17:51 +0900 Subject: [PATCH 078/111] [LeetCode] 141: Linked List Cycle --- sangryeol/Week_14/Linked List Cycle.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 sangryeol/Week_14/Linked List Cycle.cpp diff --git a/sangryeol/Week_14/Linked List Cycle.cpp b/sangryeol/Week_14/Linked List Cycle.cpp new file mode 100644 index 0000000..df5f497 --- /dev/null +++ b/sangryeol/Week_14/Linked List Cycle.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + bool hasCycle(ListNode *head) { + ListNode* pCurr = head; + ListNode* pNext = head; + while (pNext && pNext->next) { + pCurr = pCurr->next; + pNext = pNext->next->next; + if (pCurr == pNext) return true; + } + return false; + } +}; \ No newline at end of file From 415a9606d26c11c02210d82e80e412d159f711e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sat, 20 Aug 2022 20:50:45 +0900 Subject: [PATCH 079/111] [LeetCode] 2295: Replace Elements in an Array --- daejik/Week_15/leetcode2295.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 daejik/Week_15/leetcode2295.java diff --git a/daejik/Week_15/leetcode2295.java b/daejik/Week_15/leetcode2295.java new file mode 100644 index 0000000..5aca8a8 --- /dev/null +++ b/daejik/Week_15/leetcode2295.java @@ -0,0 +1,21 @@ +class Solution { + int[] indexArray = new int[1000001]; + + public int[] arrayChange(int[] nums, int[][] operations) { + for(int i=0; i Date: Sat, 20 Aug 2022 20:51:18 +0900 Subject: [PATCH 080/111] [LeetCode] 189: Rotate Array --- daejik/Week_15/leetcode189.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 daejik/Week_15/leetcode189.java diff --git a/daejik/Week_15/leetcode189.java b/daejik/Week_15/leetcode189.java new file mode 100644 index 0000000..ca94d22 --- /dev/null +++ b/daejik/Week_15/leetcode189.java @@ -0,0 +1,18 @@ +class Solution { + int[] originArray; + + public void rotate(int[] nums, int k) { + k %= nums.length; //numsw.lengthŭ 1 ⷯ resize + originArray = nums.clone(); + int fromIndex = 0; + int toIndex = k; + + while(fromIndex < nums.length){ + nums[toIndex++] = originArray[fromIndex++]; + + if(toIndex >= nums.length){ + toIndex = 0; + } + } + } +} \ No newline at end of file From ed86f809f0b2dc53bb42813ec4493090449e7ac9 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 21 Aug 2022 10:23:29 +0900 Subject: [PATCH 081/111] [LeetCode] 189: Rotate Array --- sangryeol/Week_15/Rotate Array.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 sangryeol/Week_15/Rotate Array.cpp diff --git a/sangryeol/Week_15/Rotate Array.cpp b/sangryeol/Week_15/Rotate Array.cpp new file mode 100644 index 0000000..5ca1be4 --- /dev/null +++ b/sangryeol/Week_15/Rotate Array.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + void rotate(vector& nums, int k) { + vector copyNums = nums; + int N = nums.size(); + for (int i = 0; i < N; i++) { + nums[(i + k) % N] = copyNums[i]; + } + return; + } +}; \ No newline at end of file From 46e54dc28e489ed1fc8c780373cdd0859833f3ad Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 21 Aug 2022 10:23:35 +0900 Subject: [PATCH 082/111] [LeetCode] 2295: Replace Elements in an Array --- .../Week_15/Replace Elements in an Array.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sangryeol/Week_15/Replace Elements in an Array.cpp diff --git a/sangryeol/Week_15/Replace Elements in an Array.cpp b/sangryeol/Week_15/Replace Elements in an Array.cpp new file mode 100644 index 0000000..864112c --- /dev/null +++ b/sangryeol/Week_15/Replace Elements in an Array.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + vector arrayChange(vector& nums, vector>& operations) { + const int SZ = 1e6 + 10; + vector result = nums; + int mapping[SZ]{}; + for (int i = 0; i < nums.size(); i++) { + mapping[nums[i]] = i; + } + for (auto operation : operations) { + int oldValue = operation[0]; + int newValue = operation[1]; + result[mapping[oldValue]] = newValue; + mapping[newValue] = mapping[oldValue]; + } + return result; + } +}; \ No newline at end of file From 77aa736093bdf4a8d0fb3d591d1b4e8c75d9c3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:11:00 +0900 Subject: [PATCH 083/111] [leetcode]826: most-profit-assigning-work --- daejik/Week_16/leetcode826_dp.java | 18 ++++++++++++++++++ daejik/Week_16/leetcode826_sort.java | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 daejik/Week_16/leetcode826_dp.java create mode 100644 daejik/Week_16/leetcode826_sort.java diff --git a/daejik/Week_16/leetcode826_dp.java b/daejik/Week_16/leetcode826_dp.java new file mode 100644 index 0000000..9185fd4 --- /dev/null +++ b/daejik/Week_16/leetcode826_dp.java @@ -0,0 +1,18 @@ +class Solution { + public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { + int[] dp = new int[100001]; + for (int i = 0; i < difficulty.length; i++) { + dp[difficulty[i]] = Math.max(dp[difficulty[i]], profit[i]); + } + for (int i = 0; i < dp.length; i++) { + if (i > 0) { + dp[i] = Math.max(dp[i - 1], dp[i]); + } + } + int sum = 0; + for (int i : worker) { + sum += dp[i]; + } + return sum; + } +} \ No newline at end of file diff --git a/daejik/Week_16/leetcode826_sort.java b/daejik/Week_16/leetcode826_sort.java new file mode 100644 index 0000000..ff3ca19 --- /dev/null +++ b/daejik/Week_16/leetcode826_sort.java @@ -0,0 +1,17 @@ +class Solution { + + public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { + List> jobs = new ArrayList<>(); + int N = profit.length, res = 0, i = 0, best = 0; + for (int j = 0; j < N; ++j) + jobs.add(new Pair(difficulty[j], profit[j])); + Collections.sort(jobs, Comparator.comparing(Pair::getKey)); + Arrays.sort(worker); + for (int ability : worker) { + while (i < N && ability >= jobs.get(i).getKey()) + best = Math.max(jobs.get(i++).getValue(), best); + res += best; + } + return res; + } +} \ No newline at end of file From b260a2e61ef9218eebd675c1495de0b72892a296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 4 Sep 2022 10:48:18 +0900 Subject: [PATCH 084/111] [leetcode]349: Intersection of Two Arrays --- daejik/Week_17/leetcode349.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 daejik/Week_17/leetcode349.java diff --git a/daejik/Week_17/leetcode349.java b/daejik/Week_17/leetcode349.java new file mode 100644 index 0000000..c9f6e68 --- /dev/null +++ b/daejik/Week_17/leetcode349.java @@ -0,0 +1,33 @@ +class Solution { + + HashSet set = new HashSet(); + + public int[] intersection(int[] nums1, int[] nums2) { + if(nums1.length < nums2.length){ + for(int i=0; i Date: Sun, 4 Sep 2022 10:48:58 +0900 Subject: [PATCH 085/111] [leetcode]2008: Maximum Earnings From Taxi --- daejik/Week_17/leetcode2008.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 daejik/Week_17/leetcode2008.java diff --git a/daejik/Week_17/leetcode2008.java b/daejik/Week_17/leetcode2008.java new file mode 100644 index 0000000..1517293 --- /dev/null +++ b/daejik/Week_17/leetcode2008.java @@ -0,0 +1,24 @@ +class Solution { + + + public long maxTaxiEarnings(int n, int[][] A) { + long[] dp = new long[n + 1]; + Arrays.sort(A, (a, b) -> a[0] - b[0]); + int j = 0; + + for(int i = 1; i <= n; i++) { + dp[i] = Math.max(dp[i], dp[i - 1]); + + while (j < A.length && A[j][0] == i) { + int startPoint = A[j][0]; + int endPoint = A[j][1]; + int tip = A[j][2]; + + + dp[endPoint] = Math.max(dp[endPoint], dp[i] + (endPoint - startPoint + tip)); + j++; + } + } + return dp[n]; + } +} \ No newline at end of file From 6cac5ac9ade99fd1e19fc4685aeb5422d5ce6cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sat, 10 Sep 2022 19:51:05 +0900 Subject: [PATCH 086/111] [LeetCode] 611: Valid Triangle Number --- sangryeol/Week_18/Valid Triangle Number.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 sangryeol/Week_18/Valid Triangle Number.cpp diff --git a/sangryeol/Week_18/Valid Triangle Number.cpp b/sangryeol/Week_18/Valid Triangle Number.cpp new file mode 100644 index 0000000..4e91149 --- /dev/null +++ b/sangryeol/Week_18/Valid Triangle Number.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int triangleNumber(vector& nums) { + int N = nums.size(); + sort(nums.begin() , nums.end()); + int ans = 0; + for(int i = N - 1; i > 0; i--) { + for (int j = i - 1; j > 0; j--) { + int diff = nums[i] - nums[j]; + int idx = upper_bound(nums.begin(), nums.end(), diff) - nums.begin(); + ans += max(0, j - idx); + } + } + return ans; + } +}; \ No newline at end of file From 022ca9fed38a95de268ef839c0bf23ace0e20ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=EC=83=81=EB=A0=AC?= Date: Sat, 10 Sep 2022 19:52:19 +0900 Subject: [PATCH 087/111] [LeetCode] 2130: Maximum Twin Sum of A Linked List --- .../Maximum Twin Sum of A Linked List.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 sangryeol/Week_18/Maximum Twin Sum of A Linked List.cpp diff --git a/sangryeol/Week_18/Maximum Twin Sum of A Linked List.cpp b/sangryeol/Week_18/Maximum Twin Sum of A Linked List.cpp new file mode 100644 index 0000000..c3d772d --- /dev/null +++ b/sangryeol/Week_18/Maximum Twin Sum of A Linked List.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int pairSum(ListNode* head) { + vector v; + while (head != nullptr) { + v.push_back(head->val); + head = head->next; + } + int N = v.size(); + int ans = 0; + for (int i = 0; i < N; i++) { + int last = v[N - 1 - i]; + ans = max(ans, v[i] + last); + } + return ans; + } +}; \ No newline at end of file From 4cf1b57872c31c6358589656ef3cce5d4c21b821 Mon Sep 17 00:00:00 2001 From: Heize Date: Sun, 18 Sep 2022 10:14:55 +0900 Subject: [PATCH 088/111] [LeetCode] 77: Combinations --- sangryeol/Week_19/Combinations.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 sangryeol/Week_19/Combinations.cpp diff --git a/sangryeol/Week_19/Combinations.cpp b/sangryeol/Week_19/Combinations.cpp new file mode 100644 index 0000000..0ccb9ae --- /dev/null +++ b/sangryeol/Week_19/Combinations.cpp @@ -0,0 +1,24 @@ +class Solution { + int N; + int count; + vector v; + vector> combinations; +public: + void makeCombination(int start, int depth) { + if (depth == count) { + combinations.push_back(v); + return; + } + for (int i = start; i <= N; i++) { + v.push_back(i); + makeCombination(i + 1, depth + 1); + v.pop_back(); + } + } + vector> combine(int n, int k) { + N = n; + count = k; + makeCombination(1, 0); + return combinations; + } +}; \ No newline at end of file From b73214befba0e1533dd11050e05dac18c927e6c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 18 Sep 2022 10:48:10 +0900 Subject: [PATCH 089/111] [leetcode] 77: Combinations --- daejik/Week_19/leetcode77.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 daejik/Week_19/leetcode77.java diff --git a/daejik/Week_19/leetcode77.java b/daejik/Week_19/leetcode77.java new file mode 100644 index 0000000..9d589dc --- /dev/null +++ b/daejik/Week_19/leetcode77.java @@ -0,0 +1,26 @@ +class Solution { + List> ans; + List tmp = new ArrayList<>(); + + public List> combine(int n, int k) { + ans = new ArrayList<>(); + + dfs(1, n, 0, k); + + return ans; + } + + public void dfs(int pickedIdx, int limitIdx, int pickedNum, int limitNum){ + + if(pickedNum == limitNum){ + ans.add(new ArrayList<>(tmp)); + return; + } + + for(int i=pickedIdx; i<=limitIdx; i++){ + tmp.add(i); + dfs(i + 1, limitIdx, pickedNum + 1, limitNum); + tmp.remove(tmp.size() - 1); + } + } +} \ No newline at end of file From 0c5469138a1d10c86e7f7de98c2678eaf054fff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 25 Sep 2022 10:36:13 +0900 Subject: [PATCH 090/111] [leetcode]40: Combination Sum2 --- daejik/Week_20/leetcode40.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 daejik/Week_20/leetcode40.java diff --git a/daejik/Week_20/leetcode40.java b/daejik/Week_20/leetcode40.java new file mode 100644 index 0000000..e71e4ae --- /dev/null +++ b/daejik/Week_20/leetcode40.java @@ -0,0 +1,30 @@ +class Solution { + + List> ans = new ArrayList<>(); + List tmpMemory = new ArrayList<>(); + + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort(candidates); + + dfs(0, 0, candidates, target); + + return ans; + } + + public void dfs(int idx, int sum, int[] candidates, int target){ + + if(sum == target){ + + ans.add(new ArrayList<>(tmpMemory)); + return; + } + + for(int i=idx; i target) continue; + if(i > idx && candidates[i] == candidates[i-1]) continue; + tmpMemory.add(candidates[i]); + dfs(i + 1, sum + candidates[i], candidates, target); + tmpMemory.remove(tmpMemory.size() - 1); + } + } +} \ No newline at end of file From 81195813f93c718ed835fed0ed15177207bc3a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 25 Sep 2022 10:36:41 +0900 Subject: [PATCH 091/111] [lettcode]890: Find and Replace Pattern --- daejik/Week_20/leetcode890.java | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 daejik/Week_20/leetcode890.java diff --git a/daejik/Week_20/leetcode890.java b/daejik/Week_20/leetcode890.java new file mode 100644 index 0000000..8e36649 --- /dev/null +++ b/daejik/Week_20/leetcode890.java @@ -0,0 +1,35 @@ +class Solution { + public List findAndReplacePattern(String[] words, String pattern) { + List answer = new ArrayList<>(); + + for(String str : words){ + + HashMap mapFromPattern = new HashMap<>(); + HashMap mapFromWords = new HashMap<>(); + boolean flag = true; + + for(int i=0; i Date: Fri, 30 Sep 2022 22:37:20 +0900 Subject: [PATCH 092/111] [leetcode]1662: Check If Two String Arrays are Equivalent --- daejik/Week_21/leetcode1662.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 daejik/Week_21/leetcode1662.java diff --git a/daejik/Week_21/leetcode1662.java b/daejik/Week_21/leetcode1662.java new file mode 100644 index 0000000..c699038 --- /dev/null +++ b/daejik/Week_21/leetcode1662.java @@ -0,0 +1,14 @@ +class Solution { + public boolean arrayStringsAreEqual(String[] word1, String[] word2) { + String word1Sum = ""; + String word2Sum = ""; + + for(int i=0; i Date: Fri, 30 Sep 2022 22:37:45 +0900 Subject: [PATCH 093/111] =?UTF-8?q?[leetcode]1546:=20Maximum=20Number=20of?= =?UTF-8?q?=20Non-Overlapping=20Subarrays=20With=20Sum=20Equals=20Target(?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=EC=B4=88=EA=B3=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\260\204\354\264\210\352\263\274.java" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "daejik/Week_21/leetcode1546_\354\213\234\352\260\204\354\264\210\352\263\274.java" diff --git "a/daejik/Week_21/leetcode1546_\354\213\234\352\260\204\354\264\210\352\263\274.java" "b/daejik/Week_21/leetcode1546_\354\213\234\352\260\204\354\264\210\352\263\274.java" new file mode 100644 index 0000000..c29ce17 --- /dev/null +++ "b/daejik/Week_21/leetcode1546_\354\213\234\352\260\204\354\264\210\352\263\274.java" @@ -0,0 +1,34 @@ +class Solution { + int ans = 0; + int targetMemo = 0; + + public int maxNonOverlapping(int[] nums, int target) { + targetMemo = target; + + dfs(nums, 0, 0, 0); + + + + return ans; + } + + public void dfs(int[] nums, int idx, int sum, int tmpAns){ + + if(idx > 0 && sum == targetMemo){ + tmpAns++; + + if(tmpAns > ans) ans = tmpAns; + + sum = 0; + } + + if(idx >= nums.length) return; + + + dfs(nums, idx + 1, sum + nums[idx], tmpAns); + + sum = 0; + dfs(nums, idx + 1, sum, tmpAns); + + } +} \ No newline at end of file From cd0bd7784d5eb1c0382eab92f070ccecafa2d8c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 16 Oct 2022 02:47:56 +0900 Subject: [PATCH 094/111] [leetcode] 473: Matchsticks to Square --- daejik/Week_22/leetcode473.java | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 daejik/Week_22/leetcode473.java diff --git a/daejik/Week_22/leetcode473.java b/daejik/Week_22/leetcode473.java new file mode 100644 index 0000000..4dcea8c --- /dev/null +++ b/daejik/Week_22/leetcode473.java @@ -0,0 +1,37 @@ +import java.util.*; + +class Solution { + int[] sum = new int[5]; + boolean flag = false; + int lengthSide = 0; + + public boolean makesquare(int[] matchsticks) { + if(matchsticks == null || matchsticks.length < 4) return false; + + for(int i=0; i Date: Sun, 16 Oct 2022 02:48:39 +0900 Subject: [PATCH 095/111] [LeetCode] 2317: Maximum XOR After Operations --- daejik/Week_22/leetcode2317.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 daejik/Week_22/leetcode2317.java diff --git a/daejik/Week_22/leetcode2317.java b/daejik/Week_22/leetcode2317.java new file mode 100644 index 0000000..54660de --- /dev/null +++ b/daejik/Week_22/leetcode2317.java @@ -0,0 +1,6 @@ + public int maximumXOR(int[] nums) { + int res = 0; + for (int a: nums) + res |= a; + return res; + } \ No newline at end of file From 5eaa98dff5f58e26414f655520c4a2b760179053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 4 Dec 2022 10:33:27 +0900 Subject: [PATCH 096/111] [leetcode]1404: Number of Steps to Reduce a Number in Binary Representation to One --- daejik/Week_25/leetcode1404.java | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 daejik/Week_25/leetcode1404.java diff --git a/daejik/Week_25/leetcode1404.java b/daejik/Week_25/leetcode1404.java new file mode 100644 index 0000000..eb1bdf7 --- /dev/null +++ b/daejik/Week_25/leetcode1404.java @@ -0,0 +1,34 @@ +class Solution { + public int numSteps(String s) { + int ans = 0; + + while(s.length() > 1){ + + if(s.charAt(s.length() - 1) == '1') { + // '0' idx ã + int idx = s.lastIndexOf('0'); + + String zeroStr = ""; + + if(idx < 0){ + for(int i=0; i Date: Sun, 4 Dec 2022 10:34:40 +0900 Subject: [PATCH 097/111] [leetcode]477: Total Hamming Distance --- daejik/Week_26/leetcode477.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 daejik/Week_26/leetcode477.java diff --git a/daejik/Week_26/leetcode477.java b/daejik/Week_26/leetcode477.java new file mode 100644 index 0000000..4266969 --- /dev/null +++ b/daejik/Week_26/leetcode477.java @@ -0,0 +1,21 @@ +class Solution { + public int totalHammingDistance(int[] nums) { + int n = nums.length; + int ans = 0; + + for(int i=0; i<32; i++){ + int one = 0; + int zero = 0; + + + for(int num : nums){ + if(((num >> i) & 1) == 1) one++; + else zero++; + } + + ans += one * zero; + } + + return ans; + } +} \ No newline at end of file From c442b509cb22b5d2ba21da8994ab4a56c7fb09b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 4 Dec 2022 10:35:04 +0900 Subject: [PATCH 098/111] [leetcode]2037: Minimum Number of Moves to Seat Everyone --- daejik/Week_26/leetcode2037.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 daejik/Week_26/leetcode2037.java diff --git a/daejik/Week_26/leetcode2037.java b/daejik/Week_26/leetcode2037.java new file mode 100644 index 0000000..9910587 --- /dev/null +++ b/daejik/Week_26/leetcode2037.java @@ -0,0 +1,15 @@ +class Solution { + public int minMovesToSeat(int[] seats, int[] students) { + int ans = 0; + int n = seats.length; + + Arrays.sort(seats); + Arrays.sort(students); + + for(int i=0; i Date: Sun, 4 Dec 2022 22:18:40 +0900 Subject: [PATCH 099/111] [leetcode]901: Online Stock Span --- daejik/Week_27/leetcode901.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 daejik/Week_27/leetcode901.java diff --git a/daejik/Week_27/leetcode901.java b/daejik/Week_27/leetcode901.java new file mode 100644 index 0000000..7d39dac --- /dev/null +++ b/daejik/Week_27/leetcode901.java @@ -0,0 +1,19 @@ +class StockSpanner { + // <, > + Stack stack; + + public StockSpanner() { + stack = new Stack<>(); + } + + public int next(int price) { + int ans = 1; + + while(!stack.isEmpty() && stack.peek()[0] <= price){ + ans += stack.pop()[1]; + } + stack.push(new int[]{price, ans}); + + return ans; + } +} \ No newline at end of file From 441193da9103883861d00b5b705de672e4fbb9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 4 Dec 2022 22:18:56 +0900 Subject: [PATCH 100/111] [leetcode]1160: Find Words That Can Be Formed by Characters --- daejik/Week_27/leetcode1160.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 daejik/Week_27/leetcode1160.java diff --git a/daejik/Week_27/leetcode1160.java b/daejik/Week_27/leetcode1160.java new file mode 100644 index 0000000..2f1a875 --- /dev/null +++ b/daejik/Week_27/leetcode1160.java @@ -0,0 +1,28 @@ +class Solution { + public int countCharacters(String[] words, String chars) { + int[] charsCnt = new int['z' - 'a' + 1]; + int ans = 0; + + for(int i=0; i= charsCnt[idx]){ + flag = false; + break; + } + tmpCnt[idx]++; + } + + if(flag == true) ans += nowWord.length(); + } + + return ans; + } +} \ No newline at end of file From 2ce6fe60e86888e1e40da4ef93c0e17e954d4128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 15 Jan 2023 11:06:30 +0900 Subject: [PATCH 101/111] [leetcode]1233: 1233. Remove Sub-Folders from the Filesystem --- daejik/Week_30/leetcode1233.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 daejik/Week_30/leetcode1233.java diff --git a/daejik/Week_30/leetcode1233.java b/daejik/Week_30/leetcode1233.java new file mode 100644 index 0000000..c809ceb --- /dev/null +++ b/daejik/Week_30/leetcode1233.java @@ -0,0 +1,27 @@ +class Solution { + public List removeSubfolders(String[] folder) { + Arrays.sort(folder, Comparator.comparing(s -> s.length())); + Set seen = new HashSet<>(); + + for(String str : folder){ + boolean isInside = false; + int slashIndex = 1; + + while((slashIndex = str.indexOf("/", slashIndex)) > 0){ + String tempStr = str.substring(0, slashIndex); + + if(seen.contains(tempStr) == true){ + isInside = true; + break; + } + slashIndex++; + } + + if(isInside == false){ + seen.add(str); + } + } + + return new ArrayList<>(seen); + } +} \ No newline at end of file From 3b4c291d8ac7921577cd36481d9009949916929b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 15 Jan 2023 11:07:02 +0900 Subject: [PATCH 102/111] [leetcode]2225. Find Players With Zero or One Losses --- daejik/Week_30/leetcode2225.java | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 daejik/Week_30/leetcode2225.java diff --git a/daejik/Week_30/leetcode2225.java b/daejik/Week_30/leetcode2225.java new file mode 100644 index 0000000..9572bf1 --- /dev/null +++ b/daejik/Week_30/leetcode2225.java @@ -0,0 +1,43 @@ +class Solution { + public List> findWinners(int[][] matches) { + Set zeroLossSet = new HashSet<>(); + Set oneLossSet = new HashSet<>(); + Set theOthersSet = new HashSet<>(); + + for (int[] match : matches) { + int winner = match[0]; + int loser = match[1]; + + // (1) ¸ ó + if (!oneLossSet.contains(winner) && !theOthersSet.contains(winner)) { + zeroLossSet.add(winner); + } + + // (2) Loser ó + if (zeroLossSet.contains(loser)) { + // ѹ -> 1 + zeroLossSet.remove(loser); + oneLossSet.add(loser); + } else if (oneLossSet.contains(loser)) { + // ѹ -> 2 ̻ + oneLossSet.remove(loser); + theOthersSet.add(loser); + } else if (theOthersSet.contains(loser)) { + // 2 ̻ ̹ -> ƹ ó ʿ + continue; + } else { + // 𿡵 -> ѹ + oneLossSet.add(loser); + } + } + + // + List> answer = Arrays.asList(new ArrayList<>(), new ArrayList<>()); + answer.get(0).addAll(zeroLossSet); + answer.get(1).addAll(oneLossSet); + Collections.sort(answer.get(0)); + Collections.sort(answer.get(1)); + + return answer; + } +} \ No newline at end of file From d6642693963b0653b02eca44223cdcfed2101483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 12 Feb 2023 00:06:25 +0900 Subject: [PATCH 103/111] [leetcode]1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts --- daejik/Week_31/leetcode1465.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 daejik/Week_31/leetcode1465.java diff --git a/daejik/Week_31/leetcode1465.java b/daejik/Week_31/leetcode1465.java new file mode 100644 index 0000000..de17c59 --- /dev/null +++ b/daejik/Week_31/leetcode1465.java @@ -0,0 +1,19 @@ +class Solution { + public int maxArea(int h, int w, int[] horizontalCuts, int[] verticalCuts) { + Arrays.sort(horizontalCuts); + Arrays.sort(verticalCuts); + + int max_h = horizontalCuts[0]; + int max_v = verticalCuts[0]; + + for (int i = 0; i < horizontalCuts.length - 1; i++) + max_h = Math.max(max_h, horizontalCuts[i + 1] - horizontalCuts[i]); + for (int i = 0; i < verticalCuts.length - 1; i++) + max_v = Math.max(max_v, verticalCuts[i + 1] - verticalCuts[i]); + + max_h = Math.max(max_h, h - horizontalCuts[horizontalCuts.length - 1]); + max_v = Math.max(max_v, w - verticalCuts[verticalCuts.length - 1]); + + return (int)((long)max_h * max_v % 1000000007); + } +} \ No newline at end of file From aec28001d7c5588dd0631f9110770c22da5002c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 12 Feb 2023 00:09:50 +0900 Subject: [PATCH 104/111] [leetcode]2437: Number of Valid Clock Times --- daejik/Week_31/leetcode2437.java | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 daejik/Week_31/leetcode2437.java diff --git a/daejik/Week_31/leetcode2437.java b/daejik/Week_31/leetcode2437.java new file mode 100644 index 0000000..bc71395 --- /dev/null +++ b/daejik/Week_31/leetcode2437.java @@ -0,0 +1,33 @@ +class Solution { + public int countTime(String time) { + int ans = 1; + + if(time.charAt(4) == '?') { + ans *= 10; + } + if(time.charAt(3) == '?'){ + ans *= 6; + } + + if(time.charAt(0) == '?' && time.charAt(1) == '?'){ + ans *= 24; + } else { + if(time.charAt(0) == '?'){ + if(time.charAt(1) < '4') { + ans *= 3; + } else { + ans *= 2; + } + } + if(time.charAt(1) == '?'){ + if(time.charAt(0) == '2'){ + ans *= 4; + } else { + ans *= 10; + } + } + } + + return ans; + } +} \ No newline at end of file From 03884dbeafbb8bc0b9cf156ef089efaab7107933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 5 Mar 2023 10:42:37 +0900 Subject: [PATCH 105/111] [leetcode]20: Valid Parentheses --- daejik/Week_34/leetcode1865.java | 33 +++++++++++++++++++++++++++ daejik/Week_34/leetcode20.java | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 daejik/Week_34/leetcode1865.java create mode 100644 daejik/Week_34/leetcode20.java diff --git a/daejik/Week_34/leetcode1865.java b/daejik/Week_34/leetcode1865.java new file mode 100644 index 0000000..bc71395 --- /dev/null +++ b/daejik/Week_34/leetcode1865.java @@ -0,0 +1,33 @@ +class Solution { + public int countTime(String time) { + int ans = 1; + + if(time.charAt(4) == '?') { + ans *= 10; + } + if(time.charAt(3) == '?'){ + ans *= 6; + } + + if(time.charAt(0) == '?' && time.charAt(1) == '?'){ + ans *= 24; + } else { + if(time.charAt(0) == '?'){ + if(time.charAt(1) < '4') { + ans *= 3; + } else { + ans *= 2; + } + } + if(time.charAt(1) == '?'){ + if(time.charAt(0) == '2'){ + ans *= 4; + } else { + ans *= 10; + } + } + } + + return ans; + } +} \ No newline at end of file diff --git a/daejik/Week_34/leetcode20.java b/daejik/Week_34/leetcode20.java new file mode 100644 index 0000000..f8791f8 --- /dev/null +++ b/daejik/Week_34/leetcode20.java @@ -0,0 +1,39 @@ +class Solution { + public boolean isValid(String s) { + int strLen = s.length(); + Stack stack = new Stack<>(); + + for(String i : s.split("")){ + + if(i.equals("(") || i.equals("{") || i.equals("[")){ + stack.push(i); + } else { + if(stack.empty()){ + return false; + } + } + + if(i.equals(")")){ + if(!"(".equals(stack.peek())){ + return false; + } + stack.pop(); + } else if(i.equals("}")){ + if(!"{".equals(stack.peek())){ + return false; + } + stack.pop(); + } else if(i.equals("]")){ + if(!"[".equals(stack.peek())){ + return false; + } + stack.pop(); + } + } + if(stack.size() == 0){ + return true; + } else { + return false; + } + } +} \ No newline at end of file From caedd2e557ce8992209f7979a1dcbeaabedeffea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 5 Mar 2023 10:43:13 +0900 Subject: [PATCH 106/111] [leetcode]1865: Finding Pairs With a Certain Sum --- daejik/Week_34/leetcode1865.java | 60 +++++++++++++++++--------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/daejik/Week_34/leetcode1865.java b/daejik/Week_34/leetcode1865.java index bc71395..16bfd0a 100644 --- a/daejik/Week_34/leetcode1865.java +++ b/daejik/Week_34/leetcode1865.java @@ -1,33 +1,37 @@ -class Solution { - public int countTime(String time) { - int ans = 1; +class FindSumPairs { + int[] nums1, nums2; + Map map = new HashMap(); // key: num2, value: key - if(time.charAt(4) == '?') { - ans *= 10; - } - if(time.charAt(3) == '?'){ - ans *= 6; - } + public FindSumPairs(int[] nums1, int[] nums2) { + this.nums1 = nums1.clone(); + this.nums2 = nums2.clone(); - if(time.charAt(0) == '?' && time.charAt(1) == '?'){ - ans *= 24; - } else { - if(time.charAt(0) == '?'){ - if(time.charAt(1) < '4') { - ans *= 3; - } else { - ans *= 2; - } - } - if(time.charAt(1) == '?'){ - if(time.charAt(0) == '2'){ - ans *= 4; - } else { - ans *= 10; - } + for(int num2 : nums2){ + map.put(num2, map.getOrDefault(num2, 0) + 1); + } + } + + public void add(int index, int val) { + map.put(nums2[index], map.getOrDefault(nums2[index], 0) - 1); + nums2[index] += val; + map.put(nums2[index], map.getOrDefault(nums2[index], 0) + 1); + } + + public int count(int tot) { + int cnt = 0; + + for(int num1 : nums1){ + if(map.containsKey(tot - num1)){ + cnt += map.get(tot - num1); } } - - return ans; + return cnt; } -} \ No newline at end of file +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * FindSumPairs obj = new FindSumPairs(nums1, nums2); + * obj.add(index,val); + * int param_2 = obj.count(tot); + */ \ No newline at end of file From 493e854183ddc32c12d5fbab75a47805e3753198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 12 Mar 2023 11:22:46 +0900 Subject: [PATCH 107/111] [leetcode]2353. Design a Food Rating System --- daejik/Week_35/leetcode2353.java | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 daejik/Week_35/leetcode2353.java diff --git a/daejik/Week_35/leetcode2353.java b/daejik/Week_35/leetcode2353.java new file mode 100644 index 0000000..214f84d --- /dev/null +++ b/daejik/Week_35/leetcode2353.java @@ -0,0 +1,64 @@ +class FoodRatings { + + HashMap> highestRatingFoodInCuisine = new HashMap<>(); + HashMap foodToCuisine = new HashMap<>(); + HashMap foodToRaiting = new HashMap<>(); + + public FoodRatings(String[] foods, String[] cuisines, int[] ratings) { + int n = foods.length; + + for(int i=0; i foodOfThisCuisine = highestRatingFoodInCuisine.getOrDefault(cuisine, + new TreeSet ((a,b)-> + foodToRaiting.get(a).equals(foodToRaiting.get(b)) ? a.compareTo(b) : foodToRaiting.get(b)-foodToRaiting.get(a))); + // Both comparators are equal + /* new Comparator(){ + @Override + public int compare(String a, String b){ + int aRat = foodToRat.get(a); + int bRat = foodToRat.get(b); + + if(aRat != bRat) return bRat - aRat; // largest rating first + for(int i = 0; i < Math.min(a.length(), b.length()); i++){ + if(a.charAt(i) != b.charAt(i)) return a.charAt(i) - b.charAt(i); + } + return a.length() - b.length(); + } + }) + */ + + foodOfThisCuisine.add(food); + highestRatingFoodInCuisine.put(cuisine, foodOfThisCuisine); + } + } + + // CompareTo() is used to compare whether 2 strings are equal in hashSet! + // So remove, change value of key in HashMap, then insert again + public void changeRating(String food, int newRating) { + String cuisine = foodToCuisine.get(food); + foodToRaiting.put(food, newRating); + + TreeSet foodOfThisCuisine = highestRatingFoodInCuisine.get(cuisine); + foodOfThisCuisine.remove(food); + foodOfThisCuisine.add(food); + highestRatingFoodInCuisine.put(cuisine, foodOfThisCuisine); + } + + public String highestRated(String cuisine) { + return highestRatingFoodInCuisine.get(cuisine).first(); + } +} + +/** + * Your FoodRatings object will be instantiated and called as such: + * FoodRatings obj = new FoodRatings(foods, cuisines, ratings); + * obj.changeRating(food,newRating); + * String param_2 = obj.highestRated(cuisine); + */ \ No newline at end of file From 423a42e5959963fb28ad8e78a1181f6ba5b5c2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 19 Mar 2023 10:36:33 +0900 Subject: [PATCH 108/111] [leetcode]1642: Furthest Building You Can Reach --- daejik/Week_36/leetcode1642.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 daejik/Week_36/leetcode1642.java diff --git a/daejik/Week_36/leetcode1642.java b/daejik/Week_36/leetcode1642.java new file mode 100644 index 0000000..a1b8cfa --- /dev/null +++ b/daejik/Week_36/leetcode1642.java @@ -0,0 +1,16 @@ +class Solution { + PriorityQueue pq = new PriorityQueue<>(); + + public int furthestBuilding(int[] heights, int bricks, int ladders) { + for (int i = 0; i < heights.length - 1; i++) { + int diff = heights[i + 1] - heights[i]; + if (diff > 0) + pq.add(d); + if (pq.size() > ladders) + bricks -= pq.poll(); + if (bricks < 0) + return i; + } + return heights.length - 1; + } +} \ No newline at end of file From 33f8ff5cc26da0ff66ef09145a8ed45cceda7744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 19 Mar 2023 10:59:00 +0900 Subject: [PATCH 109/111] [leetcode]659: Split Array into Consecutive Subsequences --- daejik/Week_36/leetcode659.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 daejik/Week_36/leetcode659.java diff --git a/daejik/Week_36/leetcode659.java b/daejik/Week_36/leetcode659.java new file mode 100644 index 0000000..3c2412c --- /dev/null +++ b/daejik/Week_36/leetcode659.java @@ -0,0 +1,28 @@ +class Solution { + public boolean isPossible(int[] nums) { + Map ready = new HashMap<>(); + Map end = new HashMap<>(); + + for(int num : nums){ + ready.put(num, ready.getOrDefault(num, 0) + 1); + } + + for(int num : nums){ + if(ready.get(num) == 0) continue; + ready.put(num, ready.get(num) - 1); + + if(end.containsKey(num - 1) && end.get(num - 1) > 0){ + end.put(num - 1, end.get(num - 1) - 1); + end.put(num, end.getOrDefault(num, 0) + 1); + } else if(ready.containsKey(num + 1) && ready.containsKey(num + 2) + && ready.get(num + 1) > 0 && ready.get(num + 2) > 0){ + ready.put(num + 1, ready.get(num + 1) - 1); + ready.put(num + 2, ready.get(num + 2) - 1); + end.put(num + 2, end.getOrDefault(num + 2, 0) + 1); + } else { + return false; + } + } + return true; + } +} \ No newline at end of file From dd693243aa1abcce3b1c089f953c8ff8d48c89fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 9 Apr 2023 11:15:08 +0900 Subject: [PATCH 110/111] =?UTF-8?q?[Programmers]=20=ED=83=9D=EB=B0=B0=20?= =?UTF-8?q?=EB=B0=B0=EB=8B=AC=EA=B3=BC=20=EC=88=98=EA=B1=B0=ED=95=98?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\261\260\355\225\230\352\270\260.java" | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 "daejik/Week_37/\355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.java" diff --git "a/daejik/Week_37/\355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.java" "b/daejik/Week_37/\355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.java" new file mode 100644 index 0000000..283c501 --- /dev/null +++ "b/daejik/Week_37/\355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.java" @@ -0,0 +1,20 @@ +class Solution { + public long solution(int cap, int n, int[] deliveries, int[] pickups) { + long answer = 0; + int deliver = 0, pickup = 0; + + for(int i=n-1; i>=0; i--){ + int cnt = 0; + + while(deliver < deliveries[i] || pickup < pickups[i]){ + cnt++; + deliver += cap; + pickup += cap; + } + deliver -= deliveries[i]; + pickup -= pickups[i]; + answer += (i + 1) * cnt * 2; + } + return answer; + } +} \ No newline at end of file From 887a26a3cf14a2883baaf31475f24f0299fe8f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8C=80=EC=A7=81?= <30641972+leedjik@users.noreply.github.com> Date: Sun, 9 Apr 2023 11:15:41 +0900 Subject: [PATCH 111/111] =?UTF-8?q?[Programmers]=20=EB=AF=B8=EB=A1=9C=20?= =?UTF-8?q?=ED=83=88=EC=B6=9C=20=EB=AA=85=EB=A0=B9=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\252\205\353\240\271\354\226\264.java" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "daejik/Week_37/\353\257\270\353\241\234 \355\203\210\354\266\234 \353\252\205\353\240\271\354\226\264.java" diff --git "a/daejik/Week_37/\353\257\270\353\241\234 \355\203\210\354\266\234 \353\252\205\353\240\271\354\226\264.java" "b/daejik/Week_37/\353\257\270\353\241\234 \355\203\210\354\266\234 \353\252\205\353\240\271\354\226\264.java" new file mode 100644 index 0000000..a0e408c --- /dev/null +++ "b/daejik/Week_37/\353\257\270\353\241\234 \355\203\210\354\266\234 \353\252\205\353\240\271\354\226\264.java" @@ -0,0 +1,52 @@ +class Solution { + // d -> l -> r -> u + int[] dr = {1, 0, 0, -1}; + int[] dc = {0, -1, 1, 0}; + String[] word = {"d", "l", "r", "u"}; + int maxR = 0, maxC = 0; + int goalR = 0, goalC = 0; + String tmpAnswer = ""; + + public boolean dfs(int r, int c, int k, String str, int diff){ + + if(diff > k) return false; + if(k == 0 && diff == 0){ + tmpAnswer = str; + return true; + } + for(int d=0; d<4; d++){ + int nr = r + dr[d]; + int nc = c + dc[d]; + + if(nr >= 0 && nc >= 0 && nr < maxR && nc < maxC){ + if((diff % 2 == 0 && k % 2 == 0) || (diff % 2 == 1 && k % 2 == 1)){ + + if(dfs(nr, nc, k - 1, str + word[d], Math.abs(nr - goalR) + Math.abs(nc - goalC))){ + return true; + } + } + } + } + return false; + } + + public String solution(int n, int m, int x, int y, int r, int c, int k) { + String answer; + maxR = n; + maxC = m; + goalR = r - 1; + goalC = c - 1; + + int diff = Math.abs((r - 1) - (x - 1)) + Math.abs((c - 1) - (y - 1)); + + dfs(x - 1, y - 1, k, "", diff); + + answer = tmpAnswer; + + if(answer.equals("")){ + answer = "impossible"; + } + + return answer; + } +} \ No newline at end of file