diff --git "a/BOJ/1000-5000353円262円210円/DH_1030.java" "b/BOJ/1000-5000353円262円210円/DH_1030.java" new file mode 100644 index 00000000..d8c45abd --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/DH_1030.java" @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + +/* + * 프렉탈 평면 + */ + +public class DH_1030 { + static StringBuilder sb = new StringBuilder(); + static int s, N, K, R1, R2, C1, C2; + + public static void main(String[] args) throws Exception { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + s = stoi(st); N = stoi(st); K = stoi(st); + R1 = stoi(st); R2 = stoi(st); + C1 = stoi(st); C2 = stoi(st); + + // 시간 s일 때, R1행 C1열부터 R2행 C2열까지의 모습을 출력 + if(s == 0) sb.append("0"); + else { + for(int r = R1; r < R2 + 1; r++) { + for(int c = C1; c < C2 + 1; c++) { + int outer = (int) Math.pow(N, s); + double percent = ((N - K)>> 1) * (1.0) / N; + sb.append(getBlockStatus(r, c, s, N, outer, percent)); + } + + sb.append("\n"); + } + } + + System.out.print(sb); + } + + static int getBlockStatus(int r, int c, int s, int n, int outer, double percent) { + + // 바깥 사각형과 안쪽 사각형 사이 간격 구하기 + int interval = (int) (outer * percent); + + if(isInCenter(r, c, interval, outer)) return 1; + if(s == 1) return 0; + + // 그 다음 확인할 사각형 사이즈 + int nextRecSize = (outer / n); + return getBlockStatus(r % nextRecSize, c % nextRecSize, s - 1, n, nextRecSize, percent); + } + + static boolean isInCenter(int r, int c, int interval, int outer) { + return r>= interval && r < (outer - interval) && c>= interval && c < (outer - interval); + } + + static int stoi(StringTokenizer st) { + return Integer.parseInt(st.nextToken()); + } +} diff --git "a/BOJ/1000-5000353円262円210円/DH_1725.java" "b/BOJ/1000-5000353円262円210円/DH_1725.java" new file mode 100644 index 00000000..27a7d35d --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/DH_1725.java" @@ -0,0 +1,114 @@ +import java.io.*; +import java.util.*; + +/* + * 히스토그램 + */ + +public class DH_1725 { + +// ---------- 스택 사용 코드 ---------- + static int[] arr; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + arr = new int[N + 2]; + // 양 끝에 0을 넣어줌 + for (int i = 1; i < N + 1; i++) arr[i] = Integer.parseInt(br.readLine()); + + long result = getMaxArea(N); + System.out.println(result); + } + + static long getMaxArea(int N) { + long maxArea = 0; + Stack stack = new Stack(); + + for (int i = 0; i < arr.length; i++) { + while (!stack.isEmpty() && arr[stack.peek()]> arr[i]) { + int height = arr[stack.pop()]; + int width = i - (stack.isEmpty() ? 0 : stack.peek() + 1); + maxArea = Math.max(maxArea, height * width); + } + + // push 하려는 막대의 높이가 stack.peek()보다 크다면 stack에 넣어주기 + stack.push(i); + } + + return maxArea; + } + +// ---------- 시간 초과 코드 ---------- + static int[] tree, idxTree; + static final int INF = Integer.MAX_VALUE; + static void timeOutCode() throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); + + arr = new int[N + 2]; + + int k = (int) Math.ceil(Math.log(N) / Math.log(2)) + 1; + tree = new int[1 << k]; + idxTree = new int[1 << k]; + + for(int i = 1; i < N + 1; i++) arr[i] = Integer.parseInt(br.readLine()); + + long result = 0; + for(int i = 1; i < N + 1; i++) { + int l = getIdx(0, i - 1, i), r = getIdx(i + 1, N + 1, i); + result = Math.max(result, (r - l - 1) * arr[i]); + } + + System.out.println(result); + } + + static int getIdx(int s, int e, int c) { + // 인덱스가 s와 e 사이이면서 현재(c)보다 작은 숫자를 가진 원소의 idx 구하기 + boolean isLeft = e <= c; + int k = (int) Math.ceil(Math.log(e - s + 1) / Math.log(2)) + 1; + + int idx = 1 << (k - 1); + for(int i = s; i < e + 1; i++) { + tree[idx + i - s] = arr[i]; + + // 인덱스 저장 (idxTree에서 0인 것과 0번 인덱스를 구분하기 위해, 진짜 인덱스에 + 1을 해줌) + idxTree[idx + i - s] = i + 1; + } + + if(isLeft) { + + for(int i = idx + e + 1 - s; i < idxTree.length; i++) { + tree[i] = tree[i - 1]; + idxTree[i] = idxTree[i - 1]; + } + } + + for(int i = idx - 1; i> 0; i--) { + int a = i * 2; + int b = i * 2 + 1; + + if(tree[a] < arr[c] && tree[b] < arr[c]) { + if(isLeft) { + tree[i] = tree[b]; + idxTree[i] = idxTree[b]; + } else { + tree[i] = tree[a]; + idxTree[i] = idxTree[a]; + } + } else if(tree[a] < arr[c] && tree[b]>= arr[c]) { + tree[i] = tree[a]; + idxTree[i] = idxTree[a]; + } else if(tree[a]>= arr[c] && tree[b] < arr[c]) { + tree[i] = tree[b]; + idxTree[i] = idxTree[b]; + } else { + tree[i] = tree[a]; + if(isLeft) idxTree[i] = Math.min(idxTree[a], idxTree[b]); + else idxTree[i] = Math.max(idxTree[a], idxTree[b]); + } + } + + return idxTree[1] - 1; + } +} diff --git "a/BOJ/1000-5000353円262円210円/DH_1992.java" "b/BOJ/1000-5000353円262円210円/DH_1992.java" new file mode 100644 index 00000000..b6b83927 --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/DH_1992.java" @@ -0,0 +1,52 @@ +import java.io.*; + +/* + * 쿼드트리 + */ + +public class DH_1992 { + static int[][] arr; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int N = Integer.parseInt(br.readLine()); + arr = new int[N][N]; + + for(int r = 0; r < N; r++) { + String s = br.readLine(); + + for(int c = 0; c < N; c++) { + arr[r][c] = s.charAt(c) - '0'; + } + } + + int length = N; + + String str = func(0, 0, length); + System.out.println(str); + } + + static String func(int r, int c, int l) { + String s = ""; + + int sum = 0; + for(int sr = r; sr < r + l; sr++) { + for(int sc = c; sc < c + l; sc++) { + sum += arr[sr][sc]; + } + } + + if(sum == 0) s += "0"; + else if(sum == l * l) s += "1"; + else { + int nl = l>> 1; + s += "("; + s += func(r, c, nl); // 왼쪽 위 + s += func(r, c + nl, nl); // 오른쪽 위 + s += func(r + nl, c, nl); // 왼쪽 아래 + s += func(r + nl, c + nl, nl); // 오른쪽 아래 + s += ")"; + } + return s; + } +} diff --git "a/BOJ/1000-5000353円262円210円/DH_4803.java" "b/BOJ/1000-5000353円262円210円/DH_4803.java" new file mode 100644 index 00000000..7d32c084 --- /dev/null +++ "b/BOJ/1000-5000353円262円210円/DH_4803.java" @@ -0,0 +1,72 @@ +import java.io.*; +import java.util.*; + +/* + * 트리 + */ + +public class DH_4803 { + static boolean[] v; + static ArrayList adj[]; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + String s = ""; + StringBuilder sb = new StringBuilder(); + + int tc = 1; + while(!(s = br.readLine()).equals("0 0")) { + st = new StringTokenizer(s); + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + + v = new boolean[n + 1]; + adj = new ArrayList[n + 1]; + + for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList(); + + // 간선 리스트 생성 + for(int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + adj[a].add(b); + adj[b].add(a); + } + + // 트리의 개수를 세는 변수 + int treeCnt = 0; + + for(int i = 1; i < adj.length; i++) { + if(v[i]) continue; + treeCnt += dfs(0, i); + } + + sb.append("Case ").append(tc++).append(": "); + if(treeCnt == 0) sb.append("No trees."); + else if(treeCnt == 1) sb.append("There is one tree."); + else sb.append("A forest of ").append(treeCnt).append(" trees."); + sb.append("\n"); + } + + System.out.print(sb); + } + + // 사이클 확인 (사이클이 발생한다면 0, 발생하지 않는다면 1 반환) + // prev: 직전 노드, current: 현재 노드 + static int dfs(int prev, int current) { + v[current] = true; + + for(int next: adj[current]) { + // 현재 노드를 기준으로 다음 노드로 감 + // 양방향 그래프이기 때문에 다음 노드를 갈 때는 직전 노드를 제외한 다음 노드로 가야됨 + if(next == prev) continue; + + // 이미 방문했던 노드거나, 사이클이 발생한다면 0 반환 + if(v[next] || dfs(current, next) == 0) return 0; + } + // 사이클이 발생하지 않는다면 1 반환 + return 1; + } +} diff --git "a/BOJ/5001-10000353円262円210円/DH_9372.java" "b/BOJ/5001-10000353円262円210円/DH_9372.java" new file mode 100644 index 00000000..38a3af3b --- /dev/null +++ "b/BOJ/5001-10000353円262円210円/DH_9372.java" @@ -0,0 +1,61 @@ +import java.io.*; +import java.util.*; + +/* + * 상근이의 여행 + */ + +public class DH_9372 { + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + int T = Integer.parseInt(br.readLine()); + + Queue q = new ArrayDeque(); + StringBuilder sb = new StringBuilder(); + + for(int t = 0; t < T; t++) { + st = new StringTokenizer(br.readLine()); + + int n = Integer.parseInt(st.nextToken()); + int m = Integer.parseInt(st.nextToken()); + + boolean[] v = new boolean[n + 1]; + ArrayList adj[] = new ArrayList[n + 1]; + for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList(); + + for(int i = 0; i < m; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + adj[a].add(b); + adj[b].add(a); + } + + int answer = 0; + + for(int i = 1; i < n; i++) { + if(v[i]) continue; + q.add(i); + v[i] = true; + + while(!q.isEmpty()) { + int current = q.poll(); + + for(int next: adj[current]) { + if(v[next]) continue; + answer += 1; + q.add(next); + v[next] = true; + } + } + } + + sb.append(answer).append("\n"); + } + + System.out.println(sb); + } +} diff --git "a/CodeTree/2019-2020353円205円204円/DH_354円234円267円353円206円200円354円235円264円_354円202円254円352円270円260円353円213円250円.java" "b/CodeTree/2019-2020353円205円204円/DH_354円234円267円353円206円200円354円235円264円_354円202円254円352円270円260円353円213円250円.java" new file mode 100644 index 00000000..2ebb7e70 --- /dev/null +++ "b/CodeTree/2019-2020353円205円204円/DH_354円234円267円353円206円200円354円235円264円_354円202円254円352円270円260円353円213円250円.java" @@ -0,0 +1,106 @@ +import java.io.*; +import java.util.*; + +public class DH_윷놀이_사기단 { + static final int TURN = 10; + static int[] arr, sel; // 중복 순열 + static int[][] map; + static class Horse { + int r, c; + public Horse() {} + @Override + public String toString() { + return "Horse [r=" + r + ", c=" + c + "]"; + } + } + static int maxResult; + static boolean[] v; + static Horse[] horses; + + public static void main(String[] args) throws Exception { + horses = new Horse[5]; + v = new boolean[5]; + for(int i = 0; i < horses.length; i++) horses[i] = new Horse(); + + map = new int[][]{ + {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 41}, + {10, 13, 16, 19, 25, 30, 35, 40, 41}, + {20, 22, 24, 25, 30, 35, 40, 41}, + {30, 28, 27, 26, 25, 30, 35, 40, 41}}; + + initInput(); + solution(); + System.out.println(maxResult); + } + + + static void solution() { + sel = new int[10]; + func(0, 0); + } + + static void func(int depth, int result) { + if(depth == TURN) { + maxResult = Math.max(maxResult, result); + return; + } + + for(int i = 1; i < 5; i++) { + if(v[i]) continue; + sel[depth] = i; + + int currentR = horses[i].r; + int currentC = horses[i].c; + + int nr = currentR; + int nc = currentC + arr[depth]; + + if(nc>= map[nr].length) { + nc = map[nr].length - 1; + v[i] = true; + } else { + if(map[nr][nc] == 10 || map[nr][nc] == 20 || + (map[nr][nc] == 30 && map[nr][nc - 1] == 28)) { + nr = map[nr][nc] / 10; + nc = 0; + } + + boolean canGo = true; + + for(int j = 1; j < 5; j++) { + if(j == i) continue; + if(map[horses[i].r][horses[i].c] == 0 || map[horses[j].r][horses[j].c] == 0) continue; + if(map[horses[i].r][horses[i].c] == 41 || map[horses[j].r][horses[j].c] == 41) continue; + + if(nr == horses[j].r && nc == horses[j].c) { + canGo = false; + break; + } + } + + if(!canGo) continue; + } + + horses[i].r = nr; + horses[i].c = nc; + + func(depth + 1, result + (map[nr][nc] % 41)); + + horses[i].r = currentR; + horses[i].c = currentC; + + if(currentC < map[currentR].length) { + v[i] = false; + } + } + } + + static void initInput() throws Exception { + System.setIn(new FileInputStream("./input/윶놀이사기단.txt")); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + arr = new int[10]; + for(int i = 0; i < 10; i++) arr[i] = Integer.parseInt(st.nextToken()); + } +} diff --git a/Programmers/Level3/DH_42892.java b/Programmers/Level3/DH_42892.java new file mode 100644 index 00000000..0dccdbe4 --- /dev/null +++ b/Programmers/Level3/DH_42892.java @@ -0,0 +1,86 @@ +import java.util.*; + +/* + * 길 찾기 게임 + */ + +public class DH_42892 { + static class Node { + int v, x, y; + Node l, r; + + public Node(int v, int x, int y) { + this.v = v; + this.x = x; + this.y = y; + } + } + static Node[] nodes; + static int idx; + + static int[][] solution(int[][] nodeInfo) { + + nodes = new Node[nodeInfo.length]; + + for(int i = 0; i < nodeInfo.length; i++) { + nodes[i] = new Node(i + 1, nodeInfo[i][0], nodeInfo[i][1]); + } + + // 우선순위에 따라 정렬 + Arrays.sort(nodes, (o1, o2)-> { + if(o1.y != o2.y) return -1 * Integer.compare(o1.y, o2.y); + return Integer.compare(o1.x, o2.x); + }); + + Node root = nodes[0]; + + // 트리 만들기 + for(int i = 1; i < nodes.length; i++) { + Node current = nodes[i]; + makeTree(root, current); + } + + int[][] answer = new int[2][nodeInfo.length]; + + preOrder(root, answer); + + idx = 0; + postOrder(root, answer); + + return answer; + } + + // 전위순회 + static void preOrder(Node p, int[][] answer) { + if(p == null) return; + answer[0][idx++] = p.v; + preOrder(p.l, answer); + preOrder(p.r, answer); + } + + // 후외순회 + static void postOrder(Node p, int[][] answer) { + if(p == null) return; + postOrder(p.l, answer); + postOrder(p.r, answer); + answer[1][idx++] = p.v; + + } + + // [1]: x 좌표, [2]: y좌표 + static void makeTree(Node p, Node current) { + if(p.x < current.x) { + if(p.r == null) p.r = current; + else makeTree(p.r, current); + } + else { + if(p.l == null) p.l = current; + else makeTree(p.l, current); + } + } + + public static void main(String[] args) throws Exception { + int[][] nodeInfo = {{5, 3}, {11, 5}, {13, 3}, {3, 5}, {6, 1}, {1, 3}, {8, 6}, {7, 2}, {2, 2}}; + System.out.println(solution(nodeInfo)); + } +} diff --git "a/SQL/15354円243円274円354円260円250円/Odd and Even Transactions.sql" "b/SQL/15354円243円274円354円260円250円/Odd and Even Transactions.sql" new file mode 100644 index 00000000..fb89e87b --- /dev/null +++ "b/SQL/15354円243円274円354円260円250円/Odd and Even Transactions.sql" @@ -0,0 +1,28 @@ +WITH EVEN AS ( + SELECT TRANSACTION_DATE, SUM(AMOUNT) `amount` + FROM TRANSACTIONS + WHERE MOD(AMOUNT, 2) = 0 + GROUP BY TRANSACTION_DATE +), ODD AS ( + SELECT TRANSACTION_DATE, SUM(AMOUNT) `amount` + FROM TRANSACTIONS + WHERE MOD(AMOUNT, 2) = 1 + GROUP BY TRANSACTION_DATE +) + +SELECT DISTINCT(a.TRANSACTION_DATE) `transaction_date` + , IFNULL(c.AMOUNT, 0) `odd_sum` + , IFNULL(b.AMOUNT, 0) `even_sum` +FROM TRANSACTIONS a + LEFT JOIN EVEN b ON a.transaction_date = b.transaction_date + LEFT JOIN ODD c ON a.transaction_date = c.transaction_date +ORDER BY transaction_date + +------------------------------------------ + +SELECT transaction_date + , SUM(CASE WHEN MOD(AMOUNT, 2) = 1 THEN AMOUNT ELSE 0 END) `odd_sum` + , SUM(CASE WHEN MOD(AMOUNT, 2) = 0 THEN AMOUNT ELSE 0 END) `even_sum` +FROM TRANSACTIONS +GROUP BY TRANSACTION_DATE +ORDER BY TRANSACTION_DATE \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /